zoukankan      html  css  js  c++  java
  • spring security permitAll不生效

    0 环境

    • 系统:win10
    • 编辑器:IDEA

    1 问题描述

    1 部分代码

    securityConfig http

    			http
    			     // 拦截请求,创建了FilterSecurityInterceptor拦截器
                    .authorizeRequests()
                    // 允许
                    //            "/login",
                    //            "/logout",
                    //            "/captcha",
                    //            "/favicon.ico"
                    .antMatchers(URL_WHITELIST).permitAll()
                    .anyRequest().authenticated()
                    .and()
                    // 在用户密码认证前 认证验证码是否正确
                    //.addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class)
                    // token验证 继承BasicAuthenticationFilter
                    .addFilter(jwtAuthenticationFilter());
    

    当我进入登陆页 获取验证码 http://127.0.0.1:8081/captcha 不需要用户认证直接放行 但现在不生效 提示token异常
    继承BasicAuthenticationFilter类目的 每次访问业务都需要携带token 错误提示token异常 过期等

    2 解决

    参考网址

    1 原理

    FilterSecurityInterceptor拦截器排在最后 那么authorizeRequests在BasicAuthenticationFilter之后执行

    基本原理

    2 验证启动顺序

    继承BasicAuthenticationFilter类 JwtAuthenticationFilter ⇒ 验证token(请求头Authorization)
    AuthController ==> 获取验证码captcha
    也就是说JwtAuthenticationFilter 这里遇到前面的url直接放行或者不在过滤器链中 直接忽略

    执行顺序
    1 在继承BasicAuthenticationFilter类中 url直接放行

    public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
    	.....
    	 // 假如不需要token的判断 根据自己的需要编写
         if(request.getServletPath().equals("/captcha")){
             chain.doFilter(request, response);
             return;
         }
         ....
    }
    

    2 不在过滤器链中 直接忽略

    	@Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("...","..."....需要过滤的url);
        }
    

    3 debug

    我需要判断token为空的判断 在放行 看代码没毛病 没找到问题 但接口测试工具(非postman) 测试始终过不去 后来debug发现tokenBasic Og== 强烈建议不要用某些接口测试工具(xxxpost) 大坑

    3 小结

    当我们使用了permitAll时 记住authorizeRequestsBasicAuthenticationFilter的后面(告诉面试官 我的某某学妹毕业了想来我公司实习 挺优秀的 麻烦通融通融放行白 面试官问 具体信息 就这样愉快的进来了) 或者直接忽略web.ignoring().antMatchers(...) 我跳出五行 不在过滤器链中混了
    慎用某些接口测试工具

    作者:以罗伊
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    存储过程
    数据库中的锁
    数据库事务
    三大范式
    IOC(一)
    rabbitmq部署
    配置SQLServer2012,允许远程连接
    6种常见的Git错误以及解决的办法
    灵活使用Win+R快捷键提高工作效率
    sql 创建视图常用的几种sql函数
  • 原文地址:https://www.cnblogs.com/my-ordinary/p/14934516.html
Copyright © 2011-2022 走看看