Spring boot rest service options 401 on oauth/token

前端 未结 4 1777
予麋鹿
予麋鹿 2020-12-05 16:06

I\'m using spring boot to make a simple rest service. To consume it in Angular 2, I\'ve got CORS problem when retrieving token on oauth/token endpoint.

The error mes

相关标签:
4条回答
  • 2020-12-05 16:34

    The browser checks CORS settings via a request with OPTIONS header. And if you've configured authorization, OPTIONS request will be blocked as unauthorized.

    You simply can add cors support in WebConfigurerAdapter.

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // ...
            http.cors();
        }
    }
    

    Check this link for more info: https://www.baeldung.com/spring-security-cors-preflight

    0 讨论(0)
  • 2020-12-05 16:35

    You can add this CORS Filter to your project

        @Component
        @Order(Ordered.HIGHEST_PRECEDENCE)
    
        public class SimpleCORSFilter implements Filter {
    
        @Override
        public void init(FilterConfig fc) throws ServletException {
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp,
                FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) resp;
            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, Content-Type, Authorization, credential, X-XSRF-TOKEN");
    
            if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                chain.doFilter(req, resp);
            }
    
        }
    
        @Override
        public void destroy() {
        }
    
     }
    
    0 讨论(0)
  • 2020-12-05 16:35

    Here is native Spring Framework solution:

    @Bean
    public CorsFilter corsFilter(CorsConfiguration config) {
        log.debug("Registering CORS filter");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/api/**", config);
        source.registerCorsConfiguration("/v2/api-docs", config);
        source.registerCorsConfiguration("/oauth/**", config);
        return new CorsFilter(source);
    }
    
    0 讨论(0)
  • 2020-12-05 16:36

    if you are using spring boot + Spring OAuth you must add

    @Order(Ordered.HIGHEST_PRECEDENCE)
    

    to your CORS filter

    @Configuration
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class CORSFilter implements Filter {
    
        private FilterConfig config;
    
        public static final String CREDENTIALS_NAME = "Access-Control-Allow-Credentials";
        public static final String ORIGIN_NAME = "Access-Control-Allow-Origin";
        public static final String METHODS_NAME = "Access-Control-Allow-Methods";
        public static final String HEADERS_NAME = "Access-Control-Allow-Headers";
        public static final String MAX_AGE_NAME = "Access-Control-Max-Age";
    
        @Override
        public void destroy() {
    
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp,
                             FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) resp;
            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, Content-Type, Authorization, credential, X-XSRF-TOKEN");
    
            if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                chain.doFilter(req, resp);
            }
    
        }
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            config = filterConfig;
        }
    }
    
    0 讨论(0)
提交回复
热议问题