Spring Cloud OAuth2中访问/oauth/token报invalid_client问题的解决

  • 问题分析
  • 问题解决

问题分析

初建Spring Cloud OAuth2项目中访问获取access_token票证的端点/oauth/token时报invalid_client错误,postman中错误信息如下:

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

如下图:

Java后台警告如下:

2021-01-16 18:14:53.482  WARN 11764 --- [nio-5002-exec-2] o.s.s.c.bcrypt.BCryptPasswordEncoder     : Encoded password does not look like BCrypt

导致以上问题的原因是在最新Spring Cloud Security OAuth2中,在AuthorizationServerConfigurerAdapters实现类中定义client设置secret时必须要使用密码加密,而不能直接使用明文密码。

问题解决

关键代码
把以下代码

package com.wongoing.oauth2.config;
@Configuration
@EnableAuthorizationServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {        clients.inMemory().withClient("client_1").secret("123456").authorizedGrantTypes("password").scopes("all");}
}

改为如下的代码方式

package com.wongoing.oauth2.config;
@Configuration
@EnableAuthorizationServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate PasswordEncoder passwordEncoder;@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {     clients.inMemory().withClient("client_1").secret(this.passwordEncoder.encode("123456")).authorizedGrantTypes("password").scopes("all");}
}

在WebSecurityConfigurerAdapter实现类中定义PasswordEncoder。

package com.wongoing.oauth2.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

Spring Cloud OAuth2中访问/oauth/token报invalid_client问题的解决相关推荐

  1. Spring Cloud OAuth2中访问/oauth/token报Unsupported grant type: password问题的解决

    Spring Cloud OAuth2中访问/oauth/token报Unsupported grant type: password问题的解决 问题分析 问题解决 问题分析 在新建的Spring C ...

  2. spring cloud oauth2系列篇(二)深入authorization_code授权码模式完整实现

    项目的源码地址:https://github.com/daxian-zhu/online_edu 项目是最新的,文章可能不是最新的 这里先简单的介绍项目,不然有的地方可能我表达不清楚容易造成误解 on ...

  3. Spring Cloud OAuth2 JWT 微服务认证服务器得构建

    文章目录 Spring Cloud OAuth2 JWT 微服务认证服务器得构建 前言 认证服务得搭建 `AuthorizationServer` `WebSecurityConfig` `Autho ...

  4. refreshtoken用mysql_「SpringCloud」 Spring Security OAuth2 Mysql管理在线Token

    原标题:「SpringCloud」 Spring Security OAuth2 Mysql管理在线Token 前言:Spring Cloud 分布式中的登录如何可视化的管理目前下发的令牌.使用情况. ...

  5. 《深入理解 Spring Cloud 与微服务构建》第十七章 使用 Spring Cloud OAuth2 保护微服务系统

    <深入理解 Spring Cloud 与微服务构建>第十七章 使用 Spring Cloud OAuth2 保护微服务系统 文章目录 <深入理解 Spring Cloud 与微服务构 ...

  6. Spring Cloud OAuth2 实现用户认证及单点登录

    OAuth 2 有四种授权模式,分别是授权码模式(authorization code).简化模式(implicit).密码模式(resource owner password credentials ...

  7. java调用授权接口oauth2_微信授权就是这个原理,Spring Cloud OAuth2 授权码模式

    上一篇文章Spring Cloud OAuth2 实现单点登录介绍了使用 password 模式进行身份认证和单点登录.本篇介绍 Spring Cloud OAuth2 的另外一种授权模式-授权码模式 ...

  8. 面试官:能说一说微信授权的原理吗?(Spring Cloud OAuth2 授权码模式)

    我是风筝,公众号「古时的风筝」,一个简单的程序员鼓励师. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 上一篇文章Spring Cloud OA ...

  9. Spring Cloud OAuth2 认证服务器

    1.Spring Cloud OAuth2介绍 Spring Cloud OAuth2 是 Spring Cloud 体系对OAuth2协议的实现,可以用来做多个微服务的统一认证(验证身份合法性)授权 ...

最新文章

  1. TBtools - 超过一万人在使用的生信小工具
  2. SharePoint Server 2016 部署安装(三)—— 安装SharePoint Server
  3. RPC RESTful 解释
  4. hive(3)——在hive中使用自己写的函数(python实现)
  5. impala java api 操作_Impala实践之六:使用Rest Api
  6. 微软云 mysql 重启_停止Azure MySQL PaaS数据库
  7. mysql sql常用语句大全
  8. 8.郝斌C语言笔记——函数
  9. 华硕服务器主板那个系列好,华硕主板型号性价比排行 华硕主板那个性价最好用...
  10. Hadoop:INFO mapreduce.Job: Running job
  11. 移动端H5页面在ios设备上软键盘顶起页面后隐藏,页面不回弹的解决方案
  12. ajax串数组包含双引号,解决AJAX请求中含有数组的办法
  13. 淘宝 模拟 登录 总结 【QQ 346767073 】
  14. 【微信小程序调用百度API实现图像识别功能】----项目实战
  15. 【089】深度学习读书笔记:P29证明迹Tr(AB)=Tr(BA)
  16. pandas duplicated() 重复行标记与drop_duplicates()删除
  17. 俄政府为“俄罗斯离岸区”推出特别加密货币监管措施
  18. Ardunio下的STM32串口通信
  19. 分享一个国内可用的免费ChatGPT网站
  20. php解析json转数组,php json转数组出错

热门文章

  1. 拟牛顿法之DFP算法
  2. 用python3.7编写考拉兹猜想
  3. C# 支付宝接口在线收款退款
  4. 微信小程序开发工具快捷键 以及常用的调试页面介绍
  5. 自行实现 dotnet core rpc
  6. ORA-19905: log_archive_format must contain %s, %t and %r
  7. 同声传译又下一城!微软《Skype翻译》新增俄语支持
  8. 今天败入音特美ER6i
  9. Linux运维03:ps命令详解
  10. “华君系”入主失控 *ST海润连续跌停变“仙股”