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/**");
    }
}
相关推荐
ConardLi26 分钟前
前端程序员原地失业?全面实测 Gemini 3.0,附三个免费使用方法!
前端·人工智能·后端
少废话h26 分钟前
Flume Kafka源与汇的topic覆盖问题解决
java·linux·kafka·flume
激动的兔子39 分钟前
Geoserver修行记-连接瀚高数据库显示java.sql.SQLException: org.postgresql.util.PSQLException
java·geoserver·瀚高数据库
喵个咪1 小时前
Qt 优雅实现线程安全单例模式(模板化 + 自动清理)
c++·后端·qt
一 乐1 小时前
健康打卡|健康管理|基于java+vue+的学生健康打卡系统设计与实现(源码+数据库+文档)
android·java·数据库·vue.js·spring boot·微信小程序
ghie90901 小时前
使用Java实现用户的注册和登录流程
java·数据库·oracle
颜如玉1 小时前
动态拼接SQL实践备忘📝
java·sql·mybatis
952361 小时前
数据结构-堆
java·数据结构·学习·算法
by__csdn1 小时前
Spring Boot 全面解析
java·数据库·spring boot·后端·spring
她说..2 小时前
基于Redis实现的分布式唯一编号生成工具类
java·数据库·redis·分布式·springboot