【问题标题】:How to provide access to protected endpoint with provided client id and client secret?如何使用提供的客户端 ID 和客户端密码提供对受保护端点的访问?
【发布时间】:2015-01-06 12:33:58
【问题描述】:

我有一个使用 OAuth 的应用程序。 身份验证包含 2 个步骤:获取访问令牌,使用提供的访问令牌发出请求。

是否可以配置 Spring Security 以通过提供的客户端 ID 和客户端机密作为请求参数提供访问,例如 https://localhost.com/api/endpoint?client_id=xxxx&client_secret=yyyy

【问题讨论】:

  • 您提出的方案不只是 HTTP Basic 身份验证吗?为什么要使用 OAuth?
  • 它不是 HTTP Basic,但它不是任何人在实践中都会推荐的方案(URL 中的凭据)。 HTTP Basic 会更好。
  • github 有相同的选项developer.github.com/v3/#oauth2-keysecret 用于服务器到服务器的连接

标签: java spring oauth spring-security spring-security-oauth2


【解决方案1】:

答案一如既往的简单:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints
            .tokenGranter(new CompositeTokenGranter(
                            Arrays.asList(resourceOwnerPasswordTokenGranter(), clientCredentialsTokenGranter())))
            .authenticationManager(providerManager())
            .clientDetailsService(clientDetailsService())
            .tokenServices(defaultTokenServices());
}

private TokenGranter resourceOwnerPasswordTokenGranter() {
    return new ResourceOwnerPasswordTokenGranter(
            new ProviderManager(Arrays.asList(authenticationProvider())),
            defaultTokenServices(),
            clientDetailsService(),
            defaultOAuth2RequestFactory());
}

private TokenGranter clientCredentialsTokenGranter() {
     return new ClientCredentialsTokenGranter(
             defaultTokenServices(),
             clientDetailsService(),
             defaultOAuth2RequestFactory());
}

我添加了新的 CompositeTokenGranter,其中包含 2 个授予者列表:ResourceOwnerPassswordTokenGranter(用于通过 clientId+clientPassword+username+password 进行身份验证)和 ClientCredentialsTokenGranter(用于通过 clientId+clientPassword 进行身份验证)。

编辑

看来我的问题不正确。为此事道歉。我对授权服务器进行了更改。所以它只适用于 /oauth/token 。 如何解决问题我现在不知道,但我会在找到解决方案时更新这个问题。

【讨论】:

  • 一定是和我读到的不同的问题。我很高兴你很高兴(因为默认令牌授予者已经有这两个选项)。
猜你喜欢
  • 2021-08-07
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 2019-08-16
相关资源
最近更新 更多