前言

**使用SpringSecurity OAuth时、认证服务器和资源服务器搭建完毕后,启动也没报错,然鹅问题还是有的!由于是在搭建阶段为了方便调试SpringSecurityOAuth2核心流程,也就是授权、认证这部分,所以刚开始客户端端点信息式存储在内存中的,但是实际开发中这些端点信息需要写入到数据库中的! **

内存中存储端点信息


怎么输入账号密码都不行

异常如下

Encoded password does not look like BCrypt


TMMD,这里就关系到版本问题,在SpringBoot1.X的时候认证服务配置如下即可

@Configuration
//@Order(99)
@EnableAuthorizationServer
public class MyAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("tao").secret("aaa").redirectUris("xxxxxx").scopes("all").authorizedGrantTypes("authorization_code","password");}
}

但是现在升到SpringBoot2.X的时候就包这个问题 Encoded password does not look like BCrypt
那么就需要配置一下编码器
在@Configuration配置类下注入

 @Beanpublic PasswordEncoder passwordEncoder(){//密码加密return new BCryptPasswordEncoder();}

然后在认证服务器中添加编码规则

@Configuration
@EnableAuthorizationServer
public class MyAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Autowiredpublic PasswordEncoder passwordEncoder() {//密码加密return new BCryptPasswordEncoder();}@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("tao").secret(passwordEncoder().encode("aaa")).redirectUris("http://xx.sxxxx.xx/lxxive/pay/getCode.html").scopes("all").authorizedGrantTypes("authorization_code","password");}}

这里实际上就是将"aaa"做了passwordEncoder加密存储在内存中!这样确实解决了在内存中Encoded password does not look like BCrypt异常问题!下面还没完,还有读取数据库中的端点信息报错!报错信息也是一样的Encoded password does not look like BCrypt

数据库中存储端点信息

从内存中读取客户端端点信息切换到数据库中其实很简单,几步搞定

1.导入相关依赖

     <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.2.0</version></dependency>

2.添加对应yml配置

  datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: xxxxpassword: xxxxxxurl: jdbc:mysql://xxx.xxx.xxx.xxx/xx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai

3.建库建表

DROP TABLE IF EXISTS `oauth_client_details`;
CREATE TABLE `oauth_client_details` (
`client_id` varchar(255) NOT NULL COMMENT '客户端ID',
`resource_ids` varchar(255) DEFAULT NULL COMMENT '资源ID集合,多个资源时用逗号(,)分隔',
`client_secret` varchar(255) DEFAULT NULL COMMENT '客户端密匙',
`scope` varchar(255) DEFAULT NULL COMMENT '客户端申请的权限范围',
`authorized_grant_types` varchar(255) DEFAULT NULL COMMENT '客户端支持的grant_type',
`web_server_redirect_uri` varchar(255) DEFAULT NULL COMMENT '重定向URI',
`authorities` varchar(255) DEFAULT NULL COMMENT '客户端所拥有的Spring Security的权限值,多个用逗号(,)分隔',
`access_token_validity` int(11) DEFAULT NULL COMMENT '访问令牌有效时间值(单位:秒)',
`refresh_token_validity` int(11) DEFAULT NULL COMMENT '更新令牌有效时间值(单位:秒)',
`additional_information` varchar(255) DEFAULT NULL COMMENT '预留字段',
`autoapprove` varchar(255) DEFAULT NULL COMMENT '用户是否自动Approval操作,false时需要跳转到授权界面,true无需跳转'PRIMARY KEY (`client_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='终端信息表';

提供两条测试数据

INSERT  INTO `oauth_client_details`(`client_id`,`resource_ids`,`client_secret`,`scope`,`authorized_grant_types`,`web_server_redirect_uri`,`authorities`,`access_token_validity`,`refresh_token_validity`,`additional_information`,`autoapprove`)
VALUES
('app',NULL,'app','all','password,refresh_token,authorization_code,client_credentials','https://editor.csdn.net/md/?articleId=109684318',NULL,NULL,NULL,NULL,'false'),
('tao',NULL,'$2a$10$HUYTaaKfdX2Jt7cxMU/rhunGD16d.xGRxmbF6BEQhdCXc5.Gwv.Du','all','password,refresh_token,authorization_code,client_credentials','https://editor.csdn.net/md/?articleId=109684318',NULL,NULL,NULL,NULL,'false'),

其他相关建表详情我这里目前只使用到这oauth_client_details张表,所以其他表不做扩展介绍了!

4.修改AuthorizationServerConfig代码
原有客户端端点信息配置

 @Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {//内存中读取,方便服务搭建是测试clients.inMemory().withClient("tao").secret(passwordEncoder().encode("secret")).resourceIds("product-id") //资源标识用来限制可以访问的资源服务标识.redirectUris("http://47.111.238.83:8077/build/goRoomSearch?buildId=136").scopes("all")//允许的授权范围.autoApprove(false)//false表示跳转到授权页面.authorizedGrantTypes("authorization_code","password").and().withClient("client_2").resourceIds("yiyi").authorizedGrantTypes("password", "refresh_token").scopes("select").authorities("client").secret(passwordEncoder().encode("123456"))}

修改为如下

 @Beanpublic ClientDetailsService clientDetails() {ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);return clientDetailsService;}//配置端点信息@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.withClientDetails(clientDetails());}

或者直接写在一起

 //配置端点信息@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);clients.withClientDetails(clientDetailsService );}

5重启认证服务器
前面在数据库中插入两条端点记录,一条为app的另一条为tao的,我们先用app这个端点获取授权码,获取授权码是成功的,那么我们在通过授权码获取Token,那么这时还是会报同样的错Encoded password does not look like BCrypt,不要慌,这时我故意的是为了引出根本问题所在,我们切换端点获取授权码,使用tao这个端点获取授权码,然后通过授权码得到token,那么这里是可以的,注意tao这个端点的client_secret是加密的,原密码是secret,这里其实就和内存中将"aaa"做了passwordEncoder加密存储在内存中是一样的,问题的根源就在于SpringSecurityOAuth2.0版本升级后做了安全官方说明

6.问题解决

  1. 一般我们客户端账号密码不需要加密,所以在这里实现 .passwordEncoder(NoOpPasswordEncoder.getInstance())告诉security客户端密码不需要加密(这种方式我测试了一下,好像并没什么卵用,还是会报同样的错)
@Beanpublic ClientDetailsService clientDetails() {ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);((JdbcClientDetailsService) clientDetailsService).setPasswordEncoder(NoOpPasswordEncoder.getInstance());return clientDetailsService;}
  1. 使用BCryptPasswordEncoder将数据库中client密码加密
 System.out.println(new BCryptPasswordEncoder().encode("secret"));

第一种方式不通过分析一下,因为这里校验secret的逻辑是将客户端传入的和数据库中的secret解密后进行对比,所以第一种方式setPasswordEncoder只是更改编码方式,也就是数据库中的secret必须是编码后的数据

SpringSecurityOAuth2.0获取Token时报错Encoded password does not look like BCrypt相关推荐

  1. svn获取代码时报错【 C:\Users\ADMINI~1\APPData\Loca\Temp\svn.... 找不到指定路径】

    获取代码时报错:C:\Users\ADMINI~1\APPData\Loca\Temp\svn- 找不到指定路径 解决办法:清理原始副本

  2. Encoded password does not look like BCrypt最新异常处理

    报错信息: 控制台报错信息 原由:spirng boot 1.5.* 升级到spring boot 2.0.*,当再次访问授权服务器时出现Encoded password does not look ...

  3. Spring Security使用出现 Encoded password does not look like BCrypt异常的解决以及Spring Securit加密方式的学习

    QUESTION:Spring Security使用出现 Encoded password does not look like BCrypt异常的解决以及Spring Securit加密方式的学习? ...

  4. CentOS 安装QT SDK qt-everywhere-src-5.15.0.tar.xz时报错

    状态介绍: centos7 安装了默认devement tools + 安装好 QT Creator 后,正在配置 QT SDK 参考资料: CentOS 6.5 下安装 QT 4 - 天涯路清晨 - ...

  5. 解决更改mysql密码时报错Your password does not satisfy the current policy requirements问题

    输入ALTER USER 'root'@'localhost' IDENTIFIED BY 'python123'; 然后报错Your password does not satisfy the cu ...

  6. 编译hadoop2.2.0源码时报错

    编译hadoop2.2.0源码时, mvn install -DskipTests 报错: [ERROR] COMPILATION ERROR : [INFO] ------------------- ...

  7. bae 3.0 mysql_bae3.0 mysql 有时报错?报错-问答-阿里云开发者社区-阿里云

    Jfinal 用的MySQL的数据源:MysqlDataSource ds = new MysqlDataSource(); Config: MysqlDataSource ds = new Mysq ...

  8. php获取prepay_id时报错,微信支付获取prepay_id返回签名错误,官方demo中的签名方法MD5验证有问题...

    引用官方V3微信支付PHP版的demo做的改动,在获取prepay_id发现总是返回签名错误,经与腾讯官方的"微信公众平台支付接口调试工具"对比发现MD5不一样,找到demo中代码 ...

  9. 关于在centos下安装python3.7.0以上版本时报错ModuleNotFoundError No module named _ctypes 的解决办法

    3.7版本需要一个新的包libffi-devel,安装此包之后再次进行编译安装即可. #yum install libffi-devel -y #make install 若在安装前移除了/usr/b ...

  10. 关于卡巴斯基6.0自动升级时报错后自动退出的情况

    11月22日左右,很多装了卡巴斯基的朋友都出现了卡巴斯基报错以后自动关闭的问题. 其实这次出错是卡巴自己的升级问题,卡巴的安全专家放出更新中断的解释(已解决更新中断的问题),是由于卡巴服务器更新文件错 ...

最新文章

  1. js之call,apply和bind的模拟实现
  2. Problem G: 部分复制字符串
  3. 合并K个排序链表—leetcode23
  4. 远控免杀专题(24)-CACTUSTORCH免杀
  5. [CB]TForm应用技巧
  6. 【报告分享】2021年中国人工智能产业研究报告:数字经济时代的产业升级探索.pdf(附下载链接)...
  7. python 图片生成视频_python--通过cv2多张图片生成视频
  8. Spring中的Bean是如何被回收的?
  9. python怎么复数乘方开方_孩子数学不好怎么办?怎样让孩子学好数学的方法
  10. android 自动更新
  11. JS打印对象的方法将Object转换为String的函数
  12. Gallery3d 学习笔记(14)
  13. maven mybatis实现递归查询和使用存储过程
  14. 面向对象进阶 三大特性
  15. Deprecated:function eregi() is deprecated in /usr/local/apache/libraries/lib_lang.php on line 8
  16. css完整总结:第二篇(尺寸,外补白,内补白,边框,背景,颜色,字体,文本,文本装饰)
  17. 家庭无线路由器桥接设置
  18. 中国大学Mooc浙大翁恺老师《零基础学Java语言》编程作业(续)(5~ 7)
  19. 到底多大并发才算高并发?
  20. AsyncTask源码解析,你需要摸清的细节

热门文章

  1. CentOS6安装rpm时报错“Cannot find a valid baseurl for repo: centos-sclo-rh“的解决办法
  2. androidstudio上传自己的lib到Jcenter
  3. 火爆全网的动态曲线图是怎么做的?
  4. 正态总体参数的假设检验
  5. 4.29 C语言练习(宏定义练习:输入两个整数,求他们相除的余数。用带参的宏来实现,编程序。)
  6. 亿级流量 即时通讯IM系统 设计详解(全)
  7. 【每日一练】21—CSS实现炫酷动画背景
  8. 设计模式---004策略模式(转载自我的老师 Alley-巷子)
  9. android 头像球_Android自定义View实现圆形头像效果
  10. 纯css实现3D立体六面体照片墙