springboot 设置CorsFilter跨域不生效的解决_第1页
springboot 设置CorsFilter跨域不生效的解决_第2页
springboot 设置CorsFilter跨域不生效的解决_第3页
springboot 设置CorsFilter跨域不生效的解决_第4页
springboot 设置CorsFilter跨域不生效的解决_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第springboot设置CorsFilter跨域不生效的解决公司的前后端开发项目工程,在本地调试的时候遇到了跨域的问题,同事调我的服务一直提示跨域问题,然后前端nb他自己在哪里做了跨域处理,类似nginx那种,但是我还是百度去看了一下,在一个大佬的博客中发现了解决方案。

问题原因是是写的判断登录的filter影响了登录,原因是的这个filter执行顺序在corsfilter之前导致,于是修改了一下跨域设置的配置文件

*使用CORS,用于解决ajax跨域访问问题

@Configuration

publicclassGlobalCorsConfig{

@Bean

publicFilterRegistrationBeancorsFilter(){

//1.添加CORS配置信息

CorsConfigurationconfig=newCorsConfiguration();

//1)允许的域,不要写*,否则cookie就无法使用了

//config.addAllowedOrigin(

//config.addAllowedOrigin(

config.addAllowedOrigin(*

//2)是否发送Cookie信息

config.setAllowCredentials(true);

//3)允许的请求方式

config.addAllowedMethod(OPTIONS

config.addAllowedMethod(HEAD

config.addAllowedMethod(GET

config.addAllowedMethod(PUT

config.addAllowedMethod(POST

config.addAllowedMethod(DELETE

config.addAllowedMethod(PATCH

config.setMaxAge(3600L);

//4)允许的头信息

config.addAllowedHeader(*

//2.添加映射路径,我们拦截一切请求

UrlBasedCorsConfigurationSourceconfigSource=newUrlBasedCorsConfigurationSource();

configSource.registerCorsConfiguration(/**,config);

//3.返回新的CorsFilter.

//returnnewCorsFilter(configSource);

FilterRegistrationBeanbean=newFilterRegistrationBean(newCorsFilter(configSource));

bean.setOrder(0);

returnbean;

}

跨域配置CorsFilter不生效原因

项目中有多个Filter时,需要通过@Order(Ordered.HIGHEST_PRECEDENCE)注解设置过滤器的执行顺序

order的规则

1.order的值越小,优先级越高

2.order如果不标注数字,默认最低优先级,因为其默认值是int最大值

3.该注解等同于实现Ordered接口getOrder方法,并返回数字。

如果使用如下注释掉的方法进行设置跨域,Filter的doFilter()方法中直接return出去时,前端会提示跨域

因为这个CorsConfig并没有实现Filter接口,即使加上@Order注解也不会生效,需要通过如下新的方式返回一个新的FilterRegistrationBean出去,并设置order

importcom.nanase.takeshi.constants.JwtConstant;

importlombok.extern.slf4j.Slf4j;

importorg.springframework.boot.web.servlet.FilterRegistrationBean;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.core.Ordered;

importorg.springframework.web.cors.CorsConfiguration;

importorg.springframework.web.cors.UrlBasedCorsConfigurationSource;

importorg.springframework.web.filter.CorsFilter;

*CorsConfig

*跨域请求配置

*@author725

*@date2025/12/1018:17

@Slf4j

@Configuration

publicclassCorsConfig{

privateCorsConfigurationbuildConfig(){

CorsConfigurationcorsConfiguration=newCorsConfiguration();

//1设置访问源地址

corsConfiguration.addAllowedOrigin(*

//2设置访问源请求头

corsConfiguration.addAllowedHeader(*

//3设置访问源请求方法

corsConfiguration.addAllowedMethod(*

//4暴露哪些头部信息

corsConfiguration.addExposedHeader(JwtConstant.HEADER);

returncorsConfiguration;

@Bean

publicCorsFiltercorsFilter(){

(跨域设置。。。。

UrlBasedCorsConfigurationSourcesource=newUrlBasedCorsConfigurationSource();

//对接口配置跨域设置

source.registerCorsConfiguration(/**,buildConfig());

returnnewCorsFilter(source);

@Bean

publicFilterRegistrationBeanCorsFiltercorsFilter(){

(跨域设置。。。。

UrlBasedCorsConfigurationSourcesource=newUrlBasedCorsConfigurationSource();

//5对接口配置跨域设置

source.registerCorsConfiguration(/**,buildConfig());

//有多个filter时此处设置改CorsFilter的优先执行顺序

FilterRegistrationBe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论