系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 脚本中心 > 其它 > 详细页面

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

时间:2020-10-07来源:www.pcxitongcheng.com作者:电脑系统城

问题描述

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

未生效拦截器

解决方法

在springboot启动类上添加

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

路径替换为@WebFilter所在包

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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拦截器未生效问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

分享到:

相关信息

  • 如何使用python统计字符在文件中出现的次数

    一、本项目来源: 二、先上传自己写的程序 三、解读程序语句。 四、程序运行效果 五、程序中需要注意的事...

    2023-03-09

  • Go中数组传参的几种方式小结

    本文主要介绍了Go中数组传参的几种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载