-
-
Save CemalUnal/5c7aa5206c1a6f6e26b28d672b697a72 to your computer and use it in GitHub Desktop.
CORS filter for Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.filter.OncePerRequestFilter; | |
@Component | |
public class CorsFilter extends OncePerRequestFilter { | |
@Override | |
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | |
response.setHeader("Access-Control-Allow-Origin", "*"); | |
response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); | |
response.setHeader("Access-Control-Max-Age", "3600"); | |
response.setHeader("Access-Control-Allow-Headers", "authorization, content-type, xsrf-token"); | |
response.addHeader("Access-Control-Expose-Headers", "xsrf-token"); | |
if ("OPTIONS".equals(request.getMethod())) { | |
response.setStatus(HttpServletResponse.SC_OK); | |
} else { | |
filterChain.doFilter(request, response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment