1项目包-配置文件包-新增cors配置类 文件名自定
Cors.java:
package com.rsgk.xrhd.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class Cors extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
.allowCredentials(true).maxAge(3600);
}
}
UserController.java
上面配置完重启跨域问题解决。
但是接口文档页面打不开
原因:配置文件 图一 1和2执行顺序先后问题
方法:
先加载接口文档:
新建META-INF/spring.factories
文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.rsgk.xrhd.config.Swagger2Configuration,\
com.rsgk.xrhd.config.Cors
Swagger2Configuration 顺序放置cors前,问题解决