我们使用带有 JWT token 的 Spring-boot 2.0.5
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
返回 String (Username)
代替 CustomUserDetails
实现的对象仅 WAR 文件中的 UserDetails
类。
这个问题只是偶尔重复,大部分时间代码运行良好
下面提到的是我们使用的唯一自定义过滤器,它在 FilterSecurityInterceptor 过滤器之后调用
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
CustomHttpServletRequestWrapper request = new CustomHttpServletRequestWrapper((HttpServletRequest) req);
if(request!=null && request.getHeader("Authorization")!=null && request.getHeader("Authorization").length()>7) {
CustomUserDetails user = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
它应该返回用户详细信息对象
最佳答案
信息不足,无法为您提供任何答案,最好知道:
- 什么实例从
SecurityContextHolder.getContext().getAuthentication()
返回? - 您拥有的安全过滤器列表是什么?您的自定义过滤器位于何处?
- 这是什么时候发生的?你必须知道重现它的步骤
关于linux - Spring Security getprincipal() 方法返回字符串(用户名)代替类 UserDetails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55509468/