仅在客户端上使用spring-security-oauth2-client

问题描述 投票:0回答:2

我们尝试将spring-security-oauth2-client版本5.3.0.RELEASE与客户端凭证流一起使用。因此,我们尝试仅在客户端使用它,根本不应该保护所有端点(例如执行器)。

基本上,我们像这样添加了客户端凭据:

spring:
  security:
    oauth2:
      client:
        registration:
          my-client:
            authorization-grant-type: client_credentials
            client-id: foo
            client-secret: bar
            provider: my-provider
        provider:
          my-provider:
            tokenUri: http://token.uri/

我们的拦截器外观如下:

class OAuth2AuthorizedClientInterceptor implements ClientHttpRequestInterceptor {

  OAuth2AuthorizedClientManager manager
  String clientId
  AnonymousAuthenticationToken PRINCIPAL = new AnonymousAuthenticationToken("key", "anonymous", createAuthorityList("ROLE_ANONYMOUS"))

  OAuth2AuthorizedClientInterceptor(OAuth2AuthorizedClientManager manager, String clientId) {
    this.manager = manager
    this.clientId = clientId
  }

  @Override
  ClientHttpResponse intercept(
      HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
      throws IOException {
    OAuth2AuthorizeRequest authorizedRequest = OAuth2AuthorizeRequest
        .withClientRegistrationId(clientId)
        .principal(PRINCIPAL)
        .build()
    OAuth2AuthorizedClient authorizedClient = this.manager.authorize(authorizedRequest)
    if (!authorizedClient?.accessToken?.tokenValue) {
      throw new IllegalArgumentException("No access token for client '${clientId}'")
    }
    request.headers.setBearerAuth(authorizedClient?.accessToken?.tokenValue)
    return execution.execute(request, body)
  }
}

这是一个春季启动服务,在版本spring-boot-autoconfigure中包含2.2.5.RELEASE依赖项。客户端功能运行良好,但是我们遇到了一个问题,即执行器端点不再可以自由访问,但是也可以通过oauth进行保护。

这很令人困惑,因为我们只包括了spring-security-oauth2-client依赖性,而不是资源服务器依赖性。

我们找到了this Adapter 。我们不确定这是否是在服务器端增加安全性的唯一地方,但是要禁用该安全性,我们必须添加以下配置:

@Configuration
@Order(1)
class ManagedEndpointsAuthenticationConfig extends WebSecurityConfigurerAdapter {

  private static final String NOOP_PASSWORD_PREFIX = "{noop}"

  @Autowired
  SecurityProperties properties

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/actuator/**")
        .authorizeRequests()
        .requestMatchers(EndpointRequest.to("info")).permitAll()
        .requestMatchers(EndpointRequest.to("health")).permitAll()
        .requestMatchers(EndpointRequest.to("prometheus")).permitAll()
        .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
        .and()
        .httpBasic()
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    SecurityProperties.User user = properties.getUser()
    List<String> roles = user.getRoles()
    auth.inMemoryAuthentication()
        .withUser(user.name)
        .password(NOOP_PASSWORD_PREFIX + user.password)
        .roles(StringUtils.toStringArray(roles))
  }
}

这对我们来说是一个奇怪的破解,因为我们只希望在客户端使用oauth2。因此,问题是:如何在客户端使用带有弹簧安全性的oauth2?

spring-boot spring-security oauth-2.0
2个回答
0
投票

关于:


0
投票

在Spring Boot 2迁移指南中,它建议您执行以下操作:

© www.soinside.com 2019 - 2024. All rights reserved.