简体   繁体   English

预检请求未通过访问控制检查:它没有 HTTP ok 状态

[英]Preflight request doesn't pass access control check: It does not have HTTP ok status

I'm trying to fetch data from api Spring Boot using axios.我正在尝试从 api Spring 使用 axios 引导获取数据。

If I call the api with Insomnia or Postman It work as expected, I have the data like:如果我打电话给 api InsomniaPostman它按预期工作,我有如下数据:

[
  {
    "id": 2,
    "username": "Admin",   
    "profil": null,
    "enabled": true,
    "roles": [
      {
        "id": 1,
        "name": "Admin"
      }
    ]
  }
]

But using axios or fetch It throws a Cors error:但是使用 axios 或 fetch 会引发 Cors 错误:
Access to XMLHttpRequest at 'http://localhost:8181' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status . Access to XMLHttpRequest at 'http://localhost:8181' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status .

So I'm confused, why the header data is not passed to server when using axios?所以我很困惑,为什么在使用 axios 时没有将 header 数据传递到服务器? I can't have the Authorization or test我无法获得Authorizationtest

BackEnd config:后端配置:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*");
        }
    };
}

Axios request: Axios 请求:

function getData() {
    const baseURL = 'http://localhost:8181';
    const config: AxiosRequestConfig = {
      headers: {
        Authorization: 'Bearer TQyMDQ1OX0.C5oj83xiEAnRVdXZ24hpZnoRjCA8QsiN23kxylQCwdWCyTJyZOKcH_b4E5Ktv3A0ACVs4mj1XhrmqMJT_MHkeQ',
        test: 'test-data',
        'Access-Control-Allow-Origin': '*',
      },
    };

    axios.get(baseURL, config).then((result) => {
      console.log(result);
    });
  }

I think I missed something.我想我错过了什么。 Need a help please需要帮助请

I found out the solution.我找到了解决方案。 I fixed it by adding this line to my spring securty web config我通过将此行添加到我的 spring 安全 web 配置来修复它

protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.cors();
//other configs
 }

I fixed it by adding this line我通过添加这一行来修复它

httpSecurity.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()

I don't know what's in the middle of your controller, but you can try to remove AxiosRequestConfig.我不知道你的controller中间是什么,但你可以尝试删除AxiosRequestConfig。

function getData() {
  const baseURL = 'http://localhost:8181';
  axios.get(baseURL).then((result) => {
    console.log(result);
  });
}

If the request type is a POST type如果请求类型是 POST 类型

function getData() {
  const baseURL = 'http://localhost:8181';
  axios.post(baseURL).then((result) => {
    console.log(result);
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC Azure + AjaxCall + microsofttranslator +对预检请求的响应未通过访问控制检查:它没有HTTP ok状态 - MVC Azure + AjaxCall + microsofttranslator + Response to preflight request doesn't pass access control check: It does not have HTTP ok status CORS:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态。 REACTJS, AXIOS - CORS: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. REACTJS, AXIOS 对预检请求的响应未通过 Angular 2 的访问控制检查 - Response to preflight request doesn't pass access control check with Angular 2 对预检请求的角度响应未通过访问控制检查 - Angular Response to preflight request doesn't pass access control check Javascript - 对预检请求的响应未通过访问控制检查 - Javascript - Response to preflight request doesn't pass access control check 对预检请求的响应未通过访问控制检查 - Response to preflight request doesn't pass access control check 对预检请求的响应未通过访问控制检查(JavaScript) - Response to preflight request doesn't pass access control check (JavaScript) CORS-对预检请求的响应未通过访问控制检查 - CORS - Response to preflight request doesn't pass access control check 对预检请求的响应未通过访问控制检查错误 - Response to preflight request doesn't pass access control check error 对预检请求的响应未通过NodeJS中的访问控制检查 - Response to preflight request doesn't pass access control check in NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM