Standalone Spring OAuth2 JWT Authorization Server + CORS

前端 未结 6 1323
长发绾君心
长发绾君心 2020-11-28 04:01

So I have the following Authorization Server condensed from this example from Dave Syer

@SpringBootApplication
publi         


        
6条回答
  •  Happy的楠姐
    2020-11-28 04:23

    Found the reason for my Problem!

    I just needed to end the filterchain and return the result immediatly if a OPTIONS request is processed by the CorsFilter!

    SimpleCorsFilter.java

    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class SimpleCorsFilter implements Filter {
    
        public SimpleCorsFilter() {
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            HttpServletRequest request = (HttpServletRequest) req;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization");
    
            if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                chain.doFilter(req, res);
            }
        }
    
        @Override
        public void init(FilterConfig filterConfig) {
        }
    
        @Override
        public void destroy() {
        }
    }
    

    After that I could ignore the OPTIONS preflight request in my AuthServer =D

    So the Server works as in the snipped above and you can ignore the block comment with MyWebSecurity class in the beginning.

提交回复
热议问题