Java的oauth2.0 服务端与客户端的实现
oauth原理简述
oauth本身不是技术,而是一项资源授权协议,重点是协议!Apache基金会提供了针对Java的oauth封装。我们做Java web项目想要实现oauth协议进行资源授权访问,直接使用该封装就可以。
想深入研究原理的 可以参考:阮一峰的博客以及张开涛的博客
借用开涛老师一张图,就是整个oauth2.0 的协议实现原理,所有的技术层面的开发都是围绕这张图。
整个开发流程简述一下:
1、 在客户端web项目中构造一个oauth的客户端请求对象(OAuthClientRequest),在此对象中携带客户端信息(clientId、accessTokenUrl、response_type、redirectUrl),将此信息放入http请求中,重定向到服务端。此步骤对应上图1
2、 在服务端web项目中接受第一步传过来的request,从中获取客户端信息,可以自行验证信息的可靠性。同时构造一个oauth的code授权许可对象(OAuthAuthorizationResponseBuilder),并在其中设置授权码code,将此对象传回客户端。此步骤对应上图2
3、 在在客户端web项目中接受第二步的请求request,从中获得code。同时构造一个oauth的客户端请求对象(OAuthClientRequest),此次在此对象中不仅要携带客户端信息(clientId、accessTokenUrl、clientSecret、GrantType、redirectUrl),还要携带接受到的code。再构造一个客户端请求工具对象(oAuthClient),这个工具封装了httpclient,用此对象将这些信息以post(一定要设置成post)的方式请求到服务端,目的是为了让服务端返回资源访问令牌。此步骤对应上图3。(另外oAuthClient请求服务端以后,会自行接受服务端的响应信息。
4、 在服务端web项目中接受第三步传过来的request,从中获取客户端信息和code,并自行验证。再按照自己项目的要求生成访问令牌(accesstoken),同时构造一个oauth响应对象(OAuthASResponse),携带生成的访问指令(accesstoken),返回给第三步中客户端的oAuthClient。oAuthClient接受响应之后获取accesstoken,此步骤对应上图4
5、 此时客户端web项目中已经有了从服务端返回过来的accesstoken,那么在客户端构造一个服务端资源请求对象(OAuthBearerClientRequest),在此对象中设置服务端资源请求URI,并携带上accesstoken。再构造一个客户端请求工具对象(oAuthClient),用此对象去服务端靠accesstoken换取资源。此步骤对应上图5
6、 在服务端web项目中接受第五步传过来的request,从中获取accesstoken并自行验证。之后就可以将客户端请求的资源返回给客户端了。
代码:
客户端:
一、pom依赖:
<dependency> <groupId>org.apache.oltu.oauth2</groupId> <artifactId>org.apache.oltu.oauth2.client</artifactId> <version>0.31</version> </dependency> |
二、controller方法:
2.1 向服务端请求授权码code的controller方法:
@RequestMapping("/server") @Controller public class ServerController{ String clientId = null; String clientSecret = null; String accessTokenUrl = null; String userInfoUrl = null; String redirectUrl = null; String response_type = null; String code= null; //提交申请code的请求 @RequestMapping("/requestServerCode") public String requestServerFirst(HttpServletRequestrequest, HttpServletResponseresponse, RedirectAttributesattr) throws OAuthProblemException{ clientId = "clientId"; clientSecret = "clientSecret"; accessTokenUrl = "responseCode"; redirectUrl = "http://localhost:8081/oauthclient01/server/callbackCode"; response_type = "code"; OAuthClient oAuthClient =new OAuthClient(new URLConnectionClient()); String requestUrl = null; try { //构建oauthd的请求。设置请求服务地址(accessTokenUrl)、clientId、response_type、redirectUrl OAuthClientRequest accessTokenRequest = OAuthClientRequest .authorizationLocation(accessTokenUrl) .setResponseType(response_type) .setClientId(clientId) .setRedirectURI(redirectUrl) .buildQueryMessage(); requestUrl = accessTokenRequest.getLocationUri(); System.out.println(requestUrl); } catch (OAuthSystemExceptione) { e.printStackTrace(); } return "redirect:http://localhost:8082/oauthserver/"+requestUrl ; } |
此段代码对应开发步骤1.其中accessTokenUrl是服务端返回code的controller方法映射地址。redirectUrl是告诉服务端,code要传回客户端的一个controller方法,该方法的映射地址就是redirectUrl。
2.2 向服务端请求资源访问令牌access token的controller方法:
//接受客户端返回的code,提交申请access token的请求 @RequestMapping("/callbackCode") public Object toLogin(HttpServletRequestrequest)throws OAuthProblemException{ System.out.println("-----------客户端/callbackCode--------------------------------------------------------------------------------"); clientId = "clientId"; clientSecret = "clientSecret"; accessTokenUrl="http://localhost:8082/oauthserver/responseAccessToken"; userInfoUrl = "userInfoUrl"; redirectUrl = "http://localhost:8081/oauthclient01/server/accessToken"; HttpServletRequest httpRequest = (HttpServletRequest)request; code = httpRequest.getParameter("code"); System.out.println(code); OAuthClient oAuthClient =new OAuthClient(new URLConnectionClient()); try { OAuthClientRequest accessTokenRequest = OAuthClientRequest .tokenLocation(accessTokenUrl) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId) .setClientSecret(clientSecret) .setCode(code) .setRedirectURI(redirectUrl) .buildQueryMessage(); //去服务端请求access token,并返回响应 OAuthAccessTokenResponse oAuthResponse =oAuthClient.accessToken(accessTokenRequest, OAuth.HttpMethod.POST); //获取服务端返回过来的access token String accessToken = oAuthResponse.getAccessToken(); //查看access token是否过期 Long expiresIn =oAuthResponse.getExpiresIn(); System.out.println("客户端/callbackCode方法的token:::"+accessToken); System.out.println("-----------客户端/callbackCode--------------------------------------------------------------------------------"); return"redirect:http://localhost:8081/oauthclient01/server/accessToken?accessToken="+accessToken; } catch (OAuthSystemExceptione) { e.printStackTrace(); } return null; } |
此方法对应开发步骤3的全部和步骤4的一半,也就是还包括接受服务端返回的access token。最后的redirect地址不是服务端的地址,只是将此token传进客户端的另一个方法,该方法就是最后的资源请求方法。
2.3 利用服务端给的token去请求服务端的资源的controller方法。这里说的资源就是服务端数据库中的user表的uname值的拼接字段。
//接受服务端传回来的access token,由此token去请求服务端的资源(用户信息等) @RequestMapping("/accessToken") public ModelAndView accessToken(StringaccessToken) { System.out.println("---------客户端/accessToken----------------------------------------------------------------------------------"); userInfoUrl = "http://localhost:8082/oauthserver/userInfo"; System.out.println("accessToken"); OAuthClient oAuthClient =new OAuthClient(new URLConnectionClient()); try { OAuthClientRequest userInfoRequest =new OAuthBearerClientRequest(userInfoUrl) .setAccessToken(accessToken).buildQueryMessage(); OAuthResourceResponse resourceResponse =oAuthClient.resource(userInfoRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class); String username = resourceResponse.getBody(); System.out.println(username); ModelAndView modelAndView =new ModelAndView("usernamePage"); modelAndView.addObject("username",username); System.out.println("---------客户端/accessToken----------------------------------------------------------------------------------"); returnmodelAndView; } catch (OAuthSystemExceptione) { e.printStackTrace(); } catch (OAuthProblemExceptione) { e.printStackTrace(); } System.out.println("---------客户端/accessToken----------------------------------------------------------------------------------"); return null; } |
此方法对应开发步骤5的全部和步骤6的一半,也就是还包括接受服务端返回的资源信息。获取了资源信息之后,其余的开发就和平时的springmvc一毛一样了。
以上三个方法我全部封装在同一个ServerController类中。
服务端
三 pom依赖
1. <dependency> 2. <groupId>org.apache.oltu.oauth2</groupId> 3. <artifactId>org.apache.oltu.oauth2.authzserver</artifactId> 4. <version>0.31</version> 5. </dependency> 6. <dependency> 7. <groupId>org.apache.oltu.oauth2</groupId> 8. <artifactId>org.apache.oltu.oauth2.resourceserver</artifactId> 9. <version>0.31</version> 10. </dependency> |
四 controller方法
4.1 向客户端返回授权码code的controller方法
@Controller public class AuthorizeController{ @Autowired private UserServiceuserService; //向客户端返回授权许可码 code @RequestMapping("/responseCode") public Object toShowUser(Modelmodel, HttpServletRequestrequest){ System.out.println("----------服务端/responseCode--------------------------------------------------------------"); try { //构建OAuth授权请求 OAuthAuthzRequest oauthRequest =new OAuthAuthzRequest(request); /*oauthRequest.getClientId(); oauthRequest.getResponseType(); oauthRequest.getRedirectURI(); System.out.println(oauthRequest.getClientId()); System.out.println(oauthRequest.getResponseType()); System.out.println(oauthRequest.getRedirectURI());*/ if(oauthRequest.getClientId()!=null&&oauthRequest.getClientId()!="") { //设置授权码 String authorizationCode ="authorizationCode"; //利用oauth授权请求设置responseType,目前仅支持CODE,另外还有TOKEN String responseType =oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE); //进行OAuth响应构建 OAuthASResponse.OAuthAuthorizationResponseBuilderbuilder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND); //设置授权码 builder.setCode(authorizationCode); //得到到客户端重定向地址 String redirectURI =oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI); //构建响应 final OAuthResponseresponse =builder.location(redirectURI).buildQueryMessage(); System.out.println("服务端/responseCode内,返回的回调路径:"+response.getLocationUri()); System.out.println("----------服务端/responseCode--------------------------------------------------------------"); String responceUri =response.getLocationUri(); //根据OAuthResponse返回ResponseEntity响应 HttpHeaders headers =new HttpHeaders(); try { headers.setLocation(new URI(response.getLocationUri())); } catch (URISyntaxExceptione) { // TODO Auto-generated catch block e.printStackTrace(); } return"redirect:"+responceUri; } } catch (OAuthSystemExceptione) { e.printStackTrace(); } catch (OAuthProblemExceptione) { e.printStackTrace(); } System.out.println("----------服务端/responseCode--------------------------------------------------------------"); return null; } } |
此段代码对应开发步骤2
4.2 向客户端返回资源访问令牌accesstoken的controller方法
@Controller public class AccessTokenController { //获取客户端的code码,向客户端返回access token @RequestMapping(value="/responseAccessToken",method = RequestMethod.POST) public HttpEntity token(HttpServletRequest request){ System.out.println("--------服务端/responseAccessToken-----------------------------------------------------------"); OAuthIssuer oauthIssuerImpl=null; OAuthResponse response=null; //构建OAuth请求 try { OAuthTokenRequest oauthRequest =new OAuthTokenRequest(request); String authCode =oauthRequest.getParam(OAuth.OAUTH_CODE); String clientSecret = oauthRequest.getClientSecret(); if(clientSecret!=null||clientSecret!=""){ //生成Access Token oauthIssuerImpl =new OAuthIssuerImpl(new MD5Generator()); final StringaccessToken =oauthIssuerImpl.accessToken(); System.out.println(accessToken); System.out.println("--oooo---"); //生成OAuth响应 response = OAuthASResponse .tokenResponse(HttpServletResponse.SC_OK) .setAccessToken(accessToken) .buildJSONMessage(); } System.out.println("--------服务端/responseAccessToken-----------------------------------------------------------"); //根据OAuthResponse生成ResponseEntity return new ResponseEntity(response.getBody(), HttpStatus.valueOf(response.getResponseStatus())); } catch (OAuthSystemExceptione) { // TODO Auto-generated catch block e.printStackTrace(); } catch (OAuthProblemExceptione) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("--------服务端/responseAccessToken-----------------------------------------------------------"); return null; } } |
此段代码对应开发步骤4的前面一半,即服务端验证code、生成token并给客户端
4.3 向客户端返回请求资源(username)的controller方法
@Controller public class UserInfoController { @Autowired private UserServiceuserService; @RequestMapping("/userInfo") public HttpEntity userInfo(HttpServletRequest request)throws OAuthSystemException{ System.out.println("-----------服务端/userInfo-------------------------------------------------------------"); try { //获取客户端传来的OAuth资源请求 OAuthAccessResourceRequest oauthRequest =new OAuthAccessResourceRequest(request, ParameterStyle.QUERY); //获取Access Token String accessToken =oauthRequest.getAccessToken(); System.out.println("accessToken"); //验证Access Token /*if (accessToken==null||accessToken=="") { // 如果不存在/过期了,返回未验证错误,需重新验证 OAuthResponse oauthResponse = OAuthRSResponse .errorResponse(HttpServletResponse.SC_UNAUTHORIZED) .setError(OAuthError.ResourceResponse.INVALID_TOKEN) .buildHeaderMessage(); HttpHeaders headers = new HttpHeaders(); headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE)); return new ResponseEntity(headers, HttpStatus.UNAUTHORIZED); } */ //返回用户名 User user=userService.selectByPrimaryKey(1); String username = accessToken+"---"+Math.random()+"----"+user.getUname(); System.out.println(username); System.out.println("服务端/userInfo::::::ppp"); System.out.println("-----------服务端/userInfo----------------------------------------------------------"); return new ResponseEntity(username, HttpStatus.OK); } catch (OAuthProblemExceptione) { // TODO Auto-generated catch block e.printStackTrace(); //检查是否设置了错误码 String errorCode =e.getError(); if (OAuthUtils.isEmpty(errorCode)) { OAuthResponse oauthResponse = OAuthRSResponse .errorResponse(HttpServletResponse.SC_UNAUTHORIZED) .buildHeaderMessage(); HttpHeaders headers =new HttpHeaders(); headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE)); return new ResponseEntity(headers, HttpStatus.UNAUTHORIZED); } OAuthResponse oauthResponse = OAuthRSResponse .errorResponse(HttpServletResponse.SC_UNAUTHORIZED) .setError(e.getError()) .setErrorDescription(e.getDescription()) .setErrorUri(e.getUri()) .buildHeaderMessage(); HttpHeaders headers =new HttpHeaders(); headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE)); System.out.println("-----------服务端/userInfo------------------------------------------------------------------------------"); return new ResponseEntity(HttpStatus.BAD_REQUEST); } } } |
此代码对应开发步骤6的前一半。即服务端验证access token、并将资源信息给客户端
至此,整个Java集成oauth就完成了。
另外:需要验证的客户端信息,如clientId、clientSecret都是自行指定,与自己的项目相关,同时客户端信息的验证方法也是依情况而定,没有什么具体标准,我的demo里为了方便,基本上省略了客户端信息验证,都是默认合法。但是accessTokenUrl、userInfoUrl、redirectUrl一定要与自己的项目路径相符合。response_type、GrantType有标准模板,见代码。服务端生成的access token也是有标准的,见代码,too。
其他的所有模块和代码就是普通的spring-springmvc-mybatis了。
项目运行:
项目下载地址:点击打开链接
下载项目压缩包,解压,里面两个maven项目:oauthserver和oauthclient01,分别对应oauth服务端和客户端。
服务端对应的数据库sql文件在源码压缩包里可以看到。
两个项目分别用8082端口(服务端端口)和8081端口(客户端端口)部署并启动。
输入客户端地址:http://localhost:8081/oauthclient01/index,显示如下:
点击到服务端请求资源,就可以得到如下结果:
即获取到了服务端的资源。
转载于:https://www.cnblogs.com/powerwu/articles/9461986.html
Java的oauth2.0 服务端与客户端的实现相关推荐
- java的oauth2.0_[转]Java的oauth2.0 服务端与客户端的实现
oauth原理简述 oauth本身不是技术,而是一项资源授权协议,重点是协议!Apache基金会提供了针对Java的oauth封装.我们做Java web项目想要实现oauth协议进行资源授权访问,直 ...
- Java Websocket实例【服务端与客户端实现全双工通讯】
Java Websocket实例[服务端与客户端实现全双工通讯] 现很多网站为了实现即时通讯,所用的技术都是轮询(polling).轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发 出HTTP ...
- php对接AliGenie天猫精灵服务器控制智能硬件esp8266② 全面认识第三方授权机制 oauth2.0 协议,如何在 php 上搭建 oauth2.0服务端!(附带demo)
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1. php对接AliGenie天猫精灵服务器控制智能硬件esp82 ...
- KIS云·云桌面V8.0服务端和客户端下载地址
KIS云·云桌面V8.0服务端和客户端下载地址 KIS云·云桌面V8.0服务端:http://downloads.cmcloud.cn/kisqs/kis_cloud_yzm8.0.rar MD5校验 ...
- Java中Socket通信-服务端和客户端双向传输字符串实现
场景 什么是socket 在计算机通信领域,socket 被翻译为"套接字",它是计算机之间进行通信的一种约定或一种方式.通过 socket 这种约定,一台计算机可以接收其他计算机 ...
- JAVA NIO 异步TCP服务端向客户端消息群发代码教程实战
前言 项目需要和第三方厂商的服务需要用TCP协议通讯,考虑到彼此双方可能都会有断网重连.宕机重启的情况,需要保证 发生上述情况后,服务之间能够自动实现重新通信.研究测试之后整理如下代码实现.因为发现客 ...
- java 集成 cas系统 服务端和客户端配置
http://blog.csdn.net/yunye114105/article/details/7997041 参考: http://blog.csdn.net/diyagea/article/de ...
- Apache Oltu 实现 OAuth2.0 服务端【授权码模式(Authorization Code)】
要实现OAuth服务端,就得先理解客户端的调用流程,服务提供商实现可能也有些区别,实现OAuth服务端的方式很多,具体可能看 http://oauth.net/code/ 各语言的实现有(我使用了Ap ...
- 服务器响应回调函数,解决有关flask-socketio中服务端和客户端回调函数callback参数的问题(全网最全)...
由于工作当中需要用的flask_socketio,所以自己学习了一下如何使用,查阅了有关文档,当看到回调函数callback的时候,发现文档里都描述的不太清楚,最后终于琢磨出来了,分享给有需要的朋友 ...
最新文章
- php 原生查询mongo,PHP操作MongoDB的原生CURD方法
- 应用层之E-mail服务及javaMail邮件发送的知识总结
- 智能车竞赛华南赛区湖北文理学院赛点
- 按AI顶会评实力:美国7倍领先中国,谷歌雄霸全球第一,腾讯和清华分获中国产学No.1...
- 安卓摄像头 data 转bitmap
- css3宽度变大动画_不会仪表?太尴尬了。14种动画让你轻松掌握各种流量计工作原理...
- Python字符串的定义与常用操作
- [基础题] 5.(*)按如下要求编写Java应用程序:(1)编写一个用于表示战斗能力的接口Fightable,
- 要的需求 ip提取网站源码带采集 要求是PHP源码
- 基于OGG Datahub插件将Oracle数据同步上云
- Vue_案例_交互添加_利用子组件调用父组件的添加方法实现---vue工作笔记0023
- TensorFlow 学习(四)—— computation graph
- 傅里叶变换对_复变函数6傅里叶变换
- C#.Net 调用Java的Web Service
- 中国历史上十大无名英雄
- SARS-CoV-2的近端起源
- Python爬虫:搜狗(微信,知乎)公众号内容
- 华为b199usb计算机连接,华为B199如何打开USB调试的方法
- How to Read a Visualization Research Paper: Extracting the Essentials
- python3遍历目录查找文件
热门文章
- esp8266 at接收数据中断时间_利用ESP8266获取网络同步时间(北京时间)
- php macro,如何利用 macro 方法来扩展 Laravel 的基础类的功能
- 递归实现组合型枚举(搜索)
- html table 充满div,HTML,使用div+css实现自适应table布局
- 新工具︱微软Microsoft Visual Studio的R语言模块下载试用Ing...(尝鲜)
- [postgresql]postgresql的锁介绍
- Entity Framework 实体框架的形成之旅--实体框架的开发的几个经验总结
- 阿里要把雄安打造成AI第一城:未来30年城市长啥样?
- hbase1.2.4安装
- 高并发系统数据幂等性