小编Ano*_*zzz的帖子

Spring Boot 2:如果附加了授权标头,基本 Http Auth 会导致未受保护的端点响应 401“未经授权”

迁移到 Spring Boot 2 并添加执行器和另一个应用程序控制端点的基本授权要求后,不可能使用授权标头调用任何不受保护的端点。

配置片段:

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .requestMatchers(EndpointRequest.to("shutdown")).fullyAuthenticated()
            .antMatchers("/payment/status/*").fullyAuthenticated()
            .and().httpBasic();
}
Run Code Online (Sandbox Code Playgroud)

例如,使用“授权:基本...”调用 .../health 将导致 401“未经授权”,即使它不受 Spring Security 保护。

问题:如何调整配置,以便可以将带有授权标头的请求发送到任何不受保护的端点而不被拒绝?

UPD:此修复按我想要的方式工作

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .requestMatchers(EndpointRequest.to("shutdown")).fullyAuthenticated()
            .antMatchers("/payment/status/*").fullyAuthenticated()
            .antMatchers("/payment/**").permitAll()
            .and().httpBasic();
}
Run Code Online (Sandbox Code Playgroud)

UPD2:没关系,刚刚测试了另一个请求,仍然收到 401“未经授权”。

curl localhost:8080/payment/<any_endpoint> -H "Authorization: Basic asdadas"
{"code":401,"message":"Unauthorized"}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这种方法会覆盖 HttpSecurity 匹配器,例如:/ payment/ 变得可访问

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .requestMatchers(EndpointRequest.to("shutdown")).fullyAuthenticated()
            .antMatchers("/payment/status/*").fullyAuthenticated()
            .and().httpBasic();
}

@Override
public void configure(WebSecurity webSecurity) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

5
推荐指数
1
解决办法
3055
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-security ×1