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/**");
    }
}
相关推荐
用户8356290780511 分钟前
使用 Python 为 PowerPoint 演示文稿添加评论
后端·python
thefool1122662 分钟前
随机链表的复制
java
jvmind_dev6 分钟前
SWT 堆外内存泄漏排查实录:一次 PNG 保存泄漏一张图,一行 g_free 治好
java·后端
CodeStats8 分钟前
《源纹天书》第三百四十六章至第三百五十章:边界的扩张、跨宇宙通信的尝试、其他宇宙的回应、新网络的形成、宇宙网络的创始者!
java·源纹天书
南雨北斗11 分钟前
TP6 安全规范的接收post请求与安全机制分析
后端
南雨北斗12 分钟前
TP6 防范XSS攻击 响应头防御(设置内容安全策略 :CSP)
后端
简单风14 分钟前
不要再人工一行一行的 review AI 生成的代码了
后端
大勇前进15 分钟前
虚拟线程避坑全解:Thread Pinning 问题定位与 3 种主流解决方案对比
后端
嘻哈baby20 分钟前
Go 语言为什么不在语言层面保证 map 线程安全?
后端
大黄评测21 分钟前
GraalVM 原生镜像构建实战:反射配置策略与 Spring Boot AOT 编译常见坑点
后端