oauth简单使用
一、oauth原理参考
二、本例中采用授权码模式
大致流程
(A)用户访问客户端,后者将前者导向认证服务器。
(B)用户选择是否给予客户端授权。
(C)假设用户给予授权,认证服务器将用户导向客户端事先指定的"重定向URI"(redirection URI),同时附上一个授权码。
(D)客户端收到授权码,附上早先的"重定向URI",向认证服务器申请令牌。这一步是在客户端的后台的服务器上完成的,对用户不可见。
(E)认证服务器核对了授权码和重定向URI,确认无误后,向客户端发送访问令牌(access token)和更新令牌(refresh token)。
参数含义
response_type:表示授权类型,必选项,此处的值固定为"code"
client_id:表示客户端的ID,必选项
redirect_uri:表示重定向URI,可选项
scope:表示申请的权限范围,可选项,本例中无
state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值,本例中无
三、项目中依赖oauth相关jar
<!-- oauth -->
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.resourceserver</artifactId>
<version>${oauth2-version}</version>
</dependency>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.authzserver</artifactId>
<version>${oauth2-version}</version>
</dependency>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>${oauth2-version}</version>
</dependency>
四、获取授权码
/**
* 获取授权码-服务端
*
* @param request
* @return
* @throws OAuthProblemException
* @throws OAuthSystemException
*/
@RequestMapping(value = "/authorize", method = RequestMethod.GET)
@ResponseBody
public Object authorize(HttpServletRequest request) throws URISyntaxException, OAuthProblemException, OAuthSystemException {
try {
// 构建OAuth授权请求
OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request); // 1.获取OAuth客户端id
String clientId = oauthRequest.getClientId();
// 校验客户端id是否正确
LightUserResult lightUserResult = userApi.queryUserByClientId(clientId);
if (null == lightUserResult) {
OAuthResponse response =
OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setError(OAuthError.TokenResponse.INVALID_CLIENT)
.setErrorDescription("无效的客户端ID")
.buildJSONMessage();
return new ResponseEntity(response.getBody(), HttpStatus.valueOf(response.getResponseStatus()));
} // 2.生成授权码
String authCode = null;
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
// ResponseType仅支持CODE和TOKEN
if (responseType.equals(ResponseType.CODE.toString())) {
OAuthIssuerImpl oAuthIssuer = new OAuthIssuerImpl(new MD5Generator());
authCode = oAuthIssuer.authorizationCode();
// 存入缓存中authCode-username
RedisUtil.getRedis().set(authCode, lightUserResult.getUserName());
}
return new ResponseEntity(authCode, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity("内部错误", HttpStatus.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
}
}
五、根据授权码获取token
/**
* 获取访问令牌
*
* @param request
* @return
* @throws OAuthProblemException
* @throws OAuthSystemException
*/
@RequestMapping(value = "accessToken", method = RequestMethod.POST)
@ResponseBody
public Object accessToken(HttpServletRequest request) throws OAuthProblemException, OAuthSystemException {
try {
// 构建OAuth请求
OAuthTokenRequest tokenRequest = new OAuthTokenRequest(request); // 1.获取OAuth客户端id
String clientId = tokenRequest.getClientId();
// 校验客户端id是否正确
LightUserResult lightUserResult = userApi.queryUserByClientId(clientId);
if (null == lightUserResult) {
OAuthResponse oAuthResponse = OAuthResponse
.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setError(OAuthError.TokenResponse.INVALID_CLIENT)
.setErrorDescription("无效的客户端ID")
.buildJSONMessage();
return new ResponseEntity(oAuthResponse.getBody(), HttpStatus.valueOf(oAuthResponse.getResponseStatus()));
} // 2.检查客户端安全key是否正确
if (!lightUserResult.getClientSecret().equals(tokenRequest.getClientSecret())) {
OAuthResponse oAuthResponse = OAuthResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setError(OAuthError.TokenResponse.UNAUTHORIZED_CLIENT)
.setErrorDescription("客户端安全key认证不通过")
.buildJSONMessage();
return new ResponseEntity<>(oAuthResponse.getBody(), HttpStatus.valueOf(oAuthResponse.getResponseStatus()));
} // 3.检查授权码是否正确
String authCode = tokenRequest.getParam(OAuth.OAUTH_CODE);
// 检查验证类型,此处只检查AUTHORIZATION_CODE类型,其他的还有password或REFRESH_TOKEN
if (!tokenRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.AUTHORIZATION_CODE.toString())) {
if (null == RedisUtil.getRedis().get(authCode)) {
OAuthResponse response = OAuthASResponse
.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setError(OAuthError.TokenResponse.INVALID_GRANT)
.setErrorDescription("授权码错误")
.buildJSONMessage();
return new ResponseEntity(response.getBody(), HttpStatus.valueOf(response.getResponseStatus())); }
} // 4.生成访问令牌Access Token
OAuthIssuer oAuthIssuer = new OAuthIssuerImpl(new MD5Generator());
final String accessToken = oAuthIssuer.accessToken();
// 将访问令牌加入缓存:accessToken-username
RedisUtil.getRedis().set(accessToken, lightUserResult.getUserName()); // 5.生成OAuth响应
OAuthResponse response = OAuthASResponse
.tokenResponse(HttpServletResponse.SC_OK)
.setAccessToken(accessToken)
.setExpiresIn(expiresIn)
.buildJSONMessage(); return new ResponseEntity(response.getBody(), HttpStatus.valueOf(response.getResponseStatus()));
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity("内部错误", HttpStatus.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
} }
六、简单测试
@RequestMapping("authority")
@ResponseBody
public JSONObject authority() throws OAuthSystemException, OAuthProblemException {
JSONObject result = new JSONObject();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthClientRequest codeTokenRequest = OAuthClientRequest
.authorizationLocation("http://127.0.0.1:8080/auth-web/oauth/authorize")
.setResponseType(ResponseType.CODE.toString())
.setClientId("c1ebe466-1cdc-4bd3-ab69-77c3561b9dee")
.buildQueryMessage();
//获取 code
OAuthResourceResponse codeResponse = oAuthClient.resource(
codeTokenRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
if(codeResponse.getResponseCode() != HttpServletResponse.SC_OK) {
result.put("code", codeResponse.getResponseCode());
result.put("msg", codeResponse.getBody());
} else {
String authCode = codeResponse.getBody();
OAuthClientRequest accessTokenRequest = OAuthClientRequest
.tokenLocation("http://127.0.0.1:8080/auth-web/oauth/accessToken")
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId("c1ebe466-1cdc-4bd3-ab69-77c3561b9dee").setClientSecret("d8346ea2-6017-43ed-ad68-19c0f971738b")
.setCode(authCode).setRedirectURI("http://127.0.0.1:8080/auth-web/")
.buildQueryMessage();
//获取access token
OAuthAccessTokenResponse tokenResponse =
oAuthClient.accessToken(accessTokenRequest, OAuth.HttpMethod.POST);
if(tokenResponse.getResponseCode() != HttpServletResponse.SC_OK) {
result.put("code", tokenResponse.getResponseCode());
result.put("msg", tokenResponse.getBody());
return result;
} else {
//验证token
OAuthClientRequest validateRequest =
new OAuthBearerClientRequest("http://127.0.0.1:8080/auth-web/oauth/validate")
.setAccessToken(tokenResponse.getAccessToken()).buildQueryMessage();
OAuthResourceResponse validateResponse = oAuthClient.resource(
validateRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
if(validateResponse.getResponseCode() != HttpServletResponse.SC_OK) {
result.put("code", validateResponse.getResponseCode());
result.put("msg", validateResponse.getBody());
} else {
JSONObject body = JSON.parseObject(validateResponse.getBody());
result.put("code", body.getString("code"));
result.put("msg", body.getString("msg"));
}
}
}
return result;
}
public static ResponseEntity oauthValidate(HttpServletRequest request) throws OAuthProblemException, OAuthSystemException { // 构建OAuth资源请求
OAuthAccessResourceRequest resourceRequest = new OAuthAccessResourceRequest(request, ParameterStyle.QUERY);
// 获取访问令牌access Token
String accessToken = resourceRequest.getAccessToken();
// 验证访问令牌
if (null == RedisUtil.getRedis().get(accessToken)) {
// 如果不存在或过期了,返回未验证错误,需重新验证
OAuthResponse response = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setError(OAuthError.ResourceResponse.INVALID_TOKEN)
.setErrorDescription("访问令牌不存在或已过期,请重新验证")
.buildJSONMessage();
return new ResponseEntity(response.getBody(), HttpStatus.valueOf(response.getResponseStatus()));
}
return new ResponseEntity("验证成功", HttpStatus.valueOf(HttpServletResponse.SC_OK));
}
七、项目地址
oauth简单使用的更多相关文章
- 基于 OAuth 安全协议的 Java 应用编程1
原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-oauth/index.html 參考博客:http://www.cnblogs.com/wan ...
- 单点登录 之 OAuth
OAuth2.0是什么 OAuth2.0是什么——豆瓣和QQ的故事 OAuth简单说就是一种授权的协议,只要授权方和被授权方遵守这个协议去写代码提供服务,那双方就是实现了OAuth模式. 举个例子,你 ...
- 简单搞懂OAuth2.0
本文转自:https://www.cnblogs.com/flashsun/p/7424071.html 原作者:闪客sun 一张图搞定OAuth2.0 目录 1.引言 2.OAuth2.0是什么 3 ...
- 秒懂OAuth2.0
1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常简单的一件事情,网上一堆神乎其神的讲解,让我不得不写一篇文章来终结它们. 一项新的技术,无非就是了解它是什么,为什 ...
- 一张图搞定OAuth2.0
1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常简单的一件事情,网上一堆神乎其神的讲解,让我不得不写一篇文章来终结它们. 一项新的技术,无非就是了解它是什么,为什 ...
- 2018/04/14 理解oAuth2.0
最近都近没有更新博客了,卡在 oAuth 上了. 之前公司做统一身份的认证,不了解 oAuth 的我在这卡了两天. 于是决定仔细研究原理,理论指导实践. -- 什么是 oAuth ? 简单来说 oAu ...
- 单点登录系统cas资料汇总
http://jasig.github.io/cas/4.0.x/index.html 主页 https://jasigcas.herokuapp.com ...
- 一张图搞定OAuth2.0 在Office应用中打开WPF窗体并且让子窗体显示在Office应用上 彻底关闭Excle进程的几个方法 (七)Net Core项目使用Controller之二
一张图搞定OAuth2.0 目录 1.引言 2.OAuth2.0是什么 3.OAuth2.0怎么写 回到顶部 1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常 ...
- Auth2.0 例子【转载】
本文转载自:https://www.cnblogs.com/flashsun/p/7424071.html 1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常简单的 ...
随机推荐
- ionic2 自定义cordova插件开发以及使用 (Android)
如何写一个cordova 用于ionic2项目中呢,在搜索了一番之后,千篇一律,我都怀疑那些文章是不是全部都是复制来复制去的,而且都不是很详细.我自己也捣鼓了一下午,踩了很多坑.所以特此写这下这篇,记 ...
- centos6.5 修改java环境变量
[root@m1 ~]# cat /etc/profile export JAVA_HOME=/usr/local/soft/jdkexport PATH=$JAVA_HOME/bin:$PATH e ...
- python 字符串常用方法
字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...
- python 2.7中urllib 2 与python 3.5中 urllib的区别。
python 3.x中urllib库和urilib2库合并成了urllib库. 其中urllib2.urlopen()变成了urllib.request.urlopen() urllib2.Reque ...
- blog界面自己写了css,参考了网站设计,想要的自己拿
junhey这就把界面的代码公布下来,可以自己修改额~(ps:麻烦加个友链http://www.cnblogs.com/junhey/ 谢谢) /* 初始化样式 */ html, body, div, ...
- CentOS上安装redis记录
下载稳定版 curl -O http://download.redis.io/releases/redis-stable.tar.gz tar -zxvf redis-stable.tar.gz cd ...
- CentOS 安装数据库笔记
1.配置YUM源 # 下载mysql源安装包 shell.noarch.rpm # 安装mysql源 shell.noarch.rpm 检查mysql源是否安装成功 shell> yum rep ...
- C# 调用cmd.exe的方法
网上有很多用C#调用cmd的方法,大致如下: [c-sharp] view plaincopy private void ExecuteCmd(string command) { Proces ...
- 删除iPhone图片,提示“没有删除此项目的权限”
解决方法:设置-照片与相机-iCloud照片图库-关闭 (IOS10)
- vue组件(Vue+webpack项目实战系列之三)
组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.特别对于大型应用开发来说,尽量组件化,并且先造好轮子库,不要重复去写组件,这会显著提升项目 ...