基于 OAuth 安全协议的 Java 应用编程
2012-03-12 10:26
791 查看
NOTE:本文转自http://www.ibm.com/developerworks/cn/java/j-lo-oauth/
OAuth 简介
OAuth 是由 Blaine Cook、Chris Messina、Larry Halff 及 David Recordon 共同发起的,目的在于为 API 访问授权提供一个安全、开放的标准。
基于 OAuth 认证授权具有以下特点:
安全。OAuth 与别的授权方式不同之处在于:OAuth 的授权不会使消费方(Consumer)触及到用户的帐号信息(如用户名与密码),也是是说,消费方无需使用用户的用户名与密码就可以申请获得该用户资源的授权。
开放。任何消费方都可以使用 OAuth 认证服务,任何服务提供方 (Service Provider) 都可以实现自身的 OAuth 认证服务。
简单。不管是消费方还是服务提供方,都很容易于理解与使用。
OAuth 的解决方案如下图所示。
图 1. OAuth Solution
如图 1 所示 OAuth 解决方案中用户、消费方及其服务提供方之间的三角关系:当用户需要 Consumer 为其提供某种服务时,该服务涉及到需要从服务提供方那里获取该用户的保护资源。OAuth 保证:只有在用户显式授权的情况下(步骤 4),消费方才可以获取该用户的资源,并用来服务于该用户。
从宏观层次来看,OAuth 按以下方式工作:
消费方与不同的服务提供方建立了关系。
消费方共享一个密码短语或者是公钥给服务提供方,服务提供方使用该公钥来确认消费方的身份。
消费方根据服务提供方将用户重定向到登录页面。
该用户登录后告诉服务提供方该消费方访问他的保护资源是没问题的。
回页首
OAuth 认证授权流程
在了解 OAuth 认证流程之前,我们先来了解一下 OAuth 协议的一些基本术语定义:
Consumer Key:消费方对于服务提供方的身份唯一标识。
Consumer Secret:用来确认消费方对于 Consumer Key 的拥有关系。
Request Token:获得用户授权的请求令牌,用于交换 Access Token。
Access Token:用于获得用户在服务提供方的受保护资源。
Token Secret:用来确认消费方对于令牌(Request Token 和 Access Token)的拥有关系。
图 2. OAuth 授权流程(摘自 OAuth 规范)
对于图 2 具体每一执行步骤,解释如下:
消费方向 OAuth 服务提供方请求未授权的 Request Token。
OAuth 服务提供方在验证了消费方的合法请求后,向其颁发未经用户授权的 Request Token 及其相对应的 Token Secret。
消费方使用得到的 Request Token,通过 URL 引导用户到服务提供方那里,这一步应该是浏览器的行为。接下来,用户可以通过输入在服务提供方的用户名 / 密码信息,授权该请求。一旦授权成功,转到下一步。
服务提供方通过 URL 引导用户重新回到消费方那里,这一步也是浏览器的行为。
在获得授权的 Request Token 后,消费方使用授权的 Request Token 从服务提供方那里换取 Access Token。
OAuth 服务提供方同意消费方的请求,并向其颁发 Access Token 及其对应的 Token Secret。
消费方使用上一步返回的 Access Token 访问用户授权的资源。
总的来讲,在 OAuth 的技术体系里,服务提供方需要提供如下基本的功能:
第 1、实现三个 Service endpoints,即:提供用于获取未授权的 Request Token 服务地址,获取用户授权的 Request Token 服务地址,以及使用授权的 Request Token 换取 Access Token 的服务地址。
第 2、提供基于 Form 的用户认证,以便于用户可以登录服务提供方做出授权。
第 3、授权的管理,比如用户可以在任何时候撤销已经做出的授权。
而对于消费方而言,需要如下的基本功能:
第 1、从服务提供方获取 Customer Key/Customer Secret。
第 2、提供与服务提供方之间基于 HTTP 的通信机制,以换取相关的令牌。
我们具体来看一个使用 OAuth 认证的例子。
在传统的网站应用中,如果您想在网站 A 导入网站 B 的联系人列表,需要在网站 A 输入您网站 B 的用户名、密码信息。例如,您登陆 Plaxo (https://www.plaxo.com ),一个联系人管理网站,当您想把 GMail 的联系人列表导入到 Plaxo,您需要输入您的 GMail 用户名 / 密码,如图 3 所示:
图 3. 在 Plaxo 获得 GMail 联系人
在这里,Plaxo 承诺不会保存您在 Gmail 的密码。
如果使用 OAuth 认证,情况是不同的,您不需要向网站 A(扮演 Consumer 角色)暴露您网站 B(扮演 Service Provider 角色)的用户名、密码信息。例如,您登录 http://lab.madgex.com/oauth-net/googlecontacts/default.aspx 网站, 如图 4 所示:
图 4. 在 lab.madgex.com 获得 GMail 联系人
点击“Get my Google Contacts”,浏览器将会重定向到 Google,引导您登录 Google,如图 5 所示:
图 5. 登录 Google
登录成功后,将会看到图 6 的信息:
图 6. Google 对 lab.madgex.com 网站授权
在您登录 Google,点击“Grant access”,授权 lab.madgex.com 后,lab.madgex.com 就能获得您在 Google 的联系人列表。
在上面的的例子中,网站 lab.madgex.com 扮演着 Consumer 的角色,而 Google 是 Service Provider,lab.madgex.com 使用基于 OAuth 的认证方式从 Google 获得联系人列表。
下一节,本文会给出一个消费方实现的例子,通过 OAuth 机制请求 Google Service Provider 的 OAuth Access Token,并使用该 Access Token 访问用户的在 Google 上的日历信息 (Calendar)。
回页首
示例
准备工作
作为消费方,首先需要访问 https://www.google.com/accounts/ManageDomains,从 Google 那里获得标志我们身份的 Customer Key 及其 Customer Secret。另外,您可以生成自己的自签名 X509 数字证书,并且把证书上传给 Google,Google 将会使用证书的公钥来验证任何来自您的请求。
具体的操作步骤,请读者参考 Google 的说明文档:http://code.google.com/apis/gdata/articles/oauth.html。
在您完成这些工作,您将会得到 OAuth Consumer Key 及其 OAuth Consumer Secret,用于我们下面的开发工作。
如何获得 OAuth Access Token
以下的代码是基于 Google Code 上提供的 OAuth Java 库进行开发的,读者可以从 http://oauth.googlecode.com/svn/code/java/core/ 下载获得。
指定 Request Token URL,User Authorization URL,以及 Access Token URL,构造 OAuthServiceProvider 对象:
指定 Customer Key,Customer Secret 以及 OAuthServiceProvider,构造 OAuthConsumer 对象:
为 OAuthConsumer 指定签名方法,以及提供您自签名 X509 数字证书的 private key。
由 OAuthConsumer 对象生成相应的 OAuthAccessor 对象:
指定您想要访问的 Google 服务,在这里我们使用的是 Calendar 服务:
通过 OAuthClient 获得 Request Token:
使用 Request Token, 将用户重定向到授权页面,如图 7 所示:
图 7. OAuth User Authorization
当用户点击“Grant access”按钮,完成授权后,再次通过 OAuthClient 获得 Access Token:
在上述步骤成功完成后,Access Token 将保存在 accessor 对象的 accessToken 成员变量里。查看您的 Google Account 安全管理页面,可以看到您授权的所有消费方,如图 8 所示。
图 8. Authorized OAuth Access to your Google Account
使用 OAuth Access Token 访问 Google 服务
接下来,我们使用上一节获得的 Access Token 设置 Google Service 的 OAuth 认证参数,然后从 Google Service 获取该用户的 Calendar 信息:
清单 1 是完整的示例代码,供读者参考。
清单 1. 基于 OAuth 认证的 Google Service 消费方实现
回页首
小结
OAuth 协议作为一种开放的,基于用户登录的授权认证方式,目前互联网很多 Open API 都对 OAuth 提供了支持,这包括 Google, Yahoo,Twitter 等。本文以 Google 为例子,介绍了 Java 桌面程序如何开发 OAuth 认证应用。在开发桌面应用访问 Web 资源这样一类程序时,一般通行的步骤是:使用 OAuth 做认证,然后使用获得的 OAuth Access Token,通过 REST API 访问用户在服务提供方的资源。
事实上,目前 OAuth 正通过许多实现(包括针对 Java、C#、Objective-C、Perl、PHP 及 Ruby 语言的实现)获得巨大的动力。大部分实现都由 OAuth 项目维护并放在 Google 代码库 (http://oauth.googlecode.com/svn/) 上。开发者可以利用这些 OAuth 类库编写自己需要的 OAuth 应用。
参考资料
学习
“Using OAuth with the Google Data APIs”:查看 Google 对 OAuth 的支持文档。
“OAuth.net”:查看 OAuth 开放协议的官方网站。
“Google sample: OAuth Playground”:查看 Google 上 OAuth 协议的示例应用。
技术书店:浏览关于这些和其他技术主题的图书。
developerWorks Java 技术专区:数百篇关于 Java 编程各个方面的文章。
讨论
加入 developerWorks 社区。
查看 developerWorks 博客的最新信息。
关于作者
李三红,任职于 IBM 中国软件开发中心 Lotus 产品部门,负责 Lotus Notes 安全相关研发工作。在这之前,他一直从事网络应用开发相关工作。他感兴趣的技术领域包括:分布式对象计算、网络应用、OSGi、协作计算、Java 安全等方面。
OAuth 简介
OAuth 是由 Blaine Cook、Chris Messina、Larry Halff 及 David Recordon 共同发起的,目的在于为 API 访问授权提供一个安全、开放的标准。
基于 OAuth 认证授权具有以下特点:
安全。OAuth 与别的授权方式不同之处在于:OAuth 的授权不会使消费方(Consumer)触及到用户的帐号信息(如用户名与密码),也是是说,消费方无需使用用户的用户名与密码就可以申请获得该用户资源的授权。
开放。任何消费方都可以使用 OAuth 认证服务,任何服务提供方 (Service Provider) 都可以实现自身的 OAuth 认证服务。
简单。不管是消费方还是服务提供方,都很容易于理解与使用。
OAuth 的解决方案如下图所示。
图 1. OAuth Solution
如图 1 所示 OAuth 解决方案中用户、消费方及其服务提供方之间的三角关系:当用户需要 Consumer 为其提供某种服务时,该服务涉及到需要从服务提供方那里获取该用户的保护资源。OAuth 保证:只有在用户显式授权的情况下(步骤 4),消费方才可以获取该用户的资源,并用来服务于该用户。
从宏观层次来看,OAuth 按以下方式工作:
消费方与不同的服务提供方建立了关系。
消费方共享一个密码短语或者是公钥给服务提供方,服务提供方使用该公钥来确认消费方的身份。
消费方根据服务提供方将用户重定向到登录页面。
该用户登录后告诉服务提供方该消费方访问他的保护资源是没问题的。
回页首
OAuth 认证授权流程
在了解 OAuth 认证流程之前,我们先来了解一下 OAuth 协议的一些基本术语定义:
Consumer Key:消费方对于服务提供方的身份唯一标识。
Consumer Secret:用来确认消费方对于 Consumer Key 的拥有关系。
Request Token:获得用户授权的请求令牌,用于交换 Access Token。
Access Token:用于获得用户在服务提供方的受保护资源。
Token Secret:用来确认消费方对于令牌(Request Token 和 Access Token)的拥有关系。
图 2. OAuth 授权流程(摘自 OAuth 规范)
对于图 2 具体每一执行步骤,解释如下:
消费方向 OAuth 服务提供方请求未授权的 Request Token。
OAuth 服务提供方在验证了消费方的合法请求后,向其颁发未经用户授权的 Request Token 及其相对应的 Token Secret。
消费方使用得到的 Request Token,通过 URL 引导用户到服务提供方那里,这一步应该是浏览器的行为。接下来,用户可以通过输入在服务提供方的用户名 / 密码信息,授权该请求。一旦授权成功,转到下一步。
服务提供方通过 URL 引导用户重新回到消费方那里,这一步也是浏览器的行为。
在获得授权的 Request Token 后,消费方使用授权的 Request Token 从服务提供方那里换取 Access Token。
OAuth 服务提供方同意消费方的请求,并向其颁发 Access Token 及其对应的 Token Secret。
消费方使用上一步返回的 Access Token 访问用户授权的资源。
总的来讲,在 OAuth 的技术体系里,服务提供方需要提供如下基本的功能:
第 1、实现三个 Service endpoints,即:提供用于获取未授权的 Request Token 服务地址,获取用户授权的 Request Token 服务地址,以及使用授权的 Request Token 换取 Access Token 的服务地址。
第 2、提供基于 Form 的用户认证,以便于用户可以登录服务提供方做出授权。
第 3、授权的管理,比如用户可以在任何时候撤销已经做出的授权。
而对于消费方而言,需要如下的基本功能:
第 1、从服务提供方获取 Customer Key/Customer Secret。
第 2、提供与服务提供方之间基于 HTTP 的通信机制,以换取相关的令牌。
我们具体来看一个使用 OAuth 认证的例子。
在传统的网站应用中,如果您想在网站 A 导入网站 B 的联系人列表,需要在网站 A 输入您网站 B 的用户名、密码信息。例如,您登陆 Plaxo (https://www.plaxo.com ),一个联系人管理网站,当您想把 GMail 的联系人列表导入到 Plaxo,您需要输入您的 GMail 用户名 / 密码,如图 3 所示:
图 3. 在 Plaxo 获得 GMail 联系人
在这里,Plaxo 承诺不会保存您在 Gmail 的密码。
如果使用 OAuth 认证,情况是不同的,您不需要向网站 A(扮演 Consumer 角色)暴露您网站 B(扮演 Service Provider 角色)的用户名、密码信息。例如,您登录 http://lab.madgex.com/oauth-net/googlecontacts/default.aspx 网站, 如图 4 所示:
图 4. 在 lab.madgex.com 获得 GMail 联系人
点击“Get my Google Contacts”,浏览器将会重定向到 Google,引导您登录 Google,如图 5 所示:
图 5. 登录 Google
登录成功后,将会看到图 6 的信息:
图 6. Google 对 lab.madgex.com 网站授权
在您登录 Google,点击“Grant access”,授权 lab.madgex.com 后,lab.madgex.com 就能获得您在 Google 的联系人列表。
在上面的的例子中,网站 lab.madgex.com 扮演着 Consumer 的角色,而 Google 是 Service Provider,lab.madgex.com 使用基于 OAuth 的认证方式从 Google 获得联系人列表。
下一节,本文会给出一个消费方实现的例子,通过 OAuth 机制请求 Google Service Provider 的 OAuth Access Token,并使用该 Access Token 访问用户的在 Google 上的日历信息 (Calendar)。
回页首
示例
准备工作
作为消费方,首先需要访问 https://www.google.com/accounts/ManageDomains,从 Google 那里获得标志我们身份的 Customer Key 及其 Customer Secret。另外,您可以生成自己的自签名 X509 数字证书,并且把证书上传给 Google,Google 将会使用证书的公钥来验证任何来自您的请求。
具体的操作步骤,请读者参考 Google 的说明文档:http://code.google.com/apis/gdata/articles/oauth.html。
在您完成这些工作,您将会得到 OAuth Consumer Key 及其 OAuth Consumer Secret,用于我们下面的开发工作。
如何获得 OAuth Access Token
以下的代码是基于 Google Code 上提供的 OAuth Java 库进行开发的,读者可以从 http://oauth.googlecode.com/svn/code/java/core/ 下载获得。
指定 Request Token URL,User Authorization URL,以及 Access Token URL,构造 OAuthServiceProvider 对象:
OAuthServiceProvider serviceProvider = new OAuthServiceProvider( "https://www.google.com/accounts/OAuthGetRequestToken", "https://www.google.com/accounts/OAuthAuthorizeToken", "https://www.google.com/accounts/OAuthGetAccessToken"); |
OAuthConsumer oauthConsumer = new OAuthConsumer(null , "www.example.com" , "hIsGkM+T4+90fKNesTtJq8Gs" , serviceProvider); |
oauthConsumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1); oauthConsumer.setProperty(RSA_SHA1.PRIVATE_KEY, privateKey); |
accessor = new OAuthAccessor(consumer); |
Collection<? extends Map.Entry> parameters = OAuth.newList("scope","http://www.google.com/calendar/feeds/"); |
OAuthMessage response = getOAuthClient().getRequestTokenResponse( accessor, null, parameters); |
图 7. OAuth User Authorization
当用户点击“Grant access”按钮,完成授权后,再次通过 OAuthClient 获得 Access Token:
oauthClient.getAccessToken(accessor, null, null); |
图 8. Authorized OAuth Access to your Google Account
使用 OAuth Access Token 访问 Google 服务
接下来,我们使用上一节获得的 Access Token 设置 Google Service 的 OAuth 认证参数,然后从 Google Service 获取该用户的 Calendar 信息:
OAuthParameters para = new OAuthParameters(); para.setOAuthConsumerKey("www.example.com"); para.setOAuthToken(accessToken); googleService.setOAuthCredentials(para, signer); |
清单 1. 基于 OAuth 认证的 Google Service 消费方实现
|
小结
OAuth 协议作为一种开放的,基于用户登录的授权认证方式,目前互联网很多 Open API 都对 OAuth 提供了支持,这包括 Google, Yahoo,Twitter 等。本文以 Google 为例子,介绍了 Java 桌面程序如何开发 OAuth 认证应用。在开发桌面应用访问 Web 资源这样一类程序时,一般通行的步骤是:使用 OAuth 做认证,然后使用获得的 OAuth Access Token,通过 REST API 访问用户在服务提供方的资源。
事实上,目前 OAuth 正通过许多实现(包括针对 Java、C#、Objective-C、Perl、PHP 及 Ruby 语言的实现)获得巨大的动力。大部分实现都由 OAuth 项目维护并放在 Google 代码库 (http://oauth.googlecode.com/svn/) 上。开发者可以利用这些 OAuth 类库编写自己需要的 OAuth 应用。
参考资料
学习
“Using OAuth with the Google Data APIs”:查看 Google 对 OAuth 的支持文档。
“OAuth.net”:查看 OAuth 开放协议的官方网站。
“Google sample: OAuth Playground”:查看 Google 上 OAuth 协议的示例应用。
技术书店:浏览关于这些和其他技术主题的图书。
developerWorks Java 技术专区:数百篇关于 Java 编程各个方面的文章。
讨论
加入 developerWorks 社区。
查看 developerWorks 博客的最新信息。
关于作者
李三红,任职于 IBM 中国软件开发中心 Lotus 产品部门,负责 Lotus Notes 安全相关研发工作。在这之前,他一直从事网络应用开发相关工作。他感兴趣的技术领域包括:分布式对象计算、网络应用、OSGi、协作计算、Java 安全等方面。
相关文章推荐
- 基于 OAuth 安全协议的 Java 应用编程
- 基于 OAuth 安全协议的 Java 应用编程1
- 基于 OAuth 安全协议的 Java 应用编程
- 基于 OAuth 安全协议的 Java 应用编程
- 基于 OAuth 安全协议的 Java 应用编程1
- 基于 OAuth 安全协议的 Java 应用编程
- 基于 OAuth 安全协议的 Java 应用编程
- JAVA常用API或编程工具002---SpringSource Tool Suite:基于Eclipse的Spring应用开发环境
- 基于Java语言的安卓编程学习之六Activity的切换
- Java编程中应用dom4j轻松地处理XML文档
- JavaWeb应用中的身份验证(声明式)——基于表单的身份认证
- 基于Java+SparkStreaming整合kafka编程
- 黑白棋算法简单实现与基于Qt的GUI编程的综合应用
- 应用Java技术开发WAP应用程序-Java基础-Java-编程开发
- [零基础学JAVA]Java SE应用部分-33.Java数据库编程(JDBC)之二 推荐
- Java中基于TCP_IP协议的网络编程例子
- java Socket编程-基于UDP
- java进阶 ------ 基于Socket低层次网络编程
- CORBA 及Java IDL应用编程
- java基于xml配置的通用excel单表数据导入组件(一、实际应用过程)