当前位置:   article > 正文

springboot整合security+jwt中当SecurityConfig使用addFilterBefore加入jwtAuthentication...后antMatchers失效_security重写了shouldnotfilte

security重写了shouldnotfilte

问题

我在学习springboot整合security+jwt中当SecurityConfig使用addFilterBefore加入jwtAuthenticationTokenFilter后antMatchers失效了,也不知道是什么原因,查阅资料后,解决了此问题

步骤

1.application.yml文件中加入自定义过滤路径

filter:
  path:
    includes: /fuck,/test,/mm
  • 1
  • 2
  • 3

2.在SecurityConfig中引入配置,并在原http位置配置

@Value("${filter.path.includes}")
    private String[] paths;

 .antMatchers(paths).anonymous()
  • 1
  • 2
  • 3
  • 4

3.在引入的JwtAuthenticationTokenFilter中引入配置,并在shouldNotFilter添加忽略路径

 @Value("${filter.path.includes}")
    private String[] paths;


    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return addIgnorePaths(paths, request);
    }

    protected Boolean addIgnorePaths(String[] paths, HttpServletRequest request) {
        Boolean result = false;
        if (paths != null && paths.length > 0) {
            for (String path : paths) {
                if (result || new AntPathMatcher().match(path, request.getServletPath())) {
                    return true;
                }
            }
        }
        return false;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

测试

在这里插入图片描述
至此,可添加忽略过滤路径。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/921898
推荐阅读
相关标签
  

闽ICP备14008679号