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