Spring Security6 设置免登录接口地址

  1. 在SecurityFilterChain中设置免登录接口地址。如果定义了多个SecurityFilterChain,并且前面的SecurityFilterChain里使用了anyRequest().authenticated(),后面的免登录可能会失效。
java 复制代码
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(authorize -> authorize
                .requestMatchers("/api1/**", "/api2/**").permitAll()
                .anyRequest().authenticated()
            );
        return http.build();
    }

}
  1. 使用WebSecurityCustomizer设置免登录接口地址。这里设置的免登录接口地址是优先于SecurityFilterChain进行判定的。
java 复制代码
@Configuration
public class SecurityConfiguration {
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers("/ignore1/**", "/ignore2/**");
    }
}
相关推荐
小强198810 分钟前
Java程序员必知的4种引用类型:强、软、弱、虚——彻底告别内存泄漏
后端
ch.ju11 分钟前
Java程序设计(第3版)第二章——函数的递归
java·开发语言
鱼人14 分钟前
Spring Boot启动过程中偷偷干了什么?手撕run方法源码
后端
长大198814 分钟前
MySQL + Redis + Caffeine:Java后端通用三级缓存架构实战
后端
乘风破浪酱5243615 分钟前
别再乱用Redisson分布式锁了!这可能是你见过最标准的教程(附完整代码)
后端
兔子零102421 分钟前
当 Codex 成为主力,软件工程的重心已经变了
前端·后端·架构
其实防守也摸鱼22 分钟前
ctfshow--Crypto(crypto1-14)解题步骤
java·开发语言·网络·安全·密码学·ctf·ctfshow
用户67570498850228 分钟前
别再死记硬背了!一文扒光 I/O 多路复用的底裤(Epoll/Select/Poll)
后端
牛奶32 分钟前
网关是怎么当"门卫"的?
前端·后端·负载均衡