原理:

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();

如果想用上面的代码获得当前用户,必须在spring security过滤器执行中执行,否则在过滤链执行完时org.springframework.security.web.context.SecurityContextPersistenceFilter类会

调用SecurityContextHolder.clearContext();而把SecurityContextHolder清空,所以会得到null。    经过spring security认证后,

security会把一个SecurityContextImpl对象存储到session中,此对象中有当前用户的各种资料。

网上的情况:

1.关键就是要把filters="none" 变化为相应的权限如access="permitAll"(必须设置<http auto-config="true" use-expressions="true">,否则会提示permitAll找不到),或者access = "IS_AUTHENTICATED_ANONYMOUSLY, IS_AUTHENTICATED_FULLY, IS_AUTHENTICATED_REMEMBERED",当然security 3.1是要修改<http pattern="/login" security="none"/>这类的

原文:https://blog.csdn.net/softwarehe/article/details/7710707/

2.SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

3.删除 <intercept-url pattern="/aaa/bbb*" filters="none"/> 类似配置

我的情况:

我给静态资源js配置了<security:http pattern="/js/**" security="none"/>

又给action加了@Namespace("/js/xjgzjs") 注解

所以在访问这个action时,没有经过spring security过滤器,SecurityContextHolder清空,所以会得到null。

把@Namespace("/js/xjgzjs")改为@Namespace("/ry/xjgzjs")即可。

SecurityContextHolder.getContext().getAuthentication()为null的情况的更多相关文章

  1. SecurityContextHolder.getContext().getAuthentication() return null

    <security:http> <security:intercept-url pattern="/web/**" access="IS_AUTHENT ...

  2. 有关Mysql的mysql_store_result函数返回NULL的情况以及其他注意事项

    成功调用mysql_query()后,mysql_store_result()能够返回NULL.出现该情况时,表明出现了下述条件之一: ·         出现了malloc()故障(例如,如果结果集 ...

  3. php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。

    php 在linux 用fopen() 函数打开,fread()函数 读取  另外一台服务器映射过来的文件 总是返回false,null的情况. #获取平台类型   $type='android';  ...

  4. mybatis sql语句中 in() 长度为0或null的情况

    mybatis sql语句中 in() 长度为0或null的情况 比如: select * from A where colName IN <foreach collection="m ...

  5. 使用COALESCE时注意left join为null的情况

    1.使用COALESCE时,用到group by with cube,如果之前两个表left join时,有数据为null,就会使得查出的数据主键不唯一 例如: select COALESCE (c. ...

  6. Oracle“不等于号”与Null的情况

    今天突然才发现,Oracle中的“不等于操作符”是忽略Null的. 比如,查询comm不等于的300的记录,我会理所当然地使用where comm != 300 预想会返回包含Null的不等于300的 ...

  7. Mybatis中sql语句中的in查询,一定要判断null的情况

    不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...

  8. sql 在not in 子查询有null值情况下经常出现的陷阱

    如果下:TempSalesPriceFixedValues表和SalesPriceFixedValues表,要求查询出在TempSalesPriceFixedValues表中且不在SalesPrice ...

  9. 在@Async注解下RequestContextHolder.getRequestAttributes() 获得null的情况

    我们有的时候会在service层获取request填充一些诸如用户名和IP地址等信息,这个时候如果不想从Controller层传request,可以在service直接使用 HttpServletRe ...

随机推荐

  1. iOS programming UITableView and UITableViewController

    iOS programming  UITableView and UITableViewController A UITableView displays a single column of dat ...

  2. windows上把git生成的ssh key

    右键鼠标,选中 “Git Bash here”: 输入指令,创建ssh key: cd ~/.ssh/ #bash: cd: /c/Users/Administrator/.ssh/: No such ...

  3. testlink 从1.8.5 升级到 1.9.8

    step1:备份原 1.8.5 的数据库.   step2:分别下载 1.9.0 / 1.9.3 / 1.9.8 的安装包.   step3:分别解压 1.9.0 / 1.9.3 / 1.9.8 成3 ...

  4. How do I get started with Node.js

    From: http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js Tutorials NodeSch ...

  5. Win7系统32位和64位的区别

    Win7系统32位和64位的区别已经是一个老话题了,可是还是有很多朋友不明白.这两者到底有什么区别呢?下面本文与大家通俗的介绍下Win7系统32位和64位的区别,其他一些深入的理论讲述,大家可以看看文 ...

  6. CAD交互绘制文字(com接口)

    在cad设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...

  7. MFC中调用Windows API函数的方式

    windows aoi 函数的调用前面加::

  8. 文件描述符 文件操作 <> open 文件句柄

    #! /usr/bin/perl use strict;use warnings; =head1print "\n---------------------------------test_ ...

  9. 数据库中的Schema是什么?

    在数据库中,schema(发音 “skee-muh” 或者“skee-mah”,中文叫模式)是数据库的组织和结构,schemas andschemata都可以作为复数形式.模式中包含了schema对象 ...

  10. 零基础入门学习Python(27)--集合:在我的世界里,你就是唯一

    知识点 集合:set set和dict类似,也是一组key的集合,但不存储value.由于key不能重复,所以,在set中,没有重复的key. 集合中的元素的三个特征: 1)确定性(元素必须可hash ...