使用 spring security 内省不透明令牌以实现安全日志记录

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

我们有一个在 Ping 和 Kong 后面受到保护的 API,但我想在 API 中使用不透明令牌自省从传入令牌中获取客户端 ID 以用于安全日志记录。

到目前为止,我已经添加了

org.springframework.boot:spring-boot-starter-oauth2-resource-server
com.nimbusds:oauth2-oidc-sdk:5.17.1
依赖项,并具有以下配置:

spring:
  security:
    oauth2:
      resourceserver:
        opaque-token:
          introspection-uri: https://internal.authserver.com/as/introspect.oauth2
          client-id: client-id
          client-secret: client-secret

因为我不想对传入请求运行身份验证:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

但是我很难弄清楚如何在过滤器链之外调用内省端点。对于上下文,我确实使用自定义 http 客户端完成了相同的功能,如下所示:

public PingResponse getClientIdFromPing(String clientKey) {
        final String oath2Url = "https://internal.authserver.com/as/introspect.oauth2";
        final String oath2Username = "client-id";
        final String oath2Password = "client-secret";

        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("token", clientKey);

        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(oath2Username, oath2Password);
        headers.add("Content-type","application/x-www-form-urlencoded");

        return restTemplate.postForEntity(
                URI.create(oath2Url),
                new HttpEntity<>(requestBody, headers),
                PingResponse.class
        ).getBody();
    }

如果可能的话,我更愿意使用 Spring Security,有没有办法做到以上几点?或者,因为我实际上并没有保护我的 api(因为它是内部的并且已经被 ping/kong 保护),Spring Security 是这个用例的错误选择吗?我对 Java/Spring 有点陌生,一直在研究块帖子和其他 SO 问题,但还没有找到解决方案。

java spring spring-boot spring-security spring-security-oauth2
© www.soinside.com 2019 - 2024. All rights reserved.