View Javadoc
1   package org.imageconverter.config.filter;
2   
3   import java.io.IOException;
4   import java.util.Objects;
5   
6   import javax.servlet.FilterChain;
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import org.springframework.security.web.csrf.CsrfToken;
12  import org.springframework.web.filter.OncePerRequestFilter;
13  
14  /**
15   * Filter created to pass the CSRF token on response.
16   * 
17   * @author Fernando Romulo da Silva
18   */
19  public final class CsrfLoggerFilter extends OncePerRequestFilter {
20  
21      /**
22       * {@inheritDoc}
23       */
24      @Override
25      protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
26  
27  	final var csrfToken = (CsrfToken) request.getAttribute("_csrf");
28  
29  	if (Objects.nonNull(csrfToken)) {
30  	    response.setHeader("XSRF-TOKEN", csrfToken.getToken());
31  	}
32  
33  	filterChain.doFilter(request, response);
34      }
35  }