【发布时间】:2017-07-15 21:33:15
【问题描述】:
这是我第一次使用 OAuth2 方法开发应用程序。我是根据某些教程开始的,我正在从这个(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)继续前进。
我会将应用程序部署到集群的 WebSphere,因此,据我所知,内存中不起作用(...clients.inMemory().withClient ...)。
我想使用 Redis(我也是第一次使用),但我有点困惑如何在某些无 xml java config 应用程序中设置它。
我在 xml 中发现了某些类似的问题,但我仍然没有第一次尝试 (Redis Token Store)。有趣的是,在这里,问题所有者谈到了“Spring-Security OAuth,即 2.8.0 提供 RedisTokenStore”,但我发现“2.0.12.RELEASE”是最新的 mvn 发布版本。
也就是说,我的直截了当的问题是:如何调整下面的代码以依赖 Redis 而不是内存?
任何关于如何设置 RedisTokenStore 的评论将不胜感激。
另外,如果添加这样的附加注释很容易,“.passwordEncoder”和“.secret”有什么区别?下面的代码依赖于带有硬编码表达式(固定值)的“.secret”,而我看到很少有使用 jdbc 的示例,其中“.passwordEncoder 由 springframework.security.crypto.bcrypt.BCryptPasswordEncoder”填充,这似乎更有意义。当我猜我使用“.secret”或“.passwordEncoder”时,我是对的吗?当我认为 secret 代表固定值而 passwordEncoder 代表动态值时,我是对的吗?
(使用“.passwordEncoder”和clients.jdbc https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application.java#L102的示例)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private static String REALM="MY_OAUTH_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("abc-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.secret("abc-secret")
.accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
}
@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");
}
}
【问题讨论】:
标签: java spring-mvc spring-security oauth redis