【问题标题】:Redis Token StoreRedis 令牌存储
【发布时间】:2016-02-18 17:03:09
【问题描述】:

我想在集群环境中部署我的 REST API。为此,我需要将我的 OAuth 2.0 令牌存储在共享令牌存储中。目前我正在使用 Spring Security 的 InMemoryTokenStore,它不能在多节点集群上共享。我打算使用 Redis 存储令牌。

我发现 Spring-Security OAuth 的最新版本,即 2.8.0 也提供了 RedisTokenStore。我对此有一些疑问:

  1. 在现有 spring-security xml 配置中使用 RedisTokenStore 需要进行哪些更改。目前我正在使用 InMemoryTokenStore。

  2. 如何使 RedisTokenStore 可与集群中的所有节点共享。

  3. 如果可以,我可以使用 Redis 集群来存储令牌吗?

【问题讨论】:

    标签: java spring oauth


    【解决方案1】:

    关于第一个问题:

    首先给大家参考一下redis token store的spring-security xml例子

    <!--Use Redis Token Store-->
    <beans:bean id="tokenStore"
                class="org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore">
        <beans:constructor-arg name="connectionFactory" ref="redisConnectionFactory"/>
    </beans:bean>
    
    <!--create redis connection factory and set db 1-->
    <beans:bean id="redisConnectionFactory"
                class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <beans:property name="hostName" value="localhost"/>
        <beans:property name="port" value="6379"/>
        <beans:property name="password" value=""/>
        <beans:property name="database" value="1"/>
    </beans:bean>
    

    其次,你需要在你的项目中添加spring data redis和jedis jar,我使用的是gradle,在依赖项中添加如下:

    ......
    compile 'org.springframework.data:spring-data-redis:1.6.2.RELEASE'
    compile 'redis.clients:jedis:2.8.0'
    ......
    

    关于第二个问题:

    如果您的一个集群的所有节点都使用一个 reids 服务器或集群,您的访问令牌将在所有节点之间共享。您可以检查您的 redis 数据库数据,并跟踪访问过程以验证这一点。所以你不用担心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-07
      • 2019-01-06
      • 2021-05-03
      • 1970-01-01
      • 2019-02-01
      • 2021-01-14
      • 2020-08-09
      • 2019-04-29
      相关资源
      最近更新 更多