Springboot中如何确保@WebFilter拦截器正确生效?

2026-05-16 04:051阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计314个文字,预计阅读时间需要2分钟。

Springboot中如何确保@WebFilter拦截器正确生效?

问题描述:在Spring Boot启动类上添加WebFilter以解决未生效的拦截器问题。

解决方案:在Spring Boot启动类上添加`@ServletComponentScan`注解,指定基础包路径为`full.package.path`,并将路径替换为`@WebFilter`所在的包。

问题描述

@WebFilter(filterName = “ssoFilter”,urlPatterns = “/*”)

未生效拦截器

解决方法

在springboot启动类上添加

@ServletComponentScan(basePackages = “full.package.path”)

路径替换为@WebFilter所在包

Springboot中如何确保@WebFilter拦截器正确生效?

补充知识:在spring boot中使用@WebFilter配置filter(包括排除URL)

我就废话不多说了,大家还是直接看代码吧~

@WebFilter(urlPatterns = "/*") @Order(value = 1) public class TestFilter implements Filter { private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>( Arrays.asList("/main/excludefilter", "/login", "/logout", "/register"))); @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init-----------filter"); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", ""); boolean allowedPath = ALLOWED_PATHS.contains(path); if (allowedPath) { System.out.println("这里是不需要处理的url进入的方法"); chain.doFilter(req, res); } else { System.out.println("这里是需要处理的url进入的方法"); } } @Override public void destroy() { System.out.println("destroy----------filter"); } }

@Order中的value越小,优先级越高。

以上这篇解决Springboot @WebFilter拦截器未生效问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计314个文字,预计阅读时间需要2分钟。

Springboot中如何确保@WebFilter拦截器正确生效?

问题描述:在Spring Boot启动类上添加WebFilter以解决未生效的拦截器问题。

解决方案:在Spring Boot启动类上添加`@ServletComponentScan`注解,指定基础包路径为`full.package.path`,并将路径替换为`@WebFilter`所在的包。

问题描述

@WebFilter(filterName = “ssoFilter”,urlPatterns = “/*”)

未生效拦截器

解决方法

在springboot启动类上添加

@ServletComponentScan(basePackages = “full.package.path”)

路径替换为@WebFilter所在包

Springboot中如何确保@WebFilter拦截器正确生效?

补充知识:在spring boot中使用@WebFilter配置filter(包括排除URL)

我就废话不多说了,大家还是直接看代码吧~

@WebFilter(urlPatterns = "/*") @Order(value = 1) public class TestFilter implements Filter { private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>( Arrays.asList("/main/excludefilter", "/login", "/logout", "/register"))); @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init-----------filter"); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", ""); boolean allowedPath = ALLOWED_PATHS.contains(path); if (allowedPath) { System.out.println("这里是不需要处理的url进入的方法"); chain.doFilter(req, res); } else { System.out.println("这里是需要处理的url进入的方法"); } } @Override public void destroy() { System.out.println("destroy----------filter"); } }

@Order中的value越小,优先级越高。

以上这篇解决Springboot @WebFilter拦截器未生效问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。