How to specify response headers to CORS?

后端 未结 3 728
猫巷女王i
猫巷女王i 2020-12-20 02:21

I am building a backend REST API in spring and my friend is building a Angular JS front end app to call my API.I have a token header with key Authorization and

3条回答
  •  天命终不由人
    2020-12-20 02:50

    Here is the filter which avoid the preflight error

            @Override
            protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException {
                LOG.info("Adding CORS Headers ........................");        
                res.setHeader("Access-Control-Allow-Origin", "*");
                res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                res.setHeader("Access-Control-Max-Age", "3600");
                res.setHeader("Access-Control-Allow-Headers", "X-PINGOTHER,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization");
                res.addHeader("Access-Control-Expose-Headers", "xsrf-token");
                if ("OPTIONS".equals(req.getMethod())) {
                 res.setStatus(HttpServletResponse.SC_OK);
                } else { 
                 chain.doFilter(req, res);
                }        
            }
    

    Found it from the post Cross Origin Request Blocked Spring MVC Restful Angularjs

提交回复
热议问题