环境:

spring boot 2.0.4.RELEASE

spring security oauth 2.3.3.RELEASE

OAuth2的配置

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate AuthenticationManager authenticationManager;@Autowiredprivate UserDetailsService userService;@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {security.allowFormAuthenticationForClients();}@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("client_1").secret("123456").authorizedGrantTypes("password", "refresh_token").scopes("read", "write").accessTokenValiditySeconds(10000).refreshTokenValiditySeconds(10000);}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints){endpoints.authenticationManager(authenticationManager).userDetailsService(userService);}}

Spring Security配置

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredUserDetailsService userDetailsService;@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService);}@Overrideprotected void configure(HttpSecurity http) throws Exception {}}

使用postman进行请求时,会提示bad client credentials

{"error": "invalid_client","error_description": "Bad client credentials"
}

该请求甚至没有走到 UserDetailService 就已经被返回,很是诡异。

在网上搜索,查找相关技术博客,都无果。

其实问题出在 PasswordEncoder 身上,网上很多说明都没有写这个Bean的配置,如果不配置这个Bean的话AuthenticationManager 是直接初始化失败的,而Spring Security自带的这个Encoder,是对密码进行了一层加密,如果我们传铭文进去是会被直接报warn的log并且返回。

作者原来自以为"正确"的使用姿势:

    @Beanpublic PasswordEncoder passwordEncoder() {return new PasswordEncoder() {@Overridepublic String encode(CharSequence charSequence) {return charSequence.toString();}@Overridepublic boolean matches(CharSequence charSequence, String s) {return Objects.equals(charSequence.toString(),s);}};}

这样看起来请求是一切正常的。

但是!!!

根据评论的小伙伴【@街头诗人sp https://me.csdn.net/sp_studio】 的提示,spring security文档中提示了可以在密码前加入一段内容,来使用特别的encoder进行操作。

https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding

While this password is not stored in a secure format, it does allow for other passwords to be stored in a secure format. We can also request the users to change their password which would update the hash it was stored in.

For astute readers, it might be obvious that you can also migrate plain text passwords by prefixing them with {noop}. For example, with a password of

password

you can simply prefix the password with {noop} like:

{noop}password

因此,简单的在密码前拼接{noop}即可

同理,也支持拼接{sha256}等方法提示spring使用的加密方式

注意,实际应用中不要使用明文密码!数据库中存储的密码应当是加密过的,明文密码后患无穷。

【Spring Boot】Spring Boot 2.x + Spring Security OAuth2 2.3.3 出现 bad client credentials 错误的踩坑记录相关推荐

  1. Eclipse转IDEA开发java项目spring+mybaits项目踩坑记录

    久了不用一个东西总有遗忘,记录是你快速找回状态之本.今天将原来eclipse写的spring+mybatis Demo在 idea上跑起来,花了不少时间.这里将坑记录下: 一.IDEA创建项目 1. ...

  2. Spring Boot整合Security+Swagger2踩坑记录,Swagger2无法访问首页

    问题: 访问不了Swagger的页面 有可能出现的原因: 1.Security将访问拦截掉了 2.给拦截器拦截了 3.Swagger返回的结果,被自己定义的拦截器or其他方式给篡改了数据,导致无法正常 ...

  3. Spring踩坑记录

    1. Spring properties配置项不能解析问题 本地部分配置文件迁到disconf,希望disconf的配置文件交由spring托管.这样的话,原有代码中引用配置的地方就不用变(还是用${ ...

  4. spring boot 集成 redis spring-boot-starter-data-redis 2.1.7.RELEASE jedis: pool: #连接池配置 及踩坑经验

    目录 先上一些踩坑报错,各类报错 @org.springframework.beans.factory.annotation.Autowired(required=true) Error creati ...

  5. Spring Boot 学习之路之 Spring Security(二)加入mybatis

    上一篇 Spring Security 基础配置:  http://t.csdn.cn/m9oq5​​​​​​​ 在上文Spring Boot 学习之路之 Spring Security(一)中完成了 ...

  6. Spring Boot (三)集成spring security

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  7. spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke

    spring boot启动报错: Exception encountered during context initialization - cancelling refresh attempt: o ...

  8. spring boot security oauth2 认证服务器搭建

    最近在搞关于oauth2的系统,在网上的资料比较杂乱,也痛苦了不少了时间,最后终于开发好了符合自己实际应用的系统 在这里顺便把整理后的例子发上来,希望能够帮到别人,同时也给自己留个记录 首先,先添加依 ...

  9. Spring Boot Security OAuth2 入门

    1. 概述 本文,我们来入门 Spring Security OAuth2.0 的使用.通过本文,希望你对 OAuth2.0 有一次身临其境的感受. 另外,这是一篇入门的文章,所以实际场景下,需要做一 ...

最新文章

  1. 后浪优秀!21 岁小伙兼职程序员养家,大三存款达 6 位数
  2. C# Json 序列化与反序列化二
  3. 数据库设计范式1NF-5NF白话总结
  4. 10投屏后没有声音_手机投屏到电视没有声音?
  5. rpc 服务器不可用_RPC和微服务
  6. 理工男最该死的十二个瞬间
  7. LOCK TABLES
  8. Android 成用户隐私安全问题严重
  9. Markdown下载地址
  10. excel vba 数据分析
  11. [自学笔记]UE4(虚幻四)初学者入门
  12. Explain是什么?Explain能干嘛?
  13. CSS3 盒子设置border和padding不撑开盒子
  14. 常用英语口语绝佳句型100句
  15. meso-四(邻烷氧基苯基)卟啉合钴(meso-T(2-ROP)PCo);meso-四-(N-苄基)吡碇基卟啉锌(ZnTBPyP);离子型锰卟啉化合物[MnTTMAPP][PF6]5齐岳供应
  16. 数据仓库之主题模型之IBM与TD金融模型
  17. 好奇心可以抵达的地方
  18. R语言可视化进阶-高级点图、气泡图、动态图、图形叠加与相关图
  19. Cadence Allegro如何设置stroke快捷命令
  20. java毕业设计电力公司员工安全培训系统Mybatis+系统+数据库+调试部署

热门文章

  1. 无线知识:dBm、dBw、dB、dBi、dBd、asu、RSSI(接收信号强度)、天线增益、灵敏度等无线参数快速了解
  2. Python实现生成EAN-13条码并识别(一)
  3. php 编辑器 插入视频,苗景云的博客-PHPCMS V9编辑器中新增上传mp4视频(上传视频并插入HTML5的video标签)的功能...
  4. 使用微软云人工智能,合成带感情的语音
  5. 配置maven及其换源
  6. java自定义运算符_Java运算符
  7. 用Visio画时序分析波形图
  8. 因男友迟迟没升 P8 分手?? | 每日趣闻
  9. 让AI帮你工作(2)-如何阅读pdf论文
  10. 二进制,八进制,十进制,十六进制相互转换的快速记忆法