Spring Boot配置统一跨域过滤器
2020-01-11 21:12
507 查看
重写WebMvcConfigurer的addCorsMappings()可以解决一部分跨域的问题,但是对于有些过滤器涉及到跨域,且拦截器位面较高的话,还是会出现一些跨域问题。
代码:
//配置CorsFilter跨域过滤器CorsFilterRegistrationConfig
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import java.util.ArrayList; import java.util.List; /** * 跨域过滤器 * */ @Component @Configuration @ConfigurationProperties(prefix = CorsFilterRegistrationConfig.PREFIX) public class CorsFilterRegistrationConfig { public static final String PREFIX = "fss.filter.cors"; /** * 允许跨域地址 * 允许所有:* */ private List<String> allowedOriginList = new ArrayList<>(); /** * 允许请求头信息 * 允许所有:* */ private List<String> allowedHeaderList = new ArrayList<>(); /** * 允许请求信息 * 允许所有:* */ private List<String> allowedMethodList = new ArrayList<>(); /** * 允许暴露信息 */ private List<String> exposedHeaderList = new ArrayList<>(); /** * 允许证书 */ private Boolean allowCredentials = true; /** * 缓存时间 */ private Long maxAge = 3600L; /** * 过滤地址 */ private String mapping = ""; /** * 跨域过滤器 * * @return */ @Bean public FilterRegistrationBean CrosFilterRegistrationBean() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowedOrigins(allowedOriginList); corsConfiguration.setAllowedHeaders(allowedHeaderList); corsConfiguration.setAllowedMethods(allowedMethodList); corsConfiguration.setExposedHeaders(exposedHeaderList); corsConfiguration.setMaxAge(maxAge); corsConfiguration.setAllowCredentials(allowCredentials); UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource(); configurationSource.registerCorsConfiguration(mapping, corsConfiguration); CorsFilter corsFilter = new CorsFilter(configurationSource); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(corsFilter); filterRegistrationBean.setOrder(0); return filterRegistrationBean; } public List<String> getAllowedOriginList() { return allowedOriginList; } public void setAllowedOriginList(List<String> allowedOriginList) { this.allowedOriginList = allowedOriginList; } public List<String> getAllowedHeaderList() { return allowedHeaderList; } public void setAllowedHeaderList(List<String> allowedHeaderList) { this.allowedHeaderList = allowedHeaderList; } public List<String> getAllowedMethodList() { return allowedMethodList; } public void setAllowedMethodList(List<String> allowedMethodList) { this.allowedMethodList = allowedMethodList; } public List<String> getExposedHeaderList() { return exposedHeaderList; } public void setExposedHeaderList(List<String> exposedHeaderList) { this.exposedHeaderList = exposedHeaderList; } public Boolean getAllowCredentials() { return allowCredentials; } public void setAllowCredentials(Boolean allowCredentials) { this.allowCredentials = allowCredentials; } public Long getMaxAge() { return maxAge; } public void setMaxAge(Long maxAge) { this.maxAge = maxAge; } public String getMapping() { return mapping; } public void setMapping(String mapping) { this.mapping = mapping; } }
//在application.yml文件中配置跨域属性
fss: filter: cors: allowed-origin-list: - '*' allowed-header-list: - '*' allowed-method-list: - POST - GET exposed-header-list: - access-control-allow-headers - access-control-allow-methods - access-control-allow-origin - access-control-max-age - X-Frame-Options mapping:/**
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- springboot 跨域过滤器配置
- springboot配置跨域请求(CorsFilter)
- SpringBoot下如何配置实现跨域请求?
- 使用 vue + springboot 前后端分离 跨域、cookie、session 问题,全套配置一篇就够了
- 在springboot项目中配置跨域
- 使用mpvue+springboot开发微信小程序 二 后台服务跨域访问配置filter
- SpringBoot下如何配置实现跨域请求?
- 基于spring boot 的前后端分离跨域问题在后台的配置
- SpringBoot下如何配置实现跨域请求?
- springboot中 后端跨域的实现配置
- springboot 上传文件 中过滤器配置*.do后缀 无法上传的问题
- SpringBoot配置CORS跨域访问
- SpringBoot下如何配置实现跨域请求?
- springboot 配置资源统一管理
- SpringBoot 与 Nginx跨域配置
- SpringBoot下如何配置实现跨域请求?
- Springboot 跨域配置
- SpringBoot下如何配置实现跨域请求?
- springboot中用注解配置过滤器并配置多个过滤路径
- SpringBoot下如何配置实现跨域请求?