欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot 跨域配置無效及接口訪問報錯的解決方法

 更新時間:2020年12月28日 14:58:08   作者:程序員大佬超  
這篇文章主要介紹了Springboot 跨域配置無效及接口訪問報錯的解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

跨域配置如下,Springboot 版本為 2.4.1

///跨域訪問配置
@Configuration
public class CorsConfig {
 private CorsConfiguration buildConfig() {
  CorsConfiguration corsConfiguration = new CorsConfiguration();
  corsConfiguration.setAllowCredentials(true); //sessionid 多次訪問一致
  corsConfiguration.addAllowedOrigin("*"); // 允許任何域名使用
  corsConfiguration.addAllowedHeader("*"); // 允許任何頭
  corsConfiguration.addAllowedMethod("*"); // 允許任何方法(post、get等)
  return corsConfiguration;
 }

 @Bean
 public CorsFilter corsFilter() {
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  source.registerCorsConfiguration("/**", buildConfig()); // 對接口配置跨域設(shè)置
  return new CorsFilter(source);
 }
}

問題:跨域配置無效,訪問接口報如下錯誤

java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
	at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:453) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.cors.CorsConfiguration.checkOrigin(CorsConfiguration.java:557) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.cors.DefaultCorsProcessor.checkOrigin(DefaultCorsProcessor.java:174) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.cors.DefaultCorsProcessor.handleInternal(DefaultCorsProcessor.java:116) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.cors.DefaultCorsProcessor.processRequest(DefaultCorsProcessor.java:95) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:87) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.2.jar:5.3.2]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_60]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_60]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.41.jar:9.0.41]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]

分析:

由于我是升級了 Springboot 到 2.4.1 版本之后才出現(xiàn)的這個問題,再結(jié)合報錯信息提示不能使用*號設(shè)置允許的Origin,所以有兩個解決方法。

解決方法:

1、降低 Springboot 版本

 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

2、如果不降低版本,則在跨域設(shè)置時使用setAllowedOriginPatterns方法

///跨域訪問配置
@Configuration
public class CorsConfig {
 private CorsConfiguration buildConfig() {
  CorsConfiguration corsConfiguration = new CorsConfiguration();
  corsConfiguration.setAllowCredentials(true); //sessionid 多次訪問一致

  // 允許訪問的客戶端域名
  List<String> allowedOriginPatterns = new ArrayList<>();
  allowedOriginPatterns.add("*");
  corsConfiguration.setAllowedOriginPatterns(allowedOriginPatterns);
//  corsConfiguration.addAllowedOrigin("*"); // 允許任何域名使用
  corsConfiguration.addAllowedHeader("*"); // 允許任何頭
  corsConfiguration.addAllowedMethod("*"); // 允許任何方法(post、get等)
  return corsConfiguration;
 }

 @Bean
 public CorsFilter corsFilter() {
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  source.registerCorsConfiguration("/**", buildConfig()); // 對接口配置跨域設(shè)置
  return new CorsFilter(source);
 }
}

到此這篇關(guān)于Springboot 跨域配置無效及接口訪問報錯的解決方法的文章就介紹到這了,更多相關(guān)Springboot 跨域配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Intellij IDEA如何自定義注釋模板的實現(xiàn)方法

    Intellij IDEA如何自定義注釋模板的實現(xiàn)方法

    這篇文章主要介紹了Intellij IDEA如何自定義注釋模板的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 詳解Spring中實現(xiàn)接口動態(tài)的解決方法

    詳解Spring中實現(xiàn)接口動態(tài)的解決方法

    最近在工作遇到的一個,發(fā)現(xiàn)網(wǎng)上的資料較少,所以想著總結(jié)分享下,下面這篇文章主要給大家介紹了關(guān)于Spring中實現(xiàn)接口動態(tài)的解決方法,文中通過完整的示例代碼給大家介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Java遞歸實現(xiàn)菜單樹的方法詳解

    Java遞歸實現(xiàn)菜單樹的方法詳解

    這篇文章主要為大家詳細介紹了Java遞歸實現(xiàn)菜單樹的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 分析講解Java?Random類里的種子問題

    分析講解Java?Random類里的種子問題

    Random類中實現(xiàn)的隨機算法是偽隨機,也就是有規(guī)則的隨機。在進行隨機時,隨機算法的起源數(shù)字稱為種子數(shù)(seed),在種子數(shù)的基礎(chǔ)上進行一定的變換,從而產(chǎn)生需要的隨機數(shù)字
    2022-05-05
  • Java中多態(tài)性的實現(xiàn)方式

    Java中多態(tài)性的實現(xiàn)方式

    這篇文章主要介紹了Java中多態(tài)性的實現(xiàn)方式,什么是多態(tài)?通過簡單的一道題目幫大家理解java多態(tài)性,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • java如何根據(jù)IP獲取當前區(qū)域天氣信息詳解

    java如何根據(jù)IP獲取當前區(qū)域天氣信息詳解

    根據(jù)IP自動獲取當?shù)氐奶鞖忸A(yù)報信息這個功能大家應(yīng)該都遇到過,天氣預(yù)報信息用途非常廣泛,篇文章主要給大家介紹了關(guān)于java如何根據(jù)IP獲取當前區(qū)域天氣信息的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Java List集合方法及遍歷過程代碼解析

    Java List集合方法及遍歷過程代碼解析

    這篇文章主要介紹了Java List集合方法及遍歷過程代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • java接口使用默認方法的講解

    java接口使用默認方法的講解

    在本篇文章里小編給大家整理了一篇關(guān)于java接口使用默認方法的講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2021-04-04
  • Java超詳細教你寫一個學(xué)籍管理系統(tǒng)案例

    Java超詳細教你寫一個學(xué)籍管理系統(tǒng)案例

    這篇文章主要介紹了怎么用Java來寫一個學(xué)籍管理系統(tǒng),學(xué)籍管理主要涉及到學(xué)生信息的增刪查改,本篇將詳細的實現(xiàn),感興趣的朋友跟隨文章往下看看吧
    2022-03-03
  • java中instanceof和getClass()的區(qū)別分析

    java中instanceof和getClass()的區(qū)別分析

    本篇文章介紹了,在java中instanceof和getClass()的區(qū)別分析。需要的朋友參考下
    2013-04-04

最新評論