访问当前用户,在spring控制器中,getAuthentication()返回null [英] Accessing the current user, in a spring controller, getAuthentication() returns null

查看:1472
本文介绍了访问当前用户,在spring控制器中,getAuthentication()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施此问题中提供的解决方案。

I have attempted to implement the solutions provided in this question.

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

However, none of them are working for me. In my abstract base controller, I have the following method (I also tried in helper class with static methods, but same problem):

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

getAuthentication()返回null ,甚至虽然在我的jsps中,以下功能完美:

the getAuthentication() is returning null, even though in my jsps, the following is working perfectly:

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

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

In that my custom user object is accesible.

为什么getAuthentication返回null?

Why does getAuthentication return null ?

另外,我尝试将Principal as参数传递给我的特定控制器方法,这个null也解析为null ...

Also, I tried passing in the Principal as parameter to my specific controller method, this is null resolves to null too ...

为什么我的校长为空,但是jsp工作正常?

Why is my principal null, but yet the jsp is working ?

推荐答案

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

Make sure you don't have filters = "none" in <intercept-url> for your controller's URL, it disables Spring Security filters, thus security context doesn't get populated.

如果您需要让所有人都可以使用您的控制器,请使用其他方法

If you need to make your controller accessible for everyone, use other approaches.

这篇关于访问当前用户,在spring控制器中,getAuthentication()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆