如何配置 Spring Security OAuth 2.0 客户端存储到数据库

How to configure Spring Security OAuth 2.0 client store to database

我在 https://github.com/royclarkson/spring-rest-service-oauth

上找到了关于 Spring REST 服务 OAuth 的教程

但是我想知道如何配置客户端存储到数据库中,这样我就可以轻松管理。 在教程中,客户端配置存储在 class OAuth2ServerConfiguration.java

的内存中
    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        // @formatter:off
        clients.inMemory().withClient("clientapp")
                .authorizedGrantTypes("password", "refresh_token")
                .authorities("USER").scopes("read", "write")
                .resourceIds(RESOURCE_ID).secret("123456");
        // @formatter:on
    }

我相信这就是您要找的答案:

https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/client/JdbcClientDetailsService.java

这是 Spring-oAuth Impl class for JDBC。

HTH

@OhadR 谢谢你的回答,真的很感激!

我通过这个帖子偶然找到了答案:

为此我只需要两步:

  1. 创建代表客户详细信息的 table
   CREATE TABLE oauth_client_details (
      client_id VARCHAR(256) PRIMARY KEY,
      resource_ids VARCHAR(256),
      client_secret VARCHAR(256),
      scope VARCHAR(256),
      authorized_grant_types VARCHAR(256),
      web_server_redirect_uri VARCHAR(256),
      authorities VARCHAR(256),
      access_token_validity INTEGER,
      refresh_token_validity INTEGER,
      additional_information VARCHAR(4096),
      autoapprove VARCHAR(256)
    );
  1. 定义JDBC配置
DataSource dataSource = DataSourceBuilder.create()
    .driverClassName("com.mysql.jdbc.Driver")
    .url("jdbc:mysql://localhost:3306/gsrestdb").username("***").password("***").build();

    clients.jdbc(dataSource);