【问题标题】:Implementation ClientDetailsService (Spring Security OAuth2) for working with JPA使用 JPA 实现 ClientDetailsS​​ervice (Spring Security OAuth2)
【发布时间】:2016-09-18 05:40:28
【问题描述】:

我开发了一个 REST 服务,我想添加 OAuth2。我是否正确理解 OAuth2 中的客户端是受信任的应用程序,开发人员必须在 Intstagram 或 VK.com 或 Facebook 中注册它们?

目前我以这种方式创建客户端:

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
            .inMemory()
                .withClient("clientapp")
                    .authorizedGrantTypes("password", "refresh_token")
                    .authorities("USER")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .secret("123456");
    }

但我想动态创建它们并保存到数据库中。我找到了 JBDC 的实现。但我想使用 JPA(Hibernate)来做到这一点。

我是否理解正确,我需要:

1.创建数据库架构

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)
);

2。创建实体 CustomClientDetails 实现

public interface ClientDetails extends Serializable

3。并实施

public interface ClientDetailsService

4。最后

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
                .withClientDetails(customClientDetailsService); 
        }

那么使用这种方式我可以使用存储库和服务层动态创建客户端吗?

【问题讨论】:

    标签: java spring spring-security spring-boot oauth-2.0


    【解决方案1】:

    它对我的工作方式, 1. 使用与 oauth_client_details 模式匹配的列名对 JPA 对象进行建模 2.编写ClientService类对OathClientDetails对象进行CRUD操作 3. 更改您的 ClientServerConfigure 以使用 jdbc

    @Autowired
    DataSource dataSource;
    
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }
    
    
    @Bean
    public PasswordEncoder userPasswordEncoder() {
       return new BCryptPasswordEncoder(4);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2015-05-18
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2021-02-25
      • 2018-10-25
      • 2020-01-03
      相关资源
      最近更新 更多