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/**");
    }
}
相关推荐
特立独行的猫A35 分钟前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
一起_java1 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
码事漫谈2 小时前
2027年火的很可能是AI工作台
后端
Flynn Liang2 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
吃我一窝jio3 小时前
Python与Go线程模型深度对比
java·开发语言·python
这个昵称不容易4 小时前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图
java·springboot·毕设
文丑颜不良啊4 小时前
LeetCode3041~3060题目解析
java·leetcode
文丑颜不良啊5 小时前
LeetCode3101~3120题目解析
java·leetcode
Resean02235 小时前
SpringMVC 6+源码分析(十一)视图渲染与异步请求(收尾篇)
java·spring·springmvc
starzy19906 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala