(避坑)SpringSecurity关于使用.antMatchers放行接口不生效问题

问题

在使用SpringSecurity的时候发现放行指定接口一直没有生效,使用"/**"就可以生效的问题

关于securityConfig的配置代码

@Bean
    protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable() // 关闭csrf防护,不过使用session保存在cookie中的话还是得开启csrf防护的
                .formLogin().disable() // 禁用默认登录页
                .logout().disable() // 禁用默认登出页
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)) // STATELESS禁用session(使用token作为会话的情况)
                .exceptionHandling(exceptions -> exceptions.accessDeniedHandler(accessDeniedHandler)) // 自定义授权异常处理
                .exceptionHandling(exceptions -> exceptions.authenticationEntryPoint(authenticationEntryPoint)) // 自定义认证异常处理
                .authorizeRequests()
                .antMatchers("/auth/login").permitAll() // 放行接口
                .antMatchers("/api/admin/**", "/api/setting/**").hasAuthority("admin") // 指定“admin”权限放行
                .anyRequest().authenticated(); // 所有接口都需要验证

        // 将自定义的过滤器指定执行顺序,在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(sessionAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        // 允许跨域
        http.cors();

        return http.build();
    }

而YML文件配置中也配置了context-path路径

解决问题及原因

通过输出security的dubug信息可以发现,添加到matchers中的是“/sinderBoot/auth/login”,但是当我们请求后发现,实际上security识别的是"/auth/login"。

通过查看FilterChainProxy的源码可以看到,当servletPath为空的时候会截取contextPath再校验。

总结

当你在yml文件中配置了ContextPath的时候,security中的放行接口就不用加上contextPath路径;

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你需要在代码中定义 JwtConfigurer 类,或者使用 Spring Security 提供的 JwtConfigurer 类,具体代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtTokenProvider jwtTokenProvider; @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .addFilterBefore(new JwtTokenFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(12); } } ``` 其中,JwtTokenFilter 是实现了 JWT 鉴权的过滤器。通过在 `configure` 方法中添加 `.addFilterBefore(new JwtTokenFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)`,让该过滤器先于 Spring Security 的默认过滤器执行,以实现 JWT 鉴权。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sinder_小德

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值