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/**");
    }
}
相关推荐
high一马平川几秒前
# SystemUI Dagger2 精读 · 第 2 章 @Inject:1200 处构造注入,和 1 处例外
android·java·kotlin
小陈从不会敲代码2 分钟前
JavaEE面试复习(多线程-初阶)
java·面试·java-ee
BingoGo2 分钟前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
后端·php
周厉3 分钟前
酒店PMS系统设计:房型房价管理与动态价格优先级实现(Java/MySQL)
java·数据库·pms酒店系统
一碗白开水一9 分钟前
Day 5 · 弹性 | 微观
java·前端·数据库
迪飞特科技10 分钟前
大模型接入业务系统:API、本地部署、RAG、智能体四种路线怎么选
java·数据库·人工智能·知识图谱·deepseek·本地化大模型
星轨zb13 分钟前
[专题5]支付发放物品的一致性保障:事务、幂等与状态流转
数据库·后端·mongodb·支付架构学习
JaguarJack13 分钟前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
后端·php·服务端
vipxieliang24 分钟前
Go 泛型结构体与约束进阶:从自定义约束到标准库泛型 API 实战
后端·go
isfox25 分钟前
Python 文件操作:读写文件这件事,记住 with open 就够了
后端