可以帮忙做网站做公司,phpcms和wordpress,网上购物平台类型有哪些,怎么给网站做百度坐标定位一、背景
使用Springboot框架开发后端#xff0c;在鉴权的时候使用到了过滤器。但是在测试的过程发现#xff0c;跨域的过滤器在过滤链中出现了两次#xff0c;导致前端访问后端接口时报错#xff1a;The Access-Control-Allow-Origin headers contains multiple values,b…一、背景
使用Springboot框架开发后端在鉴权的时候使用到了过滤器。但是在测试的过程发现跨域的过滤器在过滤链中出现了两次导致前端访问后端接口时报错The Access-Control-Allow-Origin headers contains multiple values,but only one allowed.错误
在浏览器端比较正常访问接口和报错接口的headers发现报错接口出现了两次headers然后开始了debug之路发现异常接口跨域过滤器进行了重复注册最终定位到了是注解的问题。
二、不同注解的作用
1、Component注解
在Filter类使用了Component注解在不指定bean名字的前提下会默认生成一个类名首字母小写的bean name。
当在CorsFilter类上面使用Component注解默认生成bean name是corsFilter访问接口的时候会按照过滤器链按顺序进行过滤
2、WebFilter ServletComponentScan注解
在Filter类上线使用了WebFilter注解在不指定名字的情况下默认的名字是类的完全限定名也就是包名类名比如com.example.filters.ExampleFilter。
ServletComponentScan注解作用ServletComponentScan 注解告诉 Spring Boot 在启动时扫描特定的包以查找使用 WebFilter、WebServlet 和 WebListener 注解的类并将它们注册为相应的 Servlet API 组件。
3、注册过滤器的两种方法
Component注解
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;Component
public class FilterConfig {Beanpublic FilterRegistrationBeanExampleFilter exampleFilterRegistration() {FilterRegistrationBeanExampleFilter registration new FilterRegistrationBean();registration.setFilter(new ExampleFilter());registration.addUrlPatterns(/example/*);registration.setOrder(Ordered.HIGHEST_PRECEDENCE); // 设置过滤器的顺序return registration;}
}OR
WebFilter ServletComponentScan两个注解必须同时使用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;SpringBootApplication
ServletComponentScan(com.example.filters) // 指定过滤器所在的包
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}WebFilter(filterName exampleFilter, urlPatterns /example/*)
public class ExampleFilter implements Filter {// 实现过滤器的逻辑
}三、其他
定位方法查看过滤链
虽然有两个过滤器都同时使用了WebFilter注解和Component注解但由于其中一个过滤器在使用WebFilter注解的时候指定了名字和Component默认生成的名字一致所以在过滤器链中只有一个
但另一个在使用WebFilter注解时没有指定名字所以两个注解分别注册了两个不同名字的bean导致在调用对应接口时出错。