如何在Spring中使用JDBC为ClientDetailsS​​erviceConfigurer添加客户端? [英] How to add a client using JDBC for ClientDetailsServiceConfigurer in Spring?

查看:538
本文介绍了如何在Spring中使用JDBC为ClientDetailsS​​erviceConfigurer添加客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内存工作方式如下:

I have the in memory thing working as follows:

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

我想使用JDBC实现.为此,我创建了以下表格(使用MySQL):

I would like to use the JDBC implementation. For this, I have created the following tables (using MySQL):

-- Tables for OAuth token store

CREATE TABLE oauth_client_details (
  client_id               VARCHAR(255) PRIMARY KEY,
  resource_ids            VARCHAR(255),
  client_secret           VARCHAR(255),
  scope                   VARCHAR(255),
  authorized_grant_types  VARCHAR(255),
  web_server_redirect_uri VARCHAR(255),
  authorities             VARCHAR(255),
  access_token_validity   INTEGER,
  refresh_token_validity  INTEGER,
  additional_information  VARCHAR(4096),
  autoapprove             TINYINT
);

CREATE TABLE oauth_client_token (
  token_id          VARCHAR(255),
  token             BLOB,
  authentication_id VARCHAR(255),
  user_name         VARCHAR(255),
  client_id         VARCHAR(255)
);

CREATE TABLE oauth_access_token (
  token_id          VARCHAR(255),
  token             BLOB,
  authentication_id VARCHAR(255),
  user_name         VARCHAR(255),
  client_id         VARCHAR(255),
  authentication    BLOB,
  refresh_token     VARCHAR(255)
);

CREATE TABLE oauth_refresh_token (
  token_id       VARCHAR(255),
  token          BLOB,
  authentication BLOB
);

CREATE TABLE oauth_code (
  code           VARCHAR(255),
  authentication BLOB
);

我需要在MySQL表中手动添加客户端吗?

Do I need to manually add a client in the MySQL tables?

我尝试过:

clients.jdbc(dataSource).withClient("clientapp")
               .authorizedGrantTypes("password", "refresh_token")
               .authorities("USER")
               .scopes("read", "write")
               .resourceIds(RESOURCE_ID)
               .secret("123456");

希望Spring可以在正确的表中插入正确的内容,但是似乎并没有这样做.为什么在jdbc()之后可以进一步链接?

Hoping that Spring would insert the correct things in the good tables, but it does not seem to do that. Why is it that you can further chain after jdbc() ?

推荐答案

这个问题已经很老了,但是没有一个答复给出了提问者最初的问题的答案.当我熟悉Spring的oauth2实现时,我偶然发现了相同的问题,并且想知道为什么ClientDetailsServiceConfigurer不能持久化通过JdbcClientDetailsServiceBuilder编程添加的客户端(通过在JdbcClientDetailsServiceBuilder上调用jdbc(datasource)方法实例化)配置器),尽管网上的所有教程都显示了类似的示例(例如Wim发布的示例).在深入研究代码之后,我注意到了原因.嗯,这仅仅是因为从未调用过更新oauth_clients_details表的代码.配置所有客户端后,缺少以下调用:.and().build().因此,Wim的代码实际上必须如下所示:

This question is fairly old but none of the replies gave an answer to the questioner's original problem. I've stumbled over the same issue while getting myself familar with spring's oauth2 implementation and wondered why the ClientDetailsServiceConfigurer is not persisting the clients that were programmatically added via the JdbcClientDetailsServiceBuilder (which is instantiated by calling the jdbc(datasource) method on the configurer), despite that all tutorials on the net showed a similar example such as that posted by Wim. After digging deeper into the code i've noticed the reason. Well, it's simply because the code to update the oauth_clients_details table is never called. What's missing is the following call after configuring all clients: .and().build(). So, Wim's code must actually look as follows:

clients.jdbc(dataSource).withClient("clientapp")
           .authorizedGrantTypes("password", "refresh_token")
           .authorities("USER")
           .scopes("read", "write")
           .resourceIds(RESOURCE_ID)
           .secret("123456").and().build();

等等,客户端clientapp现在已保存到数据库中.

Et voila, the client clientapp is now persisted into the database.

这篇关于如何在Spring中使用JDBC为ClientDetailsS​​erviceConfigurer添加客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆