跨域处理
/**
* HTTP配置类
*/
@Configuration
public class HttpConfig implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "content-type, Accept, X-Requested-With ");
response.setHeader("Access-Control-Allow-Credentials","true"); //是否支持cookie跨域
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
/**
* 配置
* @author
*/
@Configuration
public class CORSConfiguration {
/**
* @return {@link WebMvcConfigurer} 返回对象
* @方法描述:配置
*/
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
.allowCredentials(false).maxAge(3600);
}
};
}
}