spring-security-oauth2 - Spring Oauth2 - 每个客户端 ID 有多个 token

标签 spring-security-oauth2

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

一个需要这样做的示例是 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,因为这不切实际。有任何想法吗?

最佳答案

所以DefaultAuthenticationKeyGeneration用途 client_id , 和 scope创建一个 key如果在获取 token 的请求中匹配,它会提供先前生成的 token 。因此,在您的情况下,您可以拥有 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 生成多个 token ,并且这两个 token 都经过身份验证以从资源服务器访问资源。

关于spring-security-oauth2 - Spring Oauth2 - 每个客户端 ID 有多个 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051426/

相关文章:

java - 序列化问题: org. springframework.dao.support.PersistenceExceptionTranslationInterceptor

java - 无法在Spring Boot中初始化Bean

java - Spring Oauth2 重定向 uri 没有改变

spring-boot - 为 facebook 实现PrincipalExtractor(带社交的 Spring Boot)

java - Spring Security OAuth2 改变JSON错误响应格式

java - Spring security oauth2 修改响应主体连接

java - 如何使用 Spring "matches"方法检查旧密码?

java - 如何使用经典身份验证将 spring-security-oauth 服务器添加到现有 Web 应用程序?

Spring Security OAuth2 check_token 端点

spring-mvc - 如何从 Swagger UI 隐藏 Spring Security OAuth2 端点?