解决Springboot @WebFilter拦截器未生效问题

网友投稿 729 2022-11-18


解决Springboot @WebFilter拦截器未生效问题

问题描述

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

未生效拦截器

解决方法

在springboot启动类上添加

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

路径替换为@WebFilter所在包

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

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

@WebFilter(urlPatterns = "/*")

@Order(value = 1)

public class TestFilter implements Filter {

private static final Set 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 {

HttpServhttp://letRequest 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);

}

http:// else {

System.out.println("这里是需要处理的url进入的方法");

}

}

@Ovhttp://erride

public void destroy() {

System.out.println("destroy----------filter");

}

}

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot服务开启后通过端口访问无反应的解决
下一篇:实战讲解Maven安装及基本使用详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~