lenve / vhr

微人事是一个前后端分离的人力资源管理系统,项目采用SpringBoot+Vue开发。
http://vhr.javaboy.org
27.28k stars 10.29k forks source link

关于权限控制 SecurityContextHolder.getContext().getAuthentication() == null #191

Closed nevermore75 closed 4 years ago

nevermore75 commented 4 years ago

hi macro:

在过滤器 JwtAuthenticationTokenFilter的逻辑中,

if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
    UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
    if (jwtTokenUtil.validateToken(authToken, userDetails)) {
        UsernamePasswordAuthenticationToken authentication = new
                UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        LOGGER.info("authenticated user:{}", username);
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }
}

已经set 了Authentication,但debug时发现SecurityContextHolder.getContext().getAuthentication() 总是为 null。 网上查找资料有看到: 在过滤链执行完时org.springframework.security.web.context.SecurityContextPersistenceFilter类会 调用SecurityContextHolder.clearContext();而把SecurityContextHolder清空,所以会得到null。 有什么合适的解决方法么