Spring boot 弹簧靴CORS头

Spring boot 弹簧靴CORS头,spring-boot,cors,Spring Boot,Cors,我对CORS头文件和Spring boot实现是新手。我正在接受请求正文的邮政服务上启用CORS标头。 第一次发出飞行前请求时,运行良好并返回200,但当调用实际的post请求时,它总是返回403禁止,响应主体为“无效CORS请求”我已经阅读了几乎所有的spring文档和所有google/stackoverflow讨论,但没有发现我遗漏了什么..嗯 在下面的代码片段中,我通过在类顶部和方法顶部添加crossOrigin进行了测试,但没有成功。 @CrossOrigin(origins = "ht

我对CORS头文件和Spring boot实现是新手。我正在接受请求正文的邮政服务上启用CORS标头。

第一次发出飞行前请求时,运行良好并返回200,但当调用实际的post请求时,它总是返回403禁止,响应主体为“无效CORS请求”

我已经阅读了几乎所有的spring文档和所有google/stackoverflow讨论,但没有发现我遗漏了什么..嗯

在下面的代码片段中,我通过在类顶部和方法顶部添加crossOrigin进行了测试,但没有成功。

@CrossOrigin(origins = "https://domain/", allowCredentials = "false")
@RequestMapping(value = ApplicationConstants.URI_PATH)
class MainController {
       @RequestMapping(value = '/postMethod', method = RequestMethod.POST)
       Map<String, Object> postMethod(HttpServletResponse servletResponse, 
       @RequestBody(required = false) AccessToken requestedConsumerInfo) {...}
邮寄电话:状态代码403

Response headers (364 B)
Cache-Control   max-age=0, private, no-cache, …roxy-revalidate, no-transform
Connection  close
Content-Length  20
Date    Wed, 20 Dec 2017 17:57:14 GMT
Pragma  no-cache
Server  nginx/1.9.1
Strict-Transport-Security max-age=63072000; includeSubdomains;
Vary    User-Agent
X-Frame-Options SAMEORIGIN
X-XSS-Protection    1; mode=block

Request headers (2.507 KB)  
Accept  application/json, text/plain, */*
Accept-Encoding gzip, deflate, br
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Content-Length  102
Content-Type    application/json
Cookie  rxVisitor=1513720811976ARCUHEC…B4SL3K63V8|6952d9a33183e7bc|1
Host    domain
Origin  https://domain
Referer https://domain/home/account/register
User-Agent  Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0
由于这不起作用,我还单独添加了全局配置并与上面的代码片段一起进行了测试,但没有成功。

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            void addCorsMappings(CorsRegistry registry) {
                super.addCorsMappings(registry);
                registry.addMapping(ApplicationConstants.MEMBER_URL_PATH)
                        .allowedOrigins("https://domain/")
                        .allowedMethods(HttpMethod.GET.toString(), 
                                   HttpMethod.POST.toString(), HttpMethod.PUT.toString());
            }
        }
    }

在飞行前选项请求中,服务器应响应以下所有内容(看起来您已经这样做了):

Access Control Allow Origin、Access Control Allow Methods、Access Control Allow header、Access Control Allow Credentials(如果传递了cookie)


在实际的POST请求中,您需要至少返回
访问控制允许来源
访问控制允许凭据
。您当前没有为POST响应返回它们。

我也遇到了同样的问题,然后使用了@CrossOrigin注释,效果很好,但只是为了GET,当我尝试发布帖子时,仍然出现了CrossOrigin错误,然后我解决了这个问题:

创建一个拦截器,并将访问控制头添加到响应中。 (您可能不需要全部)

然后添加拦截器:

@Configuration
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("++++++ WebConfig addInterceptors() ");
        registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/**");
    }

}

我希望这能为您节省一些时间,我花了一段时间才开始工作。

但是“您当前没有返回用于POST响应的凭据”需要什么配置?实际上,您给了我一个很好的提示“访问控制允许凭据(如果传递了cookies)”。我正在控制器中设置cookies和allowCredentials=“false”,这导致了问题。谢谢你的帮助!
public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse httpResponse, Object handler, ModelAndView modelAndView)
            throws Exception { 
        httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        httpResponse.setHeader("Access-Control-Allow-Headers", "*");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Max-Age", "4800");
    } 
}
@Configuration
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("++++++ WebConfig addInterceptors() ");
        registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/**");
    }

}