【问题标题】:Spring Boot and PreFlight requestsSpring Boot 和 PreFlight 请求
【发布时间】:2018-04-13 18:08:49
【问题描述】:

我正在使用Spring Boot version 2.0.0.M5

我的JavaScript 应用程序在调用我暴露的 Rest 端点时遇到问题。 为了安全起见 - 我希望传入一个包含 api 密钥的标头。 但是 - 预检请求(选项)没有设置标头

以下JavaScriptsn-p 为我重现了错误

var baseEndpoint = "http://localhost:8480";
var data = null; 
var xhr = new XMLHttpRequest(); 
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});
xhr.open("GET", baseEndpoint + "/api/issue/user/1");
xhr.setRequestHeader("x-api-key", "fdarwetr5467hyyerf");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("Accept","application/json");
xhr.setRequestHeader("Content-Type","application/json");
xhr.setRequestHeader("Origin", "localhost");
xhr.send(null);

即使我得到它是一个 OPTIONS 请求 - x-api-key 标头也不会通过

我在 Spring Boot 中的 cors 设置如下

@Override
public void addCorsMappings(CorsRegistry corsRegistry) {
    super.addCorsMappings(corsRegistry);
    corsRegistry.addMapping("/**").allowedMethods("*").allowedOrigins("*").exposedHeaders("x-api-key", "apiKey").allowedHeaders("x-api-key", "apiKey").allowCredentials(true);      
}

我想知道处理这些预检请求的正确方法是什么?或者有没有人有任何建议让标题通过?

【问题讨论】:

    标签: javascript java spring spring-boot cors


    【解决方案1】:

    我通过在所有 api 请求上添加过滤器解决了这个问题 当请求方法为 OPTIONS 时——我刚刚成功退出过滤器

        if ("OPTIONS".equals(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return super.preHandle(request, response, handler);
        }
    

    【讨论】:

    • 您需要在您的应用程序中打开所有选项请求吗?
    • 我没有..没有特别需要这样做
    • 您不需要为此使用自定义过滤器。您可以扩展 WebSecurityConfigurerAdapter 配置类,只需覆盖 void configure(WebSecurity web) 里面只需调用 web.ignoring().antMathers(HttpMethod.OPTIONS, "/**");
    猜你喜欢
    • 2021-02-22
    • 2020-10-20
    • 2020-06-30
    • 2014-01-14
    • 1970-01-01
    • 2018-05-18
    • 2019-11-25
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多