2017-02-24 64 views
1

OAuth2アプローチを使用してアプリケーションを開発するのは初めてです。私は特定のチュートリアルに基づいて始めました。私はこれから進んでいます(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)。redisを使用してspring-security-oauth2を使用してトークンを永続化する方法

アプリケーションをクラスタ化されたWebSphereにデプロイするので、インメモリが理解できない限り(... clients.inMemory()。withClient ...)。

私はRedis(私の最初の使い方も同様)を使用したいと思うし、特定のno-xml java設定アプリケーションでsettupする方法がちょっと混乱しています。

xmlで特定の同様の質問がありましたが、最初の試行ではまだ北ではありません(Redis Token Store)。興味深いことに、ここでは、オーナーが「Spring Security OAuth、つまり2.8.0はRedisTokenStoreを提供します」と話しましたが、最新のmvnリリースバージョンとして「2.0.12.RELEASE」が見つかりました。

私のまっすぐな質問は次のようなものです。メモリ内の代わりにRedisに依存するようにコードを調整するにはどうすればいいですか?

RedisTokenStoreを設定する方法についてのご意見をお待ちしております。

さらに、このようなコメントを追加するのが簡単な場合は、「.passwordEncoder」と「.secret」の違いは何ですか?コードコーディングでは、ハードコードされた式(固定値)で ".secret"を使用していますが、jdbcを "springframework.security.crypto.bcrypt.BCryptPasswordEncoderで埋め込まれた.passwordEncoderで埋め込まれた"という例がほとんどありません。私は ".secret"か ".passwordEncoder"のどちらかを使うと思います。私は秘密が固定値を表し、passwordEncoderはダイナミックなものを意味すると思うのですか?

ここ

@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

(たとえば ".passwordEncoder" とclients.jdbc https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application.java#L102を使用して)、私はのOAuth2 authrizion [サーバー]に設定:https://github.com/zth390872451/oauth2-redis-mysqlをあなたは中国人だったら、あなたはこのblog .IFを読むことができますそうではない、それについては残念だよ! githubのこのプロジェクトでは、認証サーバーとしてoauth-serverを使用しています。これはアクセス権を格納するためにredisを使用しています。データソースとredisを構成するためだけに使用します。コピー2つのクラスを介して、そこに:AuthAuthorizeConfigとDataStoreConfig、あなたはトークンを格納するためにredisを使用することができます!

1

のpom.xmlに依存関係を追加し、春ブーツを使用している場合:あなたのAuthorizationServerConfigurationにこれを追加し、その後

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

:application.propertiesでappropiateパラメータを持つ

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

セットアップRedisの接続をあなたは行く準備ができている必要があります:

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

これは、ありがとうございます! –

関連する問題