【发布时间】:2017-08-04 04:43:44
【问题描述】:
我有一个 oauth2 的工作示例,并为客户端和资源所有者提供内存身份验证和授权。
我想使用 Redis,但对如何设置它有点困惑。
如何调整下面的代码以便能够将数据持久保存在 Redis 中(例如令牌、refresh_token 和其他客户端详细信息)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Value("${spring.oauth2.realm.id}")
private String REALM;
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client001")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_ADMIN", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.secret("secret")
.accessTokenValiditySeconds(120)
.refreshTokenValiditySeconds(600);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.realm(REALM + "/client");
}
}
[更新]
我能够通过使用 RedisTokenStore 将令牌存储到 Redis。为此,请在您的 OAuth2SecurityConfiguration 中替换以下行:
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
与...
@Bean
public TokenStore tokenStore(final RedisConnectionFactory factory) {
return new RedisTokenStore(factory);
}
现在遇到了一个新问题,在 AuthorizationServerConfiguration#configure(final ClientDetailsServiceConfigurer clients);。是否支持 redis?
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client001")
.secret("secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_ADMIN", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.accessTokenValiditySeconds(60)
.refreshTokenValiditySeconds(120);
}
当我查看 ClientDetailsServiceConfigurer 时,它只支持 inMemory() 和 jdbc()。我打算使用 withClientDetails() 来支持 redis。是否有 RedisClientDetailsService 或类似的东西?
非常感谢任何关于其实施的 cmets 和建议。
【问题讨论】:
标签: spring redis oauth-2.0 spring-oauth2