欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot 跨域配置无效及接口访问报错的解决方法

程序员文章站 2022-06-24 18:20:23
跨域配置如下,springboot 版本为 2.4.1///跨域访问配置@configurationpublic class corsconfig { private corsconfiguratio...

跨域配置如下,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()); // 对接口配置跨域设置
  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 版本之后才出现的这个问题,再结合报错信息提示不能使用*号设置允许的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、如果不降低版本,则在跨域设置时使用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()); // 对接口配置跨域设置
  return new corsfilter(source);
 }
}

到此这篇关于springboot 跨域配置无效及接口访问报错的解决方法的文章就介绍到这了,更多相关springboot 跨域配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!