|
| 1 | +package io.jsonwebtoken.jjwtfun.config; |
| 2 | + |
| 3 | +import io.jsonwebtoken.JwtException; |
| 4 | +import io.jsonwebtoken.Jwts; |
| 5 | +import io.jsonwebtoken.jjwtfun.service.SecretService; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 9 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 10 | +import org.springframework.security.web.csrf.CsrfFilter; |
| 11 | +import org.springframework.security.web.csrf.CsrfToken; |
| 12 | +import org.springframework.security.web.csrf.CsrfTokenRepository; |
| 13 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 14 | + |
| 15 | +import javax.servlet.FilterChain; |
| 16 | +import javax.servlet.RequestDispatcher; |
| 17 | +import javax.servlet.ServletException; |
| 18 | +import javax.servlet.http.HttpServletRequest; |
| 19 | +import javax.servlet.http.HttpServletResponse; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +@Configuration |
| 24 | +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + CsrfTokenRepository jwtCsrfTokenRepository; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + SecretService secretService; |
| 31 | + |
| 32 | + // ordered so we can use binary search below |
| 33 | + private String[] ignoreCsrfAntMatchers = { |
| 34 | + "/dynamic-builder-compress", |
| 35 | + "/dynamic-builder-general", |
| 36 | + "/dynamic-builder-specific", |
| 37 | + "/set-secrets" |
| 38 | + }; |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void configure(HttpSecurity http) throws Exception { |
| 42 | + http |
| 43 | + .addFilterAfter(new JwtCsrfValidatorFilter(), CsrfFilter.class) |
| 44 | + .csrf() |
| 45 | + .csrfTokenRepository(jwtCsrfTokenRepository) |
| 46 | + .ignoringAntMatchers(ignoreCsrfAntMatchers) |
| 47 | + .and().authorizeRequests() |
| 48 | + .antMatchers("/**") |
| 49 | + .permitAll(); |
| 50 | + } |
| 51 | + |
| 52 | + private class JwtCsrfValidatorFilter extends OncePerRequestFilter { |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
| 56 | + // NOTE: A real implementation should have a nonce cache so the token cannot be reused |
| 57 | + |
| 58 | + CsrfToken token = (CsrfToken) request.getAttribute("_csrf"); |
| 59 | + |
| 60 | + if ( |
| 61 | + // only care if it's a POST |
| 62 | + "POST".equals(request.getMethod()) && |
| 63 | + // ignore if the request path is in our list |
| 64 | + Arrays.binarySearch(ignoreCsrfAntMatchers, request.getServletPath()) < 0 && |
| 65 | + // make sure we have a token |
| 66 | + token != null |
| 67 | + ) { |
| 68 | + // CsrfFilter already made sure the token matched. Here, we'll make sure it's not expired |
| 69 | + try { |
| 70 | + Jwts.parser() |
| 71 | + .setSigningKeyResolver(secretService.getSigningKeyResolver()) |
| 72 | + .parseClaimsJws(token.getToken()); |
| 73 | + } catch (JwtException e) { |
| 74 | + // most likely an ExpiredJwtException, but this will handle any |
| 75 | + request.setAttribute("exception", e); |
| 76 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 77 | + RequestDispatcher dispatcher = request.getRequestDispatcher("expired-jwt"); |
| 78 | + dispatcher.forward(request, response); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + filterChain.doFilter(request, response); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments