Spring OAuth2 Redis 客户端详细信息

标签 spring redis oauth-2.0 spring-oauth2

我有一个 oauth2 的工作示例,并为客户端和资源所有者进行内存中身份验证和授权。

我想使用 Redis,但对如何设置它有点困惑。

如何调整下面的代码,以便能够在 Redis 中保存数据(例如 token 、refresh_token 和其他客户端详细信息)

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Value("${spring.oauth2.realm.id}")
    private String 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("client001")
                .authorizedGrantTypes("client_credentials", "password")
                .authorities("ROLE_ADMIN", "ROLE_TRUSTED_CLIENT")
                .scopes("read", "write", "trust")
                .secret("secret")
                .accessTokenValiditySeconds(120) 
                .refreshTokenValiditySeconds(600);
    }

    @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");
    }

}

[已更新]

我能够利用 RedisTokenStore 将 token 存储到 Redis。为此,请在您的 OAuth2SecurityConfiguration 中替换以下行:

@Bean
public TokenStore tokenStore() {
    return new InMemoryTokenStore();
}

与...

@Bean
public TokenStore tokenStore(final RedisConnectionFactory factory) {
    return new RedisTokenStore(factory);
}

现在在AuthorizationServerConfiguration#configure(final ClientDetailsS​​erviceConfigurerclients);中遇到了一个新问题。是否支持redis

@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
        .withClient("client001")
        .secret("secret")
        .authorizedGrantTypes("client_credentials", "password")
        .authorities("ROLE_ADMIN", "ROLE_TRUSTED_CLIENT")
        .scopes("read", "write", "trust")
        .accessTokenValiditySeconds(60) 
        .refreshTokenValiditySeconds(120);
    }

当我查看ClientDetailsS​​erviceConfigurer时,它只支持inMemory()jdbc()。我打算使用withClientDetails()来支持redis。是否有 RedisClientDetailsS​​ervice 或类似的东西?

非常感谢任何对其实现的意见和建议。

最佳答案

我能够将客户端详细信息存储到 Redis。

这些是我所做的步骤:

  1. 创建以下类:

    • RedisClientDetailsS​​erviceBuilder.java
    • RedisClientDetailsS​​ervice.java

RedisClientDetailsS​​ervice

public class RedisClientDetailsService implements ClientDetailsService {

    private PasswordEncoder passwordEncoder = NoOpPasswordEncoder.getInstance();

    private Map<String, ClientDetails> clientDetailsStore = new HashMap<String, ClientDetails>();

    public ClientDetails loadClientByClientId(final String clientId) throws ClientRegistrationException {
        final ClientDetails details = clientDetailsStore.get(clientId);
        if (details == null) {
            throw new NoSuchClientException("No client with requested id: " + clientId);
        }
        return details;
    }

    public void setClientDetailsStore(final Map<String, ? extends ClientDetails> clientDetailsStore) {
        this.clientDetailsStore = new HashMap<String, ClientDetails>(clientDetailsStore);
    }

    /**
     * @param passwordEncoder the password encoder to set
     */
    public void setPasswordEncoder(final PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
}

RedisClientDetailsS​​erviceBuilder

public class RedisClientDetailsServiceBuilder extends ClientDetailsServiceBuilder<RedisClientDetailsServiceBuilder> {

    private Map<String, ClientDetails> clientDetails = new HashMap<String, ClientDetails>();

    private PasswordEncoder passwordEncoder; // for writing client secrets

    public RedisClientDetailsServiceBuilder passwordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
        return this;
    }

    @Override
    protected void addClient(
            final String clientId,
            final ClientDetails build) {

        clientDetails.put(clientId, build);
    }

    @Override
    protected ClientDetailsService performBuild() {
        final RedisClientDetailsService redisClientDetailsService = new RedisClientDetailsService();
        if (passwordEncoder != null) {
            // This is used to encode secrets as they are added to the database (if it isn't set then the user has top
            // pass in pre-encoded secrets)
            redisClientDetailsService.setPasswordEncoder(passwordEncoder);
        }

        redisClientDetailsService.setClientDetailsStore(clientDetails);
        return redisClientDetailsService;
    }

}

  • 然后在 AuthorizationServerConfiguration#configure() 上,使用我们创建的构建器设置客户端构建器。
  • @Override
    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
        final RedisClientDetailsServiceBuilder builder = new RedisClientDetailsServiceBuilder();
        clients.setBuilder(builder);
    
        // @formatter:off
        builder.withClient("client001")
              .secret("secret")
              .authorizedGrantTypes("client_credentials", "password")
              .authorities("ROLE_ADMIN", "ROLE_TRUSTED_CLIENT")
              .scopes("read", "write", "trust")
              .accessTokenValiditySeconds(120)
              .refreshTokenValiditySeconds(400)
        // @formatter:on
    }

    关于Spring OAuth2 Redis 客户端详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775964/

    相关文章:

    java - 我可以使用哪种 Spring 集成模式?

    caching - 如何在 spring boot 应用程序中实现 redis 分布式缓存,允许像 RDBMS 一样查询缓存数据

    redis - 如何使用 jedis 批量插入命令

    Azure AD 登录 - 如果缓存帐户对于我的应用程序来说是错误的,如何允许用户更改 Azure 帐户

    Azure B2C登录: how to get access token in React SPA and without using the Azure login pop or login page?

    ios - 优酷 : Multiple accounts authentication (OAuth) to upload video to single account/channel

    java - 尝试在 spring 框架中进行构造函数注入(inject)时出错

    java - 未找到类型 : class java. util.LinkedHashMap 返回值的转换器

    java - Spring bean 接线

    redis - 如何使用 ServiceStack Redis impl 自定义序列化