【问题标题】:Spring Oauth2 - multiple tokens per client idSpring Oauth2 - 每个客户端 ID 多个令牌
【发布时间】:2015-11-10 03:38:00
【问题描述】:

我们已经使用 spring-oauth2 实现了一个服务器 API。我注意到即使从不同的设备调用,服务器也会为每个用户/客户端 ID 组合生成相同的令牌。这会导致一个问题,因为我的客户可以运行多个实例:例如安卓和ios应用。我需要一种将令牌链接到特定实例而不重复使用相同令牌的方法。

需要这样做的一个示例是 GCM(或推送通知),其中 API 需要知道它正在与哪个实例进行通信。

这是我当前的 spring 配置:

<http pattern="/oauth/token" create-session="stateless"
    authentication-manager-ref="clientAuthenticationManager"
    entry-point-ref="oauthAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
    <anonymous enabled="false" />
    <http-basic entry-point-ref="oauthAuthenticationEntryPoint" />
    <!-- include this only if you need to authenticate clients via request parameters -->
    <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<oauth:authorization-server
    client-details-service-ref="mongoclientDetails" token-services-ref="tokenServices"
    user-approval-handler-ref="userApprovalHandler">
    <!-- authorization-endpoint-url="/oauth/authorize"  token-endpoint-url="/oauth/token"> -->
    <oauth:authorization-code />
    <oauth:implicit />
    <oauth:refresh-token />
    <oauth:client-credentials />
    <oauth:password />
</oauth:authorization-server>

我不想给每个客户一个不同的 id,因为那是不切实际的。有什么想法吗?

【问题讨论】:

  • 你得到答案了吗?我遇到了类似的问题。
  • 看看这个。看看有没有帮助..stackoverflow.com/questions/27020702/…
  • 我可以通过使用范围来解决这个问题。我发布了一个答案。希望它有帮助..

标签: spring-security-oauth2


【解决方案1】:

所以DefaultAuthenticationKeyGeneration 使用client_id 和scope 创建一个key,如果在获取令牌的请求中匹配,它会提供先前生成的令牌。因此,在您的情况下,您可以拥有 ios、android 和范围的设备 ID。

这是我的代码。

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

.....

@Override
public void configure(ClientDetailsServiceConfigurer clients) {
    clients.inMemory()
    .withClient("my-trusted-client-with-secret")
        .authorizedGrantTypes("client_credentials")
        .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
        //.scopes("read", "write", "trust")
        .secret("somesecret")
    .accessTokenValiditySeconds(3600);
}

}

测试

» curl -H "Accept: application/json" my-trusted-client-with-secret:somesecret@localhost:8080/auth/oauth/token -d grant_type=client_credentials -d custid=1 -d siteid=2D -d scope="y"
{"access_token":"cust:site1:2D","token_type":"bearer","expires_in":3282,"scope":"y"}%                                               

» curl -H "Accept: application/json" my-trusted-client-with-secret:somesecret@localhost:8080/auth/oauth/token -d grant_type=client_credentials -d custid=1 -d siteid=3D -d scope="z"
{"access_token":"cust:site1:3D","token_type":"bearer","expires_in":3290,"scope":"z"}%                                               

» curl -H "Authorization: Bearer cust:site:3D" http://localhost:8080/dtn-auth/home
{"error":"invalid_token","error_description":"Invalid access token: cust:site:3D"}%                                                       

» curl -H "Authorization: Bearer cust:site1:3D" http://localhost:8080/dtn-auth/home
Hello World%                                                                                                                              

» curl -H "Authorization: Bearer cust:site1:2D" http://localhost:8080/dtn-auth/home
Hello World%

如您所见,我能够为同一个 client_id 生成多个令牌,并且这两个令牌都经过身份验证以从资源服务器访问资源。

【讨论】:

    【解决方案2】:

    我认为您可以在请求中获取设备 ID 并为每个 ID 生成令牌,或者您可以获取标志来确定调用您的 api 的设备类型(如 Android、IOS)并为每个平台生成令牌。

    【讨论】:

    • 一个用户可以在同一个平台上拥有多个设备。无论如何,我们需要能够在服务器上处理 id。不知道 spring 会如何做到这一点
    • 用户和访问令牌之间的关系必须是一对多,因此用户可以拥有多个令牌,但是当用户更改密码时,您将撤销所有令牌,这样您就可以生成超过一个令牌给同一平台的单个用户。
    猜你喜欢
    • 1970-01-01
    • 2015-06-02
    • 2016-12-17
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多