需要在启动类/配置类 使用 @ServletComponentScan 默认扫描启动类所在的包以及子包
@ServletComponentScan 的作用:
@SpringBootApplication
@ServletComponentScan//扫描 webfilter 注解 自动注册
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@WebFilter(urlPatterns = "/*")
public class Crossfilter implements Filter {
@Autowired
private RedisService<String> redisService;
@Override
public void init(FilterConfig filterConfig) {
if (redisService == null) {
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
redisService = factory.getBean(RedisService.class);
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
request.setCharacterEncoding("UTF-8");
HttpServletResponse response = (HttpServletResponse) servletResponse;
//这里填写你允许进行跨域的主机ip(正式上线时可以动态配置具体允许的域名和IP)
//response.setHeader("Access-Control-Allow-Origin", passip);
//Access-Control-Max-Age 用于 CORS 相关配置的缓存
response.setContentType("html/text;charset=UTF-8");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,authorization,Content-Type,farm,store");
response.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACES");
response.setHeader("Access-Control-Allow-Credentials", "true");
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
/**
* 跨域请求处理 filter
*/
@Component
@Order(value = Ordered.HIGHEST_PRECEDENCE)//这是跨域优先级
public class Crossfilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
request.setCharacterEncoding("UTF-8");
HttpServletResponse response = (HttpServletResponse) servletResponse;
//这里填写你允许进行跨域的主机ip(正式上线时可以动态配置具体允许的域名和IP)
//response.setHeader("Access-Control-Allow-Origin", passip);
//Access-Control-Max-Age 用于 CORS 相关配置的缓存
response.setContentType("html/text;charset=UTF-8");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,authorization,Content-Type,farm,store");
response.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACES");
response.setHeader("Access-Control-Allow-Credentials", "true");
filterChain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
使用 @Bean 的方式创建Filter
推荐理由: 更加有效的管理filter 既能控制执行顺序 又能控制 过滤请求路径
/**
* Filter 配置类
*
* @Author su
* @Date 2022/6/23 9:41
*/
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new Crossfilter());
filterFilterRegistrationBean.setOrder(1);//执行的顺序
filterFilterRegistrationBean.addUrlPatterns("/*");
return filterFilterRegistrationBean;
}
}
需要控制Filter 执行顺序的 点击跳转 https://blog.csdn.net/qq825478739/article/details/125350219
Filter 顺序控制跳转