在spring控制器中访问当前用户,getAuthentication()返回null
问题内容:
但是,他们都没有为我工作。在我的抽象基本控制器中,我有以下方法(我也使用静态方法在helper类中尝试过,但存在相同的问题):
public User getUser() {
User user =
(User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
在 getAuthentication()的返回null ,即使在我的JSP,以下是可以正常使用:
<sec:authentication property="principal.firstname" />
因为我的自定义用户对象是可访问的。
为什么getAuthentication返回null?
另外,我尝试将Principal作为参数传递给我的特定控制器方法,这是null也会解析为null …
为什么我的主体为null,但jsp仍在工作?
问题答案:
请确保你没有filters = "none"
在<intercept-url>
你的控制器的URL,它禁用spring安全过滤器,因此安全上下文没有得到填充。