简体   繁体   English

用于使用 JPA 的实现 ClientDetailsS​​ervice (Spring Security OAuth2)

[英]Implementation ClientDetailsService (Spring Security OAuth2) for working with JPA

I developed a REST service and I want to add OAuth2.我开发了一个 REST 服务,我想添加 OAuth2。 Do I understand correctly that clients in OAuth2 are trusted applications and the developers must register them such as in Intstagram or VK.com either Facebook ?我是否正确理解 OAuth2 中的客户端是受信任的应用程序,开发人员必须在 Intstagram 或 VK.com 或 Facebook 等中注册它们?

At the moment I create the clients in that way:目前我以这种方式创建客户端:

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

But I want to create them dynamically and to save into the database.但我想动态创建它们并保存到数据库中。 I have found the implementation by JBDC.我找到了 JBDC 的实现。 But I want to do it using JPA(Hibernate).但我想使用 JPA(Hibernate) 来做到这一点。

Do I understand right that I need to:我是否理解正确,我需要:

1.Create the database schema 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. Create the entity CustomClientDetails which implements 2. 创建实体 CustomClientDetails 实现

public interface ClientDetails extends Serializable

3. And to implement 3.并实施

public interface ClientDetailsService

4. And at last 4. 最后

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

So using this way will I be able to create the clients dynamically using the repository and service layer?那么使用这种方式我是否能够使用存储库和服务层动态创建客户端?

The way it works for me, 1. Model your JPA object with column names matching the oauth_client_details schema 2. Write ClientService class to perform CRUD operations on the OathClientDetails object 3. Change your ClientServerConfigure to make use of jdbc它适用于我的方式,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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM