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/**");
    }
}
相关推荐
程序员cxuan23 分钟前
这 6 个 Gemini 命令,真给我用爽了
人工智能·后端·程序员
sharemylove9 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_201010 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
码事漫谈11 小时前
AI出来四年了
后端
CoderYanger11 小时前
Java EE 进阶:14.案例综合练习-博客系统(第二弹):博客详情+令牌技术
服务器·前端·spring boot·后端·spring·java-ee·github
皮皮林55111 小时前
有哪些小众的开源项目养活了一大批人?
后端
摇滚侠11 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 20
java·笔记·python
大数据技术派12 小时前
微信小程序个人版开发上线注意事项
后端
FYKJ_201014 小时前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计
考虑考虑14 小时前
JDK27中的结构化并发
java·后端·java ee