Spring Security OAuth2.0(20):完善环境配置

文章目录

本章代码已提交至Gitee: https://gitee.com/lengcz/distributed-security01.git

环境配置

目前客户端信息和授权码仍然存储在内存中,生产环境中通过会存储在数据库中,下面完善环境的配置:

创建表

(1) 创建表

sql 复制代码
DROP TABLE IF EXISTS `oauth_client_details`;
CREATE TABLE `oauth_client_details` (
  `client_id` varchar(255) NOT NULL,
  `resource_ids` varchar(255) DEFAULT NULL,
  `client_secret` varchar(255) DEFAULT NULL,
  `scope` varchar(255) DEFAULT NULL,
  `authorized_grant_types` varchar(255) DEFAULT NULL,
  `web_server_redirect_uri` varchar(255) DEFAULT NULL,
  `authorities` varchar(255) DEFAULT NULL,
  `access_token_validity` int(11) DEFAULT NULL,
  `refresh_token_validity` int(11) DEFAULT NULL,
  `additional_information` varchar(255) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `archived` tinyint(4) DEFAULT NULL,
  `trusted` tinyint(4) DEFAULT NULL,
  `autoapprove` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of oauth_client_details
-- ----------------------------
INSERT INTO `oauth_client_details` VALUES ('c1', 'res1', '$2a$10$iL3DjyR4rTVdYwfp/jDtEurY7cLS7ZVZpShB72L5IFRkR/e9PiiWW', 'ROLE_ADMIN,ROLE_USER,ROLE_API', 'authorization_code,password,client_credentials,implicit,refresh_token', 'https://www.baidu.com/', 'ROLE_ADMIN,ROLE_USER,ROLE_API', '7200', '2592000', null, '2026-07-12 00:24:15', '0', '0', 'false');

DROP TABLE IF EXISTS `oauth_code`;
CREATE TABLE `oauth_code` (
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `code` varchar(255) NOT NULL,
  `authentication` blob NOT NULL,
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(2)修改UAA的AuthorizationServer,引入passwordEncoder,增加clientDetailsService

java 复制代码
@Autowired
    private PasswordEncoder passwordEncoder;

    @Bean
    public ClientDetailsService clientDetailsService(DataSource dataSource){
        ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);//配置数据源
        ((JdbcClientDetailsService)clientDetailsService).setPasswordEncoder(passwordEncoder);//配置密码器
        return clientDetailsService;
    }

(3) 使用clientDetailsService

java 复制代码
 public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(clientDetailsService); //使用自己的service
    }

(4)原来授权码存储在内存中,现在将授权码存储到数据库中

java 复制代码
//    @Bean
//    public AuthorizationCodeServices authorizationCodeServices(){
//        return new InMemoryAuthorizationCodeServices();//设置授权码模式的使用(授权码存在内存中),
//    }

    @Bean
    public AuthorizationCodeServices authorizationCodeServices(DataSource dataSource){
        return new JdbcAuthorizationCodeServices(dataSource);//设置授权码模式的使用(存储到数据库),
    }

(5) 启动UAA ,测试一下。

调用一下/uaa/oauth/token通过密码模式获取令牌

使用/uaa/oauth/check_token 查看令牌,校验令牌合法性。

比较内容是否和数据库一致

授权码测试,打开授权码的连接(注意配置的值需要和数据库中的值保持一致,或者在数据库中存在)

http://localhost:53020/uaa/oauth/authorize?client_id=c1\&response_type=code\&scope=ROLE_ADMIN\&redirect_uri=https://www.baidu.com

打开链接,登录后,提示授权,选择Approve ,并点击按钮。

然后查看oauth_code表,发现这个授权码存在数据库中。

遇到的问题

提示 Bad client credentials 认证错误

请检查 BCrypt格式的密码是否正确

java 复制代码
boolean eq = BCrypt.checkpw("明文","密文");//BCrypt比较
java 复制代码
System.out.println(BCrypt.hashpw("secret",BCrypt.gensalt()));//加密
相关推荐
我命由我1234513 分钟前
人脸识别 - 人脸识别选帧
java·人工智能·python·算法·安全·java-ee·人脸识别
宠友信息15 分钟前
IM系统开发技术路线分析,即时通讯源码助力快速搭建聊天应用
java·spring boot·redis·websocket·mysql·uni-app·vue
long3161 小时前
封装(Encapsulation)
java·人工智能·ai·ai编程
SomeB1oody1 小时前
【RustyML入门】7.2. 深入模型持久化
开发语言·后端·机器学习·rust·教程
知几蜗牛1 小时前
0 后端 · 0 数据库 · 0 备案:用 AI 两天搓出的股票管理系统,开源了
前端·后端·llm
yaoxin5211231 小时前
502. Java 反射 - 编写 MessageInterceptor 类
java·开发语言
风流 少年1 小时前
Spring AI 2.0:阿里云百炼平台(工作流应用)
java·后端·spring
长谷深风1111 小时前
AI Tool 设计:粒度、参数与错误恢复怎么做
java·大数据·人工智能·ai agent·agent工作流·智能体设计·ai产品设计
__zRainy__1 小时前
Node系列 · 数据库:单表查询
数据库·后端·mysql·node.js
诺伦1 小时前
Rust 错误处理实战:从 unwrap 到优雅 Result 的进阶之路
开发语言·后端·rust