How to alter allowed headers in Spring Boot

前端 未结 3 2036
南笙
南笙 2021-01-22 23:58

I\'m currently using Auth0 (and an Angular 2 GUI), which sends a header of the type \"x-xsrf-token\" in the request to a Spring Boot API.

I get the error:

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 00:48

    Ultimately I solved this myself. I removed this dependency here in the pom.xml file:

    
                com.auth0
                auth0-spring-security-api
                0.3.1
     
    

    because it is an open source project on github, here https://github.com/auth0/auth0-spring-security-api. I added the source code to my project in its own package, and added its dependencies to my pom.xml file. Then I changed the doFilter method in the Auth0CORSFilter to include my x-xsrf-token:

    @Override
    public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException, ServletException {
        final HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Authorization, x-xsrf-token, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, " +
                "Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
        chain.doFilter(req, res);
    }
    

    Unfortunately, I now won't be able to switch versions as easily if I need to, I also have a slightly more cluttered codebase, however as I am new to Spring this was far easier than spending hours trying to override the Auth0CORSFilter Bean, if that was ever possible.

提交回复
热议问题