编程语言
首页 > 编程语言> > java-在spring控制器中访问当前用户,getAuthentication()返回null

java-在spring控制器中访问当前用户,getAuthentication()返回null

作者:互联网

我试图实施此question.中提供的解决方案

但是,他们都没有为我工作.在我的抽象基本控制器中,我有以下方法(我也使用静态方法在helper类中尝试过,但存在相同的问题):

public User getUser() {
  User user = 
    (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();       
}

即使在我的jsps中,getAuthentication()返回null,也可以正常工作:

<sec:authentication property="principal.firstname" /> 

因为我的自定义用户对象是可访问的.

为什么getAuthentication返回null?

另外,我尝试将Principal作为参数传递给我的特定控制器方法,这是null也会解析为null …

为什么我的主体为null,但jsp仍在工作?

解决方法:

确保在< intercept-url>中没有过滤器=“ none”.对于控制器的URL,它会禁用Spring Security过滤器,因此不会填充安全上下文.

如果需要使每个人都可以访问控制器,请使用other approaches.

标签:spring-security,spring,java,spring-mvc
来源: https://codeday.me/bug/20191202/2086692.html