The first step: add the gateway gateway configuration file following these:
ly:
cors:
allowedOrigins:
- http://manage.leyou.com
- http://xxx.xxx.com
# Permit which URLs will continue to increase, do not write *, otherwise the cookie will not be used
allowedCredentials: true # 代表携带cookie
allowedHeaders:
- "*"
allowedMethods:
- GET
- POST
- DELETE
- PUT
- OPTIONS
- HEAD
maxAge: 360000
filterPath: "/**"
Step Two: Write a class to resolve the above configuration profile information
@Data
@ConfigurationProperties(prefix = "ly.cors")
public class CORSProperties {
private List<String> allowedOrigins;
private Boolean allowedCredentials;
private List<String> allowedMethods;
private List<String> allowedHeaders;
private Long maxAge;
private String filterPath;
}
The third step: Write a cross-domain filter
@ The Configuration @EnableConfigurationProperties (CORSProperties. Class )
public class GlobalCORSConfig {
@Autowired
Private CORSProperties prop ;
/ **
* @Bean annotations, the method returns the current value of the object into the IOC container
* @return
* /
@Bean
public CorsFilter corsFilter () {
// add 1 CORS configuration
CorsConfiguration config = new new CorsConfiguration (); prop .getAllowedOrigins () forEach (config :: addAllowedOrigin);. // above and below the same effect as writing // for (String allowedOrigin: prop.getAllowedOrigins ()) {
Config.addAllowedOrigin // (allowedOrigin);
//}
// 2) whether to transmit the Cookie information
config.setAllowCredentials ( prop .getAllowedCredentials ());
//. 3) allows the request method
prop .getAllowedMethods () forEach (config ::. addAllowedMethod);
//. 4) of header information to allow
prop .getAllowedHeaders () forEach (config :: addAllowedHeader);.
//. 5) Validity
config.setMaxAge ( prop .getMaxAge ());
. 2 // add map path, we intercept all requests
UrlBasedCorsConfigurationSource the configSource = new new UrlBasedCorsConfigurationSource ();
configSource.registerCorsConfiguration ( prop .getFilterPath (), config);
.. 3 // returns the newCORSFilter.
return new CorsFilter(configSource);
}
}