首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Oauth2 -每个客户端id多个令牌

Spring Oauth2 -每个客户端id多个令牌
EN

Stack Overflow用户
提问于 2015-08-17 21:15:59
回答 2查看 6.1K关注 0票数 7

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

例如,对于GCM (或推送通知),API需要知道它正在与哪个实例通信。

这是我当前的spring配置:

代码语言:javascript
复制
<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,因为那是不切实际的。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2015-10-02 03:43:40

因此,DefaultAuthenticationKeyGeneration使用client_idscope来创建一个key,如果与获取令牌的请求中的令牌匹配,它将提供先前生成的令牌。因此,在您的示例中,您可以使用ios、android和作用域的设备id。

这是我的代码。

代码语言:javascript
复制
@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);
}

}

测试

代码语言:javascript
复制
» 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生成多个令牌,并且这两个令牌都经过身份验证,可以从资源服务器访问资源。

票数 5
EN

Stack Overflow用户

发布于 2015-09-26 13:00:55

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32051426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档