3.授权码模式

时间:2021-03-18 10:05:49   收藏:0   阅读:31

1.先看下授权服务器的配置类

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients
        //存内存中
        .inMemory()
        //客户端id
        .withClient("client")
        //客户端密码
        .secret(passwordEncoder.encode("123456"))
        //重定向地址
        .redirectUris("http://www.baidu.com")
        //范围
        .scopes("all")
        //授权类型:授权码模式
        .authorizedGrantTypes("authorization_code");
  }

  authorization_code这个就是授权码模式对应的code,还有其他模式的code后续会有

2.启动项目后浏览器输入

http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://www.baidu.com&scope=all

1)接口/oauth/authorize是框架的,必须是它

2)response_type=code代表授权码模式

3)client_id,redirect_uri,scope这三个和配置里要保持一致

3.登录系统

技术分享图片

 

这里的用户名密码是我们配置userDetailServcie里面的,而不是什么client_id

 4.登录成功后得到授权码

技术分享图片

 

 5.用授权码去获取token,这里要使用postman

技术分享图片

 

 技术分享图片

 

 注意:这是一次请求的两个tab页,通过这个拿到access_token

6.用得到的token去访问资源

技术分享图片

 

 总结:整个过程和本专题第一篇文章里面的过程一模一样,授权码模式是oauth2里面最复杂的一种,也是最安全的一种。

原文:https://www.cnblogs.com/johnzhao/p/14553020.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!