Advertisement
ADL_Rodrigo_Silva

Untitled

Nov 2nd, 2022
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import io.jsonwebtoken.Claims;
  2. import io.jsonwebtoken.Jwts;
  3. import org.springframework.web.filter.GenericFilterBean;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11.  
  12. public class JwtFilter extends GenericFilterBean {
  13.  
  14. @Override
  15. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  16. throws IOException, ServletException {
  17.  
  18. final HttpServletRequest request = (HttpServletRequest) servletRequest;
  19. final HttpServletResponse response = (HttpServletResponse) servletResponse;
  20. final String authHeader = request.getHeader("authorization");
  21.  
  22. if ("OPTIONS".equals(request.getMethod())) {
  23. response.setStatus(HttpServletResponse.SC_OK);
  24. filterChain.doFilter(request, response);
  25. } else {
  26. if (authHeader == null || !authHeader.startsWith("Bearer ")) {
  27. throw new ServletException("An exception occurred");
  28. }
  29. }
  30.  
  31. final String token = authHeader.substring(7);
  32.  
  33. Claims claims = Jwts.parser().setSigningKey("secret").parseClaimsJws(token).getBody();
  34.  
  35. request.setAttribute("claims", claims);
  36. request.setAttribute("blog", servletRequest.getParameter("id"));
  37.  
  38. filterChain.doFilter(request, response);
  39.  
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement