【问题标题】:how to use redis to persist token using spring-security-oauth2如何使用 redis 使用 spring-security-oauth2 持久化令牌
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在这里,我设置了一个oauth2授权[服务器]:https://github.com/zth390872451/oauth2-redis-mysql,如果你是中国人,你可以阅读这个blog。如果不是,我很抱歉! github的这个项目,我使用oauth-server作为授权服务器,它使用redis来存储accesstoken,你只是用来配置数据源和redis!通过复制两个类,有AuthAuthorizeConfig和DataStoreConfig,就可以使用redis来存储token了!

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          Spring Boot 2.0.x 的更新,你可能会遇到这个问题:https://github.com/spring-projects/spring-security-oauth/pull/1319#issuecomment-379736348

          要在 2.0.x 分支中修复它,请使用问题中提供的 RedisTokenStorePatched 类,并实例化它而不是 RedisTokenStore。

          【讨论】:

            【解决方案5】:

            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 服务器,我使用 this 将 Redis 安装为 Windows 服务。

            【讨论】:

              猜你喜欢
              • 2020-07-04
              • 2014-11-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-08-03
              • 1970-01-01
              • 2016-04-26
              • 1970-01-01
              相关资源
              最近更新 更多