2017-02-24 218 views
1

這是我第一次用OAuth2方法開發應用程序。我開始基於一定的教程,我正在從這裏前進(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)。如何使用redis來使用spring-security-oauth2持久化令牌

我會將應用程序部署到集羣WebSpheres,所以,據我所知in-memory將無法工作(... clients.inMemory()。withClient ...)。

我想使用Redis(我的第一次使用),我有點困惑如何將其設置在某些沒有xml java配置應用程序。

我發現了一些類似的問題與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」時,我是對的嗎?當我認爲祕密代表固定值和密碼編碼器用於動態密碼時,我是對的嗎?

(使用 「.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"); 
    } 

} 

回答

2

在這裏,我設置了一個的oauth2 authrizion [服務器]:https://github.com/zth390872451/oauth2-redis-mysql,如果你是中國人,你可以看到這篇blog。如果不,對此我很抱歉! 這GitHub的項目,我用的是OAuth的服務器作爲授權服務器,它使用Redis的存儲的accessToken,你將只使用配置數據源和Redis的!通過拷貝中的兩個類中,有:AuthAuthorizeConfig和DataStoreConfig,您可以使用Redis的存儲令牌!

1

如果使用Spring啓動,添加依賴關係的pom.xml:在application.properties與此時,相應的參數

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-redis</artifactId> 
</dependency> 

安裝Redis的連接:

spring.redis.host=localhost 
spring.redis.password=secret 
spring.redis.port=6379 

然後,添加到您的AuthorizationServerConfiguration類,你應該準備去:

@Bean 
public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) { 
    return new RedisTokenStore(redisConnectionFactory); 
} 
+0

這個作品,謝謝! –