亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CORS 問題 - 對預檢請求的響應未通過訪問控制檢查:

CORS 問題 - 對預檢請求的響應未通過訪問控制檢查:

守候你守候我 2023-04-19 16:46:18
我正在研究 reactjs 前端,在后端使用 spring boot。我正在嘗試從前端調用端點,如下所示:testOktaTokenAtRest(data) {  var oauth=JSON.parse(localStorage.getItem('okta-token-storage'))  console.log("toekn is: ==>  "+oauth.accessToken.tokenType +   oauth.accessToken.accessToken)  console.log("toekn received from action is inside this obj: ",data)  var searchCriteria = JSON.stringify(data.data)  console.log("searchCriteria data -------: " , searchCriteria) let _headerForSearch={    auth : 'Bearer ' + oauth.accessToken.accessToken   }  $.ajax({    method: "post",    url: "http://localhost:8000/ckcapp/api/posttest",    contentType: "application/json",    dataType: "json",    data:searchCriteria,    beforeSend: function (xhr) {      xhr.setRequestHeader("Authorization", _headerForSearch.auth);    },    success: function(response) {      console.log("response from okta enabled get api is: ",response)    },    error: function(xhr, status, error) {      console.log("error from okta enabled get api is: ",xhr.responseText      + " " +status + " " + error );    }  });  }當我提出請求時,出現以下錯誤:-從來源“ http://localhost: 3000”訪問位于“ http://localhost :8000/ckcapp/api/posttest”的 XMLHttpRequest已被 CORS 策略阻止:對預檢請求的響應未通過訪問控制檢查:否請求的資源上存在“Access-Control-Allow-Origin”標頭。我的 spring-boot 應用程序具有以下配置:CORSFilter    public class CORSFilter implements Filter {    private static final String ONE_HOUR = "3600";      @Override      public void init(FilterConfig filterConfig) throws ServletException     {      }
查看完整描述

2 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

我通常使用一個 bean 來配置我的 CORS 設置。

@Bean

public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {

? ? UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

? ? CorsConfiguration config = new CorsConfiguration();

? ? config.setAllowCredentials(true);

? ? config.setAllowedOrigins(Collections.singletonList("*"));

? ? config.setAllowedMethods(Collections.singletonList("*"));

? ? config.setAllowedHeaders(Collections.singletonList("*"));

? ? source.registerCorsConfiguration("/**", config);

? ? FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));

? ? bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

? ? return bean;

}


查看完整回答
反對 回復 2023-04-19
?
森林海

TA貢獻2011條經驗 獲得超2個贊

我將 React 與 Spring JWT 一起使用時遇到了與 CORS 策略阻止的類似問題。


添加http.cors()到我的 SecurityConfigurer 類中以解決


@EnableWebSecurity

public class SecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and().csrf().disable().......

    }

}

控制器

@CrossOrigin(origins = {"http://localhost:3000"})

@RestController


查看完整回答
反對 回復 2023-04-19
  • 2 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號