在前后端分离以后,在进行前后端接口对接的时候,可能会出现跨域的情况。在这里进行处理
1.可以在配置文件中对进行处理
后期更新
2.可以新建一个类处理
package com.ruhr.rail.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 处理跨域请求.
*
* @author yi wei
* @date create in 2019/1/22 15:06 *
*/
@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {
private static final int MAX_AGE = 3600;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins("*")
.exposedHeaders("ruhr-auth")
.allowedMethods("GET", "POST", "DELETE", "PUT")
.allowCredentials(false)
.maxAge(MAX_AGE);
}
}