添加如下配置类:
package com.mxc.oauth.server.uaa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName: CorsConfig
* @Description: 通用配置类
* @author mengxc
* @date 2022年02月17日 上午10:19:38
*/
@Configuration
@Slf4j
public class CorsConfig {
/**
* public void addCorsMappings(CorsRegistry registry) { <br />
* registry.addMapping("/**")<br />
* .allowedHeaders("*")<br />
* .allowedMethods("*")<br />
* .allowedOrigins("http://192.168.1.163:8890")<br />
* .allowCredentials(true);<br />
* }<br />
*/
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
log.info("**********跨域设置**********");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
/**
* 跨域过滤器
*/
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}