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

  • 问题分析
  • 问题解决

问题分析

在新建的Spring Cloud OAuth2项目中使用grant_type为password方式访问时报Unsupported grant type: password。在postman中如下图:

{"error": "unsupported_grant_type","error_description": "Unsupported grant type: password"
}

如下图:

java后台报错如下:

2021-01-16 17:02:53.936  WARN 9132 --- [nio-5002-exec-2] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: UnsupportedGrantTypeException, Unsupported grant type: password

这个错误提示是说不支持grant type为password的方式获取access_token。
这里grant_type=password表示使用用户名密码的方式获取access_token。

问题解决

要允许oauth2 server支持通过grant_type=password方式获取access_token。需要代码中做以下调整
1、在WebSecurityConfigurerAdapter的实现类中增加获取AuthenticationManager的方法并进行@Bean标注,如下:

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();}@Override@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}
}

2、在AuthorizationServerConfigurerAdapter的实现类中使用AuthenticationManager,并在clients中指定授权类型authorizedGrantType(“password”),如下:

package com.wongoing.oauth2.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;@Configuration
@EnableAuthorizationServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {private TokenStore tokenStore = new InMemoryTokenStore();@Autowiredprivate PasswordEncoder passwordEncoder;@Autowiredprivate AuthenticationManager authenticationManager;@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {        clients.inMemory().withClient("client_1").secret(this.passwordEncoder.encode("123456")).authorizedGrantTypes("password")      //授权类型指定为password.scopes("all");}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);// 配置Token的存储方式endpoints.tokenStore(tokenStore)// 注入WebSecurityConfig配置的bean.authenticationManager(authenticationManager);}@Overridepublic void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {// 对获取Token的请求不再拦截oauthServer.tokenKeyAccess("permitAll()")// 验证获取Token的验证信息.checkTokenAccess("isAuthenticated()")//这个如果配置支持allowFormAuthenticationForClients的,且对/oauth/token请求的参数中有client_id和client-secret的会走ClientCredentialsTokenEndpointFilter来保护//如果没有支持allowFormAuthenticationForClients或者有支持但对/oauth/token请求的参数中没有client_id和client_secret的,走basic认证保护.allowFormAuthenticationForClients();}
}

Spring Cloud OAuth2中访问/oauth/token报Unsupported grant type: password问题的解决相关推荐

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

    Spring Cloud OAuth2中访问/oauth/token报invalid_client问题的解决 问题分析 问题解决 问题分析 初建Spring Cloud OAuth2项目中访问获取ac ...

  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. 优先深度搜索判断曲线相交_程序员必知的十大基础实用算法之-DFS(深度优先搜索)...
  2. 【玩转.Net MF – 03】远程文件查看器
  3. nginx重写rewrite的[emerg] unknown directive
  4. 加快发展设施业 农业大健康-林裕豪:从玉农业践行基础支撑
  5. countDownLatch 与 CyclicBarrier 区别
  6. c++primer书上习题date类的部分源代码(待整理)
  7. Linux网络编程 | 多路复用I/O :select、poll、epoll、水平触发与边缘触发、惊群问题
  8. Travis CI + github + hexo 自动化部署
  9. @RequestMapping 和 @GetMapping @PostMapping 区别
  10. 前端学习(1151):let经典面试题1
  11. mysql全文索引 插件,如何编写MySQL全文索引插件
  12. Kronecker 定理
  13. Delphi接入科大讯飞语音合成SDK
  14. 系统建模与仿真 - 电子书下载(高清版PDF格式+EPUB格式)
  15. Eclipse设置、调优、使用
  16. 100个开源C/C++项目中的bugs(一)数组和字符串处理的错误
  17. 手机构建Linux环境,Linux手机DIY.构建统一安装包
  18. 两层板(双面板)如何控制50欧特性阻抗
  19. java mysql 分区表_mysql 分区有实用价值吗?
  20. win10计算机管理 用户,Win10专业版系统管理员帐户的开启设置方法

热门文章

  1. aarch64-linux-gnu交叉编译器二进制方式安装
  2. 【C++】特殊类相关设计
  3. strcpy,strncpy和strncpy_s的区别 strncpy函数与memcpy函数
  4. 修改本地hosts文件,出现不能写只能读权限,近root账户,密码忘记怎么办?
  5. SecureCRT连接开发板
  6. 你可和差拨便去那里 视频转换软件
  7. 对io.UnsupportedOperation: fileno错误的解决办法
  8. 浅谈产业界与学术界的合作研究
  9. Oracle 10g 32位 下载地址
  10. Accumulation point