JwtAuthenticationTokenFilter.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.api.base.config.auth;
  2. import com.api.base.config.auth.service.DetailsService;
  3. import com.api.common.JSONUtils;
  4. import com.api.core.response.Result;
  5. import com.api.core.response.ResultEnum;
  6. import com.api.core.response.ResultGenerator;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.data.redis.RedisConnectionFailureException;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.context.SecurityContextHolder;
  13. import org.springframework.security.core.userdetails.UserDetails;
  14. import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.filter.OncePerRequestFilter;
  17. import javax.servlet.FilterChain;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. @Component
  23. public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
  24. @Value("${jwt.header}")
  25. private String tokenHeader;
  26. @Value("${jwt.tokenHead}")
  27. private String tokenHead;
  28. private DetailsService userDetailsService;
  29. private JwtTokenUtil jwtTokenUtil;
  30. @Autowired
  31. public JwtAuthenticationTokenFilter(DetailsService userDetailsService, JwtTokenUtil jwtTokenUtil) {
  32. this.userDetailsService = userDetailsService;
  33. this.jwtTokenUtil = jwtTokenUtil;
  34. }
  35. @Override
  36. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  37. try {
  38. String authHeader = request.getHeader(tokenHeader);
  39. if (authHeader != null && authHeader.startsWith(tokenHead)) {
  40. String authToken = authHeader.substring(tokenHead.length());
  41. String username = jwtTokenUtil.getUsernameFromToken(authToken);
  42. if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
  43. UserDetails userDetails = userDetailsService.loadUserByUsername(username);
  44. if (jwtTokenUtil.validateToken(authToken, username)) {
  45. UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
  46. authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
  47. SecurityContextHolder.getContext().setAuthentication(authentication);
  48. }
  49. }
  50. }
  51. filterChain.doFilter(request, response);
  52. }catch (Exception e){
  53. logger.error("TokenFilterException",e);
  54. response.setHeader("Content-Type", "application/json;charset=utf-8");
  55. response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
  56. Result result = ResultGenerator.genExceptionResult();
  57. if(e instanceof RedisConnectionFailureException){
  58. result = ResultGenerator.genResult(ResultEnum.REDIS_CONNECTION_FAILUR);
  59. }
  60. response.getWriter().write(JSONUtils.obj2json(result));
  61. response.getWriter().flush();
  62. }
  63. }
  64. }