基于授权码模式去修改:
https://www.yuque.com/docs/share/27959146-cdc4-43ec-a96a-c0969e1ac2d7?# 《授权码模式入门Demo》

代码地址

https://gitee.com/zjj19941/ZJJ_Neaten5.10/tree/master/ZJJ_SpringCloud_Oauth2/demo02

代码

修改WebSecurityConfig,增加AuthenticationManager

  1. @Configuration
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Bean
  4. public PasswordEncoder passwordEncoder(){
  5. return new BCryptPasswordEncoder();
  6. }
  7. @Override
  8. protected void configure(HttpSecurity http) throws Exception {
  9. http.formLogin().permitAll()
  10. .and().authorizeRequests()
  11. .antMatchers("/oauth/**").permitAll()
  12. .anyRequest().authenticated()
  13. .and().logout().permitAll()
  14. .and().csrf().disable();
  15. }
  16. @Bean
  17. @Override
  18. public AuthenticationManager authenticationManagerBean() throws Exception {
  19. return super.authenticationManagerBean();
  20. }
  21. }

修改AuthorizationServerConfig配置

  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig2 extends AuthorizationServerConfigurerAdapter {
  4. @Autowired
  5. private PasswordEncoder passwordEncoder;
  6. @Autowired
  7. private AuthenticationManager authenticationManagerBean;
  8. @Override
  9. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  10. endpoints.authenticationManager(authenticationManagerBean) //使用密码模式需要配置
  11. .allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST); //支持GET,POST请求
  12. }
  13. @Override
  14. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  15. //允许表单认证
  16. security.allowFormAuthenticationForClients();
  17. }
  18. @Override
  19. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  20. /**
  21. *授权码模式
  22. *http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://www.baidu.com&scope=all
  23. *http://localhost:8080/oauth/authorize?response_type=code&client_id=client
  24. *
  25. * password模式
  26. * http://localhost:8080/oauth/token?username=fox&password=123456&grant_type=password&client_id=client&client_secret=123123&scope=all
  27. *
  28. * 客户端模式
  29. * http://localhost:8080/oauth/token?grant_type=client_credentials&scope=all&client_id=client&client_secret=123123
  30. */
  31. clients.inMemory()
  32. //配置client_id
  33. .withClient("client")
  34. //配置client-secret
  35. .secret(passwordEncoder.encode("123123"))
  36. //配置访问token的有效期
  37. .accessTokenValiditySeconds(3600)
  38. //配置刷新token的有效期
  39. .refreshTokenValiditySeconds(864000)
  40. //配置redirect_uri,用于授权成功后跳转
  41. .redirectUris("http://www.baidu.com")
  42. //配置申请的权限范围
  43. .scopes("all")
  44. /**
  45. * 配置grant_type,表示授权类型
  46. * authorization_code: 授权码
  47. * password: 密码
  48. * client_credentials: 客户端
  49. */
  50. .authorizedGrantTypes("authorization_code","password","client_credentials");
  51. }
  52. }

测试

get请求获取access_token

通过浏览器测试,需要配置支持get请求和表单验证

http://localhost:8080/oauth/token?username=fox&password=123456&grant_type=password&client_id=client&client_secret=123123&scope=all

返回结果直接带access_token
image.png

{
“access_token”: “5370d3af-78c8-4c90-8e65-617ca4714d7c”,
“token_type”: “bearer”,
“expires_in”: 3040,
“scope”: “all”
}

post请求获取access_token

http://localhost:8080/oauth/token

image.png
image.png

grant_type:password
username:fox
password:123456

结果:
image.png

{
“access_token”: “5370d3af-78c8-4c90-8e65-617ca4714d7c”,
“token_type”: “bearer”,
“expires_in”: 2908,
“scope”: “all”
}

访问接口

image.png