我是Spring Security的新手。我有一个JWT令牌过滤器,它检查所有请求的授权令牌是否有效。据我所知,如果令牌无效,请求将被取消并返回错误。
@Component
@RequiredArgsConstructor
public class JwtTokenFilter extends OncePerRequestFilter {
private final JwtTokenProvider tokenProvider;
private final UserDetailsService userDetailsService;
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
final String authHeader = request.getHeader("Authorization");
final String token;
final String email;
if (authHeader == null || !authHeader.startsWith("Bearer ")){
filterChain.doFilter(request, response);
return;
}
token = authHeader.substring(7);
email = tokenProvider.getUserEmail(token);
if (email != null && SecurityContextHolder.getContext().getAuthentication() == null){
// User is not yet authenticated (not connected)
UserDetails user = this.userDetailsService.loadUserByUsername(email);
if (tokenProvider.isTokenValid(token, user)){
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
user, null, user.getAuthorities()
);
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authToken);
}
}
filterChain.doFilter(request, response);
}
public String getEmailFromHeader(String header){
if (header == null || !header.startsWith("Bearer ")){
throw new UserNotExistException();
}
String token = header.substring(7);
String email = tokenProvider.getUserEmail(token);
return email;
}
}
我希望用户能够只通过自己的token从控制器接收一些数据。现在我在控制器中有一个方法,可以从头部检索token:
@GetMapping("/friends")
public ResponseEntity<Set<User>> getFriends(@RequestHeader(HttpHeaders.AUTHORIZATION) String header){
String email = tokenFilter.getEmailFromHeader(header);
User user = service.findByEmail(email);
if (user == null){
throw new UserNotExistException();
}
return ResponseEntity.ok(user.getFriends());
}
有没有什么方法可以避免从每个控制器的头中获取令牌?我如何在JWTTokenFilter中进行身份验证后立即获取用户的电子邮件?
1条答案
按热度按时间vsikbqxv1#
如果用户通过了身份验证,则可以从安全上下文保持器获取用户信息
这是security,它将username设置为security context保持器:
控制器:
getCurrentUser()方法返回currentUser,如果想获取username可以返回login,login是username