这是我第一次使用OAuth2方法开发应用程序。我是基于某些教程开始的,现在我将从这个教程(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)开始。
我会将应用程序部署到集群WebSpheres,因此,据我所知,内存中将不会工作(...clients.inMemory().withClient ...)。
我想使用Redis (我也是第一次使用),但我有点困惑如何在某些无xml的java配置应用程序中设置它。
我在xml中发现了一些类似的问题,但我仍然不知道第一次尝试的方向(Redis Token Store)。有趣的是,在这里,问题所有者谈到了"Spring-Security,即2.8.0提供RedisTokenStore“,但我发现"2.0.12.RELEASE”是最新的OAuth发布版本。
也就是说,我直截了当的问题是:我如何调整下面的代码以依赖Redis而不是内存?
任何关于如何在下面设置RedisTokenStore的意见都将不胜感激。
另外,如果添加这样的附加注释很容易,那么".passwordEncoder“和".secret”有什么区别呢?下面的代码依赖于具有硬编码表达式(固定值)的“jdbc”,而我看到很少有示例使用带有“由springframework.security.crypto.bcrypt.BCryptPasswordEncoder”填充的.secret“的.passwordEncoder,这似乎更有意义。当我猜".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");
}
}发布于 2017-09-18 19:41:51
如果使用Spring Boot,则将依赖项添加到pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>在application.properties中使用合适的参数设置Redis连接:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379然后,将以下代码添加到您的AuthorizationServerConfiguration类中,您就应该准备好了:
@Bean
public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) {
return new RedisTokenStore(redisConnectionFactory);
}发布于 2017-07-08 20:15:06
在这里,我设置了一个oauth2授权服务器:https://github.com/zth390872451/oauth2-redis-mysql,如果你是中国人,你可以阅读这个blog .If不,我对此感到抱歉!在github的这个项目中,我使用oauth-server作为授权服务器,它使用redis来存储accesstoken,你只需要用来配置数据源和redis!通过复制两个类: AuthAuthorizeConfig和DataStoreConfig,你可以使用redis来存储token!
发布于 2020-06-10 20:02:44
如果使用Spring Boot,则将依赖项添加到pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<optional>true</optional>
</dependency>在application.properties中使用合适的参数设置Redis连接:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379然后,将以下代码添加到您的AuthorizationServerConfiguration类中,您就应该准备好了:
@Bean
public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) {
final RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
final TokenApprovalStore tokenApprovalStore = new TokenApprovalStore();
tokenApprovalStore.setTokenStore(redisTokenStore);
final JwtTokenStore jwtTokenStore = new JwtTokenStore(accessTokenConverter());
jwtTokenStore.setApprovalStore(tokenApprovalStore);
return jwtTokenStore;
}https://stackoverflow.com/questions/42449017
复制相似问题