繁体   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