Spring security oauth2 Spring Oauth2-每个客户端id有多个令牌
我们已经使用spring-oauth2实现了一个服务器API。我注意到,即使从不同的设备调用,服务器也会为每个用户/客户端id组合生成相同的令牌。这导致了一个问题,因为我的客户端可以运行多个实例:例如android和ios应用程序。我需要一种方法将令牌链接到特定实例,而不是重复使用同一令牌 例如,GCM(或推送通知)需要这样做,API需要知道它正在与哪个实例通信 这是我当前的spring配置:Spring security oauth2 Spring Oauth2-每个客户端id有多个令牌,spring-security-oauth2,Spring Security Oauth2,我们已经使用spring-oauth2实现了一个服务器API。我注意到,即使从不同的设备调用,服务器也会为每个用户/客户端id组合生成相同的令牌。这导致了一个问题,因为我的客户端可以运行多个实例:例如android和ios应用程序。我需要一种方法将令牌链接到特定实例,而不是重复使用同一令牌 例如,GCM(或推送通知)需要这样做,API需要知道它正在与哪个实例通信 这是我当前的spring配置: <http pattern="/oauth/token" create-session="sta
<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,因为那样做是不切实际的。有什么想法吗?我认为您可以在请求中获取设备id并为每个id生成令牌,或者您可以获得确定调用api的设备类型(如Android、IOS)的标志,并为每个平台生成令牌。因此
DefaultAuthenticationKeyGeneration
使用客户端id
,和scope
创建一个键
,如果该键与获取令牌的请求相匹配,则提供先前生成的令牌。因此,在您的情况下,可以使用ios、android和Scope的设备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);
}
}
测验
正如您所见,我能够为同一个客户端id生成多个令牌,并且这两个令牌都经过身份验证,可以从资源服务器访问资源。一个用户可以在同一平台上拥有多个设备。在任何情况下,我们都需要能够在服务器上处理id。不确定spring会怎么做用户和访问令牌之间的关系必须是一对多,这样用户就可以拥有多个令牌,但当用户更改其密码时,您将撤销所有令牌,这样您就可以在同一平台上为单个用户生成多个令牌。您得到答案了吗?我遇到了类似的问题。看看这个。看看是否有帮助。。我能够通过使用scope解决这个问题。我贴出了答案。希望能有帮助。。
» 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%