Advertisement
andersonalmada2

Untitled

Aug 12th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package br.ufc.mandacaru5.config;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.annotation.WebFilter;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. import org.springframework.core.Ordered;
  15. import org.springframework.core.annotation.Order;
  16. import org.springframework.stereotype.Component;
  17.  
  18. @Component
  19. @Order(Ordered.HIGHEST_PRECEDENCE)
  20. @WebFilter("/*")
  21. public class CorsFilter implements Filter {
  22.  
  23.     public CorsFilter() {
  24.     }
  25.  
  26.     @Override
  27.     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  28.         final HttpServletResponse response = (HttpServletResponse) res;
  29.         response.setHeader("Access-Control-Allow-Origin", "*");
  30.         response.setHeader("Access-Control-Allow-Methods", "*");
  31.         response.setHeader("Access-Control-Allow-Headers", "*");
  32.         if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {
  33.             response.setStatus(HttpServletResponse.SC_OK);
  34.         } else {
  35.             chain.doFilter(req, res);
  36.         }
  37.     }
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement