【发布时间】:2019-08-25 19:51:35
【问题描述】:
我们正在使用带有 JWT 令牌的 Spring-boot 2.0.5
SecurityContextHolder.getContext().getAuthentication().getPrincipal() 返回 String (Username) 代替 CustomUserDetails 对象,该对象仅在 WAR 文件中实现 UserDetails 类。
这个问题只是偶尔重复,大多数时候代码运行良好
下面提到的是我们使用的唯一自定义过滤器,它在 FilterSecurityInterceptor Filter 之后调用
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();
}
}
它应该返回用户详细信息对象
【问题讨论】:
-
这取决于配置,在我们的例子中,我们在 AuthorizationServerEndpointsConfigurer 中使用 DefaultUserAuthenticationConverter 并且它依赖于 UserDetailsService 来获取 UserDetails 如果未设置您将获得一个字符串,即用户名而不是 UserDetail 对象。跨度>
标签: linux spring-boot jwt war spring-security-oauth2