原理:

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. 依赖注入(IOC) 详解

    https://blog.csdn.net/qq_27093465/article/details/52547290 https://blog.csdn.net/qq_27093465/article ...

  2. Python __str__(self)和__unicode__(self)

    object.__str__(self) Called by the str() built-in function and by the print statement to compute the ...

  3. 【Python】列表解析表达式

    1.语法 [expression for iter_val in iterable] [expression for iter_val in iterable if cond_expr] 2.示例 s ...

  4. 微信小程序组件解读和分析:九、form表单

    form表单组件说明: 表单,将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/ ...

  5. windows 安装绿色版mysql

    (1)到官网下载绿色版mysql:http://dev.mysql.com/downloads/mysql/ (2)下载好后,放在F:\mysql,解压出来 (3)进入到mysql-5.6.19-wi ...

  6. nginx访问php程序相关配置

    server { listen *:80; charset utf-8; server_name roujiaxiaomowang.wanghaokun.com mowang.crucco.com; ...

  7. 【译】x86程序员手册37-第10章 初始化

    Chapter 10 Initialization 第10章 初始化 After a signal on the RESET pin, certain registers of the 80386 a ...

  8. IOS 面试题系列

    随着iOS平台开发的职位的增加,笔试.面试也越来越有“套路”,这里我总结了一些面试题,多数是Objective-C的基础知识,适合于面试新人,答案是我自己答的,不准确的地方,欢迎指出. 1.   Ob ...

  9. Java EE 目标

    在大三上学期学习了Java se,只是简单的学习了语法,而且没有及时的复习巩固,语法知识已经忘了许多.在这个新学期,又有了Java EE这门课,书上的内容是从没学习过的新知识,只是在网站上看到过像Sp ...

  10. Spring boot 配置tomcat后 控制台不打印SQL日志

    在pom.xml中配置tomcat启动处加上: <dependency> <groupId>org.springframework.boot</groupId> & ...