java - SecurityConfig [Spring boot version 2.5.x]:为 ServerHttpSecurity 添加过滤器,仅用于安全 url
问题描述
我正在尝试为 ServerHttpSecurity 添加过滤器,并且我想跳过白名单中的 URL 过滤器,但过滤器适用于所有内容,如何避免这种情况
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
private static final String[] AUTH_WHITELIST = {
...
};
@Autowired
private JsonWebTokenParser<Claims> jwtParser;
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
AuthenticationFilter authenticationFilter = new AuthenticationFilter(jwtParser);
http.authorizeExchange().pathMatchers(AUTH_WHITELIST).permitAll().anyExchange().authenticated()
.and().cors().and().csrf().disable()
.addFilterAfter(authenticationFilter, SecurityWebFiltersOrder.AUTHORIZATION);
return http.build();
}
}
解决方案
我猜你的意思是你不想通过过滤器来列入白名单的路径?
制作 2 个 Bean,其中一个处理您的白名单,另一个处理安全,并添加一个 securityMatcher。
Kotlin 示例,但同样适用于 Java
@Bean
@Order(1)
fun whitelistWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http
.securityMatcher(pathMatchers(AUTH_WHITELIST))
.authorizeExchange()
.pathMatchers(AUTH_WHITELIST).permitAll()
.and().build()
}
@Bean
@Order(2)
fun otherWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http
.securityMatcher(pathMatchers(/*set whatever you need here*/))
.authorizeExchange()
.pathMatchers(/*set whatever you need here (same as above)*/).authenticated()
.and().cors().and().csrf().disable()
.addFilterAfter(authenticationFilter, SecurityWebFiltersOrder.AUTHORIZATION)
.build()
}
推荐阅读
- flutter - 在非相邻选项卡上使用 TabBar 进行导航时的奇怪行为
- python - Python:OSError:[Errno 9] 尝试写入 csv 时出现错误的文件描述符
- python - 如何检查 RGB 颜色列表中的所有项目是否在没有循环的图像中?
- verilog - 划分一个verilog genvar
- sql - postgreSQL 反转 REGEXP_SPLIT 到表
- python - 比较来自两个数据集的两组属性并使用匹配更新第三个数据集
- rust - 如何消除泛型特征方法调用的歧义?
- c++ - 在构造函数c ++中初始化属性时出现问题
- c# - 在 transform.position 更改后,Unity 播放器/精灵变得不可见。如何防止玩家消失?
- python - 将临时文件永久保存,以便应用程序以后可以获取它