实现OAuth2.0服务端【授权码模式(Authorization Code)】
2016-06-07 11:02
921 查看
要实现OAuth服务端,就得先理解客户端的调用流程,服务提供商实现可能也有些区别,实现OAuth服务端的方式很多,具体可能看http://oauth.net/code/
各语言的实现有(我使用了Apache Oltu):
Java
Apache
Oltu
Spring
Security for OAuth
Apis
Authorization Server (v2-31)
Restlet
Framework (draft 30)
Apache
CXF
NodeJS
NodeJS
OAuth 2.0 Provider
Mozilla
Firefox Accounts. A full stack Identy Provider system developed to support Firefox market place and other services
Ruby
Ruby
OAuth2 Server (draft 18)
.NET
.NET
DotNetOpenAuth
Thinktecture
IdentityServer
实现主要涉及参数配置如下:
授权码设置(code)
第三方通过code进行获取 access_token的时候需要用到,code的超时时间为10分钟,一个code只能成功换取一次access_token即失效。
授权作用域(scope)
作用域代表用户授权给第三方的接口权限,第三方应用需要向服务端申请使用相应scope的权限后,经过用户授权,获取到相应access_token后方可对接口进行调用。
令牌有效期(access_token)
access_token是调用授权关系接口的调用凭证,由于access_token有效期(目前为2个小时)较短,当access_token超时后,可以使用refresh_token进行刷新,access_token刷新结果有两种:
1. 若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;
2. 若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
项目结构如下:
AuthzController:获取授权码
TokenController:获得令牌
ResourceController:资源服务
ClientController:客户端
基础代码放到github:https://github.com/zhouyongtao/homeinns-web
确保项目8080端口运行,可以手动调试
获得授权码
http://localhost:8080/oauth2/authorize?client_id=fbed1d1b4b1449daa4bc49397cbe2350&response_type=code&redirect_uri=http://localhost:8080/oauth_callback
获得令牌(POST)
http://localhost:8080/oauth2/access_token?client_id=fbed1d1b4b1449daa4bc49397cbe2350&client_secret=fbed1d1b4b1449daa4bc49397cbe2350&grant_type=authorization_code&redirect_uri=http://localhost:8080/oauth_callback&code={code}
也可以使用如下客户端测试代码,访问 http://localhost:8080/client 测试
Refer:
https://cwiki.apache.org/confluence/display/OLTU/Index
https://open.weixin.qq.com/cgi-bin/readtemplate?t=resource/app_wx_login_tmpl&lang=zh_CN#faq
各语言的实现有(我使用了Apache Oltu):
Java
Apache
Oltu
Spring
Security for OAuth
Apis
Authorization Server (v2-31)
Restlet
Framework (draft 30)
Apache
CXF
NodeJS
NodeJS
OAuth 2.0 Provider
Mozilla
Firefox Accounts. A full stack Identy Provider system developed to support Firefox market place and other services
Ruby
Ruby
OAuth2 Server (draft 18)
.NET
.NET
DotNetOpenAuth
Thinktecture
IdentityServer
实现主要涉及参数配置如下:
授权码设置(code)
第三方通过code进行获取 access_token的时候需要用到,code的超时时间为10分钟,一个code只能成功换取一次access_token即失效。
授权作用域(scope)
作用域代表用户授权给第三方的接口权限,第三方应用需要向服务端申请使用相应scope的权限后,经过用户授权,获取到相应access_token后方可对接口进行调用。
令牌有效期(access_token)
access_token是调用授权关系接口的调用凭证,由于access_token有效期(目前为2个小时)较短,当access_token超时后,可以使用refresh_token进行刷新,access_token刷新结果有两种:
1. 若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;
2. 若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
项目介绍
项目结构如下:AuthzController:获取授权码
TokenController:获得令牌
ResourceController:资源服务
ClientController:客户端
基础代码放到github:https://github.com/zhouyongtao/homeinns-web
确保项目8080端口运行,可以手动调试
获得授权码
http://localhost:8080/oauth2/authorize?client_id=fbed1d1b4b1449daa4bc49397cbe2350&response_type=code&redirect_uri=http://localhost:8080/oauth_callback
获得令牌(POST)
http://localhost:8080/oauth2/access_token?client_id=fbed1d1b4b1449daa4bc49397cbe2350&client_secret=fbed1d1b4b1449daa4bc49397cbe2350&grant_type=authorization_code&redirect_uri=http://localhost:8080/oauth_callback&code={code}
客户端
也可以使用如下客户端测试代码,访问 http://localhost:8080/client 测试https://cwiki.apache.org/confluence/display/OLTU/Index
https://open.weixin.qq.com/cgi-bin/readtemplate?t=resource/app_wx_login_tmpl&lang=zh_CN#faq
相关文章推荐
- Java中BigDecimal的8种舍入模式
- web服务经常瘫痪
- 模板方法模式(Template Method)
- 测试管理_漏测的原因分析和改进措施
- bitmap 内存溢出OOM的解决办法分享
- 理解Java中的弱引用(Weak Reference)
- 计算机网络体系结构
- 查找函数的调用关系
- 画廊etiennelawlor imagegallery真正全屏,使用隐藏状态栏和沉浸式浮动Toolbar
- C++程序设计语言练习6.14 字符串反转
- jQuery插件实现“点击获取验证码后60秒内禁止重新获取(防刷新)”
- jquery 如何监听div内容的变化
- HTML5 data-* 自定义属性
- LOL中的Lissandra怎么通过ZBrush绘制出来
- Java基础第十五天总结
- Hibernate many-to-many
- 详解ssh/https远程连接Git服务器
- java web 设置持久session
- Java Map获取key和value
- javascript之Array 数组对象详解