SpringSecurity|SpringSecurity oAuth2.0的四种模式(小结)
目录
- 1.1. 授权码授权模式(Authorization code Grant)
- 1.1.1. 流程图
- 1.1.2. 授权服务器配置配置
- 1.1.3. 资源服务器配置
- 1.1.5. 使用场景
- 1.2. 隐式授权模式(Implicit Grant)
- 1.2.1. 流程图
- 1.2.2. 改动 authorizedGrantTypes
- 1.2.3. 操作步骤
- 1.2.4. 使用场景
- 1.3. 密码模式(Resource Owner Password Credentials Grant)
- 1.3.1. 流程图
- 1.3.2. 改动
- 1.3.3. 操作步骤
- 1.3.4. 使用场景
- 1.4. 客户端凭证模式(Client Credentials Grant)
- 1.4.1. 流程图
- 1.4.2. 改动
- 1.4.3. 操作步骤
- 1.4.4. 使用场景
- 1.5. 刷新TOKEN
- 1.5.1. 流程图
- 1.5.2. 改动
1.1. 授权码授权模式(Authorization code Grant)
1.1.1. 流程图

文章图片
1.1.2. 授权服务器配置配置
授权服务器中 client,secret,redirectUri,授权模式,权限配置
//授权服务器配置@Configuration@EnableAuthorizationServerpublic class OAuth2AuthorizationServer extendsAuthorizationServerConfigurerAdapter {@Overridepublic void configure(ClientDetailsServiceConfigurer clients)throws Exception {clients.inMemory().withClient("clientapp").secret("112233").redirectUris("http://localhost:9001/callback")// 授权码模式.authorizedGrantTypes("authorization_code").scopes("read_userinfo", "read_contacts"); }}
1.1.3. 资源服务器配置
配置需要资源授权的接口地址
//资源服务配置@Configuration@EnableResourceServerpublic class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().requestMatchers().antMatchers("/api/**"); }}
1.1.4. 操作步骤
浏览器请求下列地址,获取授权code,请求参数client_id,redirect_uri回调地址,response_type响应类型,scope权限
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo输入用户名密码,该密码为Spring Security的登路密码,application.properties里配置

文章图片
# Spring Security Settingsecurity.user.name=bobosecurity.user.password=xyz
登陆后显示

文章图片
选择Approve,点击Authorize,会调用回调地址并返回code参数

文章图片
在获得授权码后,接下去获取访问令牌,访问
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

文章图片
注意:需要在headers里添加认证

文章图片
认证参数就是授权服务器配置的client和secret

文章图片
获取token后访问
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4

文章图片
如果token错误,则

文章图片
1.1.5. 使用场景
授权码模式是最常见的一种授权模式,在oauth2.0内是最安全和最完善的。适用于所有有Server端的应用,如Web站点、有Server端的手机客户端。可以得到较长期限授权。
1.2. 隐式授权模式(Implicit Grant)
1.2.1. 流程图

文章图片
1.2.2. 改动 authorizedGrantTypes
@Configuration@EnableAuthorizationServerpublic class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("clientapp").secret("112233").accessTokenValiditySeconds(60).redirectUris("http://localhost:9001/callback").authorizedGrantTypes("implicit").scopes("admin", "visitor"); }}
1.2.3. 操作步骤
申请授权token,参数和申请授权码类似,client_id,redirect_uri回调地址,response_type有变动,改为直接获取token,scope权限,state用于认证标记,传过去什么回调时传回来什么
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc

文章图片
操作同上,输入密码跳转认证确认,选Approve后点Authorize,跳转

文章图片
3. 可以看到直接返回了access_token,state也是原样返回
4. 之后按授权码模式第六步操作,把access_token参数带上,进行接口调用就可以了

文章图片
1.2.4. 使用场景
- 适用于所有无Server端配合的应用
- 如手机/桌面客户端程序、浏览器插件。
- 基于JavaScript等脚本客户端脚本语言实现的应用。
1.3. 密码模式(Resource Owner Password Credentials Grant)
1.3.1. 流程图

文章图片
1.3.2. 改动
授权服务器配置,需要添加用户认证管理端点authenticationManager,修改模式authorizedGrantTypes为password
// 授权服务器配置@Configuration@EnableAuthorizationServerpublic class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{@Autowiredprivate AuthenticationManager authenticationManager; @Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.authenticationManager(authenticationManager); }@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("clientapp").secret("112233").accessTokenValiditySeconds(60).redirectUris("http://localhost:9001/callback").authorizedGrantTypes("password").scopes("admin", "visitor"); }}
1.3.3. 操作步骤
调用以下链接,向客户端和服务器提供用户名密码
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin注意:和授权码模式一样,需要在headers里添加认证
结果:

文章图片
获取token后,步骤同1.1和1.2模式
1.3.4. 使用场景
- 这种模式适用于用户对应用程序高度信任的情况。比如是用户操作系统的一部分。
- 认证服务器只有在其他授权模式无法执行的情况下,才能考虑使用这种模式。
1.4. 客户端凭证模式(Client Credentials Grant)
1.4.1. 流程图

文章图片
1.4.2. 改动
只需修改授权服务器,authorizedGrantTypes类型client_credentials
1.4.3. 操作步骤
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin

文章图片
可以看到客户端凭证模式也需要在header里添加认证账户密码
获得token后操作同上
1.4.4. 使用场景
- 客户端模式应用于应用程序想要以自己的名义与授权服务器以及资源服务器进行互动。
- 例如使用了第三方的静态文件服务
1.5. 刷新TOKEN
1.5.1. 流程图

文章图片
【SpringSecurity|SpringSecurity oAuth2.0的四种模式(小结)】
1.5.2. 改动
1.5.3. 操作步骤
以授权码模式为例,步骤同授权码模式,取得授权码后,去取token时,返回

文章图片
在token过期后,调用
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2

文章图片
返回

文章图片
就可以拿新的access_token继续调用了建议将access_token和refresh_token的过期时间保存下来,每次调用平台方的业务api前先对access_token和refresh_token进行一下时间判断,如果过期则执行刷新access_token或重新授权操作。refersh_token如果过期就只能让用户重新授权。
参考 https://www.cnblogs.com/maoxiaolv/p/5838680.html
代码学习地址 https://github.com/spring2go/oauth2lab
到此这篇关于SpringSecurity oAuth2.0的四种模式(小结)的文章就介绍到这了,更多相关SpringSecurity oAuth2.0模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- C语言进阶数据的存储机制完整版
- Go|Go 结构体序列化的实现
- 《C++要笑着学》|【C++要笑着学】类和对象 | 初识封装 | 访问限定符 | 类的作用域和实例化 | 类对象模型 | this指针
- C++(类的成员函数定义方式)
- 数据库|《当程序员的那些狗日日子》五
- c++|c++类的默认成员函数
- JetBrains发布DataGrip 1.0——数据库与SQL领域中的瑞士军刀
- 极客日报|iOS 15.4发布,支持戴口罩解锁;JetBrains(无限期暂停在俄罗斯的销售和研发活动;Swift 5.6 发布|极客头条)
- 面试突击30(线程池是如何执行的(拒绝策略有哪些?))
- 聊聊如何让springboot拦截器的执行顺序按我们想要的顺序执行