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

时间:2017-02-24 22:00:36

标签: java spring-mvc spring-security oauth redis

这是我第一次使用OAuth2方法开发应用程序。我开始基于某些教程,我正在向前推进(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)。

我将应用程序部署到集群WebSpheres,因此,据我所知,内存不起作用(... 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"之间的区别是什么?和#34;。秘密"?下面的代码依赖于" .secret"使用硬编码表达式(固定值),而我看到几个使用jdbc的示例,其中#34; .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");
    }

}

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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 服务。