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