关于springboot security自定义拦截器 使用 permitAll() 之后仍然会走过滤器的解决方法

前言

依然是我负责的认证模块,出现了业务上的问题,想要使某些接口不走过滤链,但是再security上配置

.antMatchers("/**").permitAll()

之后,前端访问进来,依然会走我的 token 过滤器

public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
	...
}

解决方法一

尝试过,在获取到token为空的时候,判断一下请求地址是不是某些不需要token的接口,例如 短信验证码接口

// 如果请求头中没有token信息则直接放行了
        if (null == authorization  || ("").equals(authorization) 
        || ("null").equals(authorization)|| userType == null) {
            if(request.getServletPath().equals("/login/verificationCode")){
                chain.doFilter(request, response);
                return;
            }
            this.responseMsg(RestResult.failure(ErrorCode.SYS_ERROR),response);
            return;
        }

这样子做看起来是可以解决问题,但是也引起了其他问题,例如后期的扩展太麻烦,我需要不断的往if里面塞东西,就觉得很傻*,于是有了第二种想法。

解决方法二

配置某些接口直接不进入过滤链,在security的config中配置

@Override
    public void configure(WebSecurity web) {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/login/verificationCode","/images/**");
    }

如此,比上一个方法好了不止一点点。

希望可以帮助到大家。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
在Spring Security中,可以使用Filter来实现自定义的登录认证拦截器。具体步骤如下: 1. 创建一个实现了javax.servlet.Filter接口的拦截器类,例如:CustomAuthenticationFilter。 ```java public class CustomAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // 获取请求中的用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 自定义认证逻辑 if ("admin".equals(username) && "admin".equals(password)) { // 认证通过 filterChain.doFilter(request, response); } else { // 认证失败 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } } ``` 2. 在Security配置类中配置该拦截器。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.addFilterBefore(new CustomAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } } ``` 在上面的配置中,我们将自定义拦截器添加到了UsernamePasswordAuthenticationFilter之前,这样就可以在登录认证之前进行自定义认证逻辑的处理。 3. 在登录页面中提交用户名和密码的表单。 ```html <form action="/login" method="post"> <div> <label>用户名:</label> <input type="text" name="username"/> </div> <div> <label>密码:</label> <input type="password" name="password"/> </div> <div> <input type="submit" value="登录"/> </div> </form> ``` 当用户提交表单时,自定义拦截器拦截请求并进行自定义认证逻辑的处理,如果认证通过,则继续执行后续的请求处理流程,否则返回认证失败的状态码。 以上就是使用Filter实现登录认证的步骤。需要注意的是,这种方式只适用于简单的认证场景,对于更为复杂的认证需求,建议使用Spring Security提供的AuthenticationProvider进行认证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值