当前位置:   article > 正文

Spring Cloud集成Oauth2踩坑:报401错/Unsupported grant type_unsupported_grant_type

unsupported_grant_type

目录

关键代码:

问题1:授权码模式报401

错误截图:unauthrized:

解决方案如下:

问题2:授权码模式报密码模式时报:o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: UnsupportedGrantTypeException, Unsupported grant type

错误截图: 

解决方案如下:


在springcloud集成Oauth2的时候调用/oauth/token去获取token时密码模式和授权模式分别报错报401和Unsupported grant type

关键代码:

  1. @Configuration
  2. @EnableWebSecurity
  3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Bean
  5. public BCryptPasswordEncoder passwordEncoder(){
  6. return new BCryptPasswordEncoder();
  7. }
  8. @Override
  9. public void configure(AuthenticationManagerBuilder auth) throws Exception {
  10. auth.inMemoryAuthentication()
  11. .withUser("admin")
  12. .password(passwordEncoder().encode("123456"))
  13. .roles("USER","ADMIN")
  14. .authorities(AuthorityUtils.commaSeparatedStringToAuthorityList("p1,p2"));
  15. //这里配置全局用户信息
  16. }
  17. }
  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  4. @Autowired
  5. PasswordEncoder passwordEncoder;
  6. @Override
  7. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  8. //基于内存便于测试
  9. clients.inMemory()// 使用in-memory存储
  10. .withClient("auth")// client_id
  11. //.secret("secret")//未加密
  12. .secret(this.passwordEncoder.encode("secret"))//加密
  13. //.resourceIds("res1")//资源列表
  14. .authorizedGrantTypes("authorization_code", "password", "client_credentials", "implicit", "refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials
  15. .scopes("all", "ROLE_ADMIN", "ROLE_USER")// 允许的授权范围
  16. //.autoApprove(false)//false跳转到授权页面
  17. //加上验证回调地址
  18. .redirectUris("http://baidu.com");
  19. }
  20. @Override
  21. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  22. security.allowFormAuthenticationForClients()
  23. .passwordEncoder(new BCryptPasswordEncoder()); //2.第二处
  24. }
  25. }

问题1:授权码模式报401

错误截图:unauthrized:

解决方案如下:

反复检查了我的代码和参数,察觉到我的请求都没有到达接口就已经返回了401的错误,那么一定是我的参数有问题,检查了不下十遍,在这里卡了两天,终于发现自己踩坑在哪里!!!朋友们authorization里面的参数不是账号密码啊。。。是代码里设置的client_id和secret !!!

问题2:授权码模式报密码模式时报:o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: UnsupportedGrantTypeException, Unsupported grant type

错误截图: 

解决方案如下:

一直认为是我的grant_type设置不对,去打断点的时候一直只能获取到authorization_code一个,而找不到列表里面的password和我的参数匹配,就反应过来应该是哪里少了配置,查阅资料才发现需要加上AuthenticationManager

需要在 AuthorizationServerConfig中加上如下代码:

  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  4. @Autowired
  5. AuthenticationManager authenticationManager;
  6. @Override
  7. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  8. super.configure(endpoints);
  9. endpoints.authenticationManager(authenticationManager);
  10. }
  11. }

如果出现启动时找不到AuthenticationManager这个bean,需要去WebSecurityConfig里实例化一下

  1. /**
  2. * 重新实例化bean
  3. */
  4. @Override
  5. @Bean
  6. public AuthenticationManager authenticationManagerBean() throws Exception {
  7. return super.authenticationManagerBean();
  8. }

 加上之后成功获取到

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/277249
推荐阅读
相关标签
  

闽ICP备14008679号