簡體   English   中英

如何使用 redis 使用 spring-security-oauth2 來持久化令牌

[英]how to use redis to persist token using spring-security-oauth2

這是我第一次使用 OAuth2 方法開發應用程序。 我是基於某些教程開始的,我正在從這個( http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/ )向前邁進。

我會將應用程序部署到集群 WebSphere,因此,據我所知,內存中將不起作用(...clients.inMemory().withClient ...)。

我想使用 Redis(也是我的第一次使用),但我對如何在某些 no-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”時,我是對的嗎? 當我認為 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");
    }

}

如果使用 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);
}

在這里,我設置了一個oauth2授權[服務器]: https : //github.com/zth390872451/oauth2-redis-mysql ,如果你是中國人,你可以閱讀這個博客。如果不是,我很抱歉! github的這個項目,我使用的是oauth-server作為授權服務器,它使用redis來存儲accesstoken,你只是用來配置數據源和redis! 通過復制兩個類,有: AuthAuthorizeConfig 和 DataStoreConfig ,可以使用redis來存儲token!

如果使用 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;
}

Spring Boot 2.0.x 的更新,你可能會遇到這個問題: https : //github.com/spring-projects/spring-security-oauth/pull/1319#issuecomment-379736348

要在 2.0.x 分支中修復它,請使用問題中提供的 RedisTokenStorePatched 類,並實例化它而不是 RedisTokenStore。

pom.xml添加以下依賴項:

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>{version}</version>
    </dependency>

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

applicationContext.xml配置如下:

  <bean id="redisConnectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="localhost"/>
    <property name="port" value="6379"/>
    <property name="password" value=""/>
    <property name="database" value="1"/>
  </bean>

  <bean id="tokenStore"
    class="org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore">
    <constructor-arg name="connectionFactory" ref="redisConnectionFactory"/>
  </bean>

由於我們有 Windows 2016 服務器,我使用來安裝 Redis 作為 Windows 服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM