spring-boot-plus CORS跨域处理
2019-10-15 09:48
609 查看
CORS跨域处理
CORS:Cross-Origin Resource Sharing
- CORS是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。
处理方法
- 后台设置允许的请求源/请求头等信息
后台配置
CorsFilter Bean配置
使用
Spring提供的CorsFilter过滤器实现跨域配置
io.geekidea.springbootplus.core.config.SpringBootPlusCorsConfig
/** * CORS跨域设置 * * @return */ @Bean public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) { log.debug("corsProperties:{}", corsProperties); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); // 跨域配置 corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins()); corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders()); corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods()); corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials()); corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders()); corsConfiguration.setMaxAge(corsConfiguration.getMaxAge()); source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); bean.setEnabled(corsProperties.isEnable()); return bean; }
配置文件
配置文件类:
io.geekidea.springbootplus.core.properties.SpringBootPlusCorsProperties
- application.yml
spring-boot-plus: ############################ CORS start ############################ # CORS跨域配置,默认允许跨域 cors: # 是否启用跨域,默认启用 enable: true # CORS过滤的路径,默认:/** path: /** # 允许访问的源 allowed-origins: '*' # 允许访问的请求头 allowed-headers: x-requested-with,content-type,token # 是否允许发送cookie allow-credentials: true # 允许访问的请求方式 allowed-methods: OPTION,GET,POST # 允许响应的头 exposed-headers: token # 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求 max-age: 1800 ############################ CORS end ##############################
参考
相关文章推荐
- Spring boot 总结之跨域处理cors
- CORS跨域 spring boot设置
- springboot2-CORS支持(跨域)
- springBoot addCorsMappings跨域
- springboot解决跨域问题(Cors)
- Springboot(9)轻松搞定跨域访问(CORS)
- SpringBoot配置Cors解决跨域请求问题
- springBoot addCorsMappings跨域
- Spring Boot中通过CORS解决跨域问题
- SpringBoot配置Cors解决跨域请求问题
- springBoot addCorsMappings跨域
- SpringBoot(十七)_springboot跨域处理
- springboot -- 通过cors协议解决跨域问题
- springBoot addCorsMappings跨域
- springboot中通过cors协议解决跨域问题
- 第二十五章:SpringBoot添加支持CORS跨域访问
- Springboot通过cors解决跨域问题(解决spring security oath2的/oauth/token跨域问题)
- Spring Boot使用CORS解决跨域问题
- springBoot addCorsMappings跨域
- springBoot addCorsMappings跨域