【问题标题】:Spring-Boot REST service basic http auth exclude one endpointSpring-Boot REST 服务基本 http 身份验证排除一个端点
【发布时间】:2018-01-04 14:35:25
【问题描述】:

我有一个基于 Spring-Boot 版本 1.5.4.RELEASE 和 spring-boot-starter-security 构建的纯 REST 微服务。该服务没有网页,只有 JSON 输入和输出。用户名和密码在 application.properties 文件中配置。归功于http://ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/,以下配置使服务器很好地实现了基本的HTTP身份验证,它接受凭据并拒绝未经授权的请求:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests() //
                .anyRequest().authenticated().and().httpBasic();        
    }
}

我的问题是,我想从基本 HTTP 身份验证中排除一个小 ole 路径、一个小端点。从疯狂的谷歌搜索和复制粘贴中,我修改了上面的内容:

    http.csrf().disable().authorizeRequests() //
        .antMatchers("/healthcheck").permitAll()
        .anyRequest().authenticated().and().httpBasic();

这会编译并运行而不会发出警告,但不会打开该路径以进行未经身份验证的访问。我仍然必须提供凭据来检查服务运行状况。

我必须一一匹配路径吗?我的小型运行状况检查端点位于上下文路径的基础上,一大堆其他端点也是如此 - 一个接一个地添加路径会很麻烦。

我的 application.properties 文件的相关部分是:

security.user.name = web-user
security.user.password = web-pass
management.security.roles=SUPERUSER

也许我需要以某种方式摆弄角色?

请帮忙,提前谢谢。

更新 1:

路径信息 - 我希望保护这条路径(以及更多根路径):

localhost:8081/abcd/user

我只想打开这条路径,不需要身份验证:

localhost:8081/abcd/healthcheck

更新 2:看起来我在很大程度上重复了这个 3 年前的问题,但那里没有接受我的问题的答案:

spring-boot setup basic auth on a single web app path?

【问题讨论】:

标签: java rest spring-boot


【解决方案1】:

我在 Springboot 服务的 SecurityConfig 中添加了以下内容,它工作正常,我能够从基本身份验证中排除一些端点。

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/customers/**/hints", "/customers/**/search", "/customers/**/clientConfig");
    }

【讨论】:

  • 这段代码 sn-p 没有显示对方法 authorizeRequests() 的调用。在您的服务中,是否有任何端点需要授权?
  • 我确实在所有端点上配置了基本身份验证,并通过以下代码大摇大摆 @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http.httpBasic() // 表示需要基本认证 .and() .authorizeRequests() .antMatchers("/swagger-ui.html**").permitAll() .antMatchers("/**").authenticated (); }
【解决方案2】:

经过更多实验后,我发现以下工作 - @efekctive 请注意,健康检查上没有上下文前缀:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
     .antMatchers("/healthcheck").permitAll()
     .antMatchers("/**").authenticated().and().httpBasic();
}

一个重要警告:当我发布此消息时,我正在通过使用 BOGUS http 凭据调用 curl 来测试运行状况检查端点,期望 Spring 忽略它们,但 Spring 总是回答 401-unauthorized。正确的测试是调用 curl 时根本没有 http 凭据,然后运行状况检查端点会非常高兴地回答。

【讨论】:

  • 试过这个,对我不起作用。仅供参考,我使用的是 sessionCreationPolicy 而不是 httpBasic
【解决方案3】:

你说你的链接看起来像:

localhost:8081/abcd/healthcheck

试试这个:

http.csrf().disable().authorizeRequests() //
    .antMatchers("/abcd/healthcheck").permitAll()
    .anyRequest().authenticated().and().httpBasic();

【讨论】:

猜你喜欢
  • 2019-07-30
  • 2011-04-21
  • 2011-06-10
  • 2011-11-08
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
相关资源
最近更新 更多