ITeye博客

spring-security-oauth使用笔记

bukebuhao · 2011-12-30 16:42 · java开发 · 企业架构 · Markdown

最新公司的项目也搞对外开放,也就是加入开放的潮流。目前有很多大公司都已开放api,为何我们公司还搞的,我们搞的主要目的不是为了对外开放api,只是方便第三方应用获取用户授权的信息,整合所有的有效资源,因此来说我们采用的oauth协议是最简单最实用的。由于公司自己没有实力,只能依靠开源的实现,经过对多方考察,感觉spring-security-oauth实现的最好,大家可参考http://netment.iteye.com/blog/945402博客,他们也认为spring-security-oauth不错的。不过,我自己去实际考察又发现,不只是有oauth1.0和oauth2.0,而且虽然都是基于security的实现,竟然有两种不同的实现版本,主要讨论的是oauth2.0

第一个版本

网址是http://svn.codehaus.org/spring-security-oauth/branches/SS3/,已提交到maven仓库了groupId=org.codehaus.spring-security-oauth,这个版本实现的相对比较简单,只支持内存保存token,而且结合security比较紧密,非常适合学习。

oauth2.0的基本流程,1.请求用户授权,2.获取code,3.获取token,4.请求资源验证token。 因此,只需要关注WebServerOAuth2Filter,BasicUserApprovalFilter,OAuth2AuthorizationFilter,OAuth2ProtectedResourceFilter,其中WebServerOAuth2Filter是/oauth/user/authorize入口,验证code无效, unapprovedAuthenticationHandler跳转到*/oauth/confirm_access,用户提交之后* BasicUserApprovalFilter拦截之后,获取授权参数,就交给 WebServerOAuth2Filter获取code,获取code返回到client,然后client再次请求 /oauth/authorize,OAuth2AuthorizationFilter获取token,之后只要请求的时候带上token由 OAuth2ProtectedResourceFilter负责验证,是否可获取用户资源。

第二个版本

网址https://github.com/SpringSource/spring-security-oauth/wiki/oauth2,这个版本oauth2.0的基本所有功能都已实现,不过还是处于内部测试当中,spring-security-oauth2-1.0.0.BUILD-SNAPSHOT,需要自己手动下载后利用maven编译成jar,其中第一次编译的时候,去掉 <module>samples</module>,否则就会出现编译错误。我们公司目前的程序开发也处于内部测试当中,因此,大胆采用该版本。现在详细论述一下,具体的实现流程。

按照oauth2.0的协议规定,oauth2.0大体的流程如下:

具体的协议可参考http://oauth.net/2/,我们这里主要说明一下Authorization Grant,主要有四类Authorization Code,Implicit,Resource Owner Password Credentials,Client Credentials,这里主要介绍服务端Authorization Code授权流程,基本流程:1.请求用户授权,2.获取code,3.获取token,4.请求资源验证token。

  • 请求用户授权和获取code

首先要请求用户授权,其次才会生成一个临时的code.可参考一下,我简单画得一个流程图如下:

 

code关联类如下:

 

  • 获取token

token相关的类图

  • 请求资源验证token

大体的流程基本如上,具体的xml配置请参考sparklr2的src/main/webapp/WEB-INF/spring-servlet.xml。

评论(12)

  1. #12 楼 · lf008 · 2017-04-25 18:57

    你好,你提供的demo下载不到了,能加下QQ,提供demo给我参考下,441807336

  2. #11 楼 · zhdi0 · 2013-04-12 20:55

    我要结合 OAuth2RestTemplate 访问授权资源,提示 An Authentication object was not found in the SecurityContext, 客户端要怎么设置TOKEN啊,能不能交流下

  3. #10 楼 · java_4_ever · 2012-12-28 21:04

    谢谢,最终找到了一个能用的。 官网的为什么不行我也无解。

  4. #9 楼 · bukebuhao · 2012-12-25 13:20

    bukebuhao writes:

    [quote=java_4_ever]你好,我把代码下下来之后用mven编译的时候有这样的错误,不知道你有没有遇到过,如果有事怎么解决的? [error] Failed to execute goal on project tonr2: Could not resolve dependencies for project org.springframew ork.security.oauth:tonr2:war:1.0.1.BUILD-SNAPSHOT: The following artifacts could not be resolved: org.spring framework.security.oauth:sparklr2:war:1.0.1.BUILD-SNAPSHOT, org.springframework.security.oauth:spring-securi ty-oauth2:jar:1.0.1.BUILD-SNAPSHOT: Failure to find org.springframework.security.oauth:sparklr2:war:1.0.1.BU ILD-SNAPSHOT in http://maven.springframework.org/milestone was cached in the local repository, resolution wi ll not be reattempted until the update interval of spring-milestone has elapsed or updates are forced -> [he lp 1][/quote] maven编辑的时候,主要是一个先后顺序。还有就是你本地下载的地址有缓存,可能下载不成功,导致加载jar失败。

    如果是下载失败,导致加载不成功,到本地目录下删除对应的文件目录,重新下载就可了

  5. #8 楼 · bukebuhao · 2012-12-25 13:19

    java_4_ever writes:

    你好,我把代码下下来之后用mven编译的时候有这样的错误,不知道你有没有遇到过,如果有事怎么解决的? [error] Failed to execute goal on project tonr2: Could not resolve dependencies for project org.springframew ork.security.oauth:tonr2:war:1.0.1.BUILD-SNAPSHOT: The following artifacts could not be resolved: org.spring framework.security.oauth:sparklr2:war:1.0.1.BUILD-SNAPSHOT, org.springframework.security.oauth:spring-securi ty-oauth2:jar:1.0.1.BUILD-SNAPSHOT: Failure to find org.springframework.security.oauth:sparklr2:war:1.0.1.BU ILD-SNAPSHOT in http://maven.springframework.org/milestone was cached in the local repository, resolution wi ll not be reattempted until the update interval of spring-milestone has elapsed or updates are forced -> [he lp 1]

    maven编辑的时候,主要是一个先后顺序。还有就是你本地下载的地址有缓存,可能下载不成功,导致加载jar失败。

  6. #7 楼 · java_4_ever · 2012-12-22 16:26

    你好,我把代码下下来之后用mven编译的时候有这样的错误,不知道你有没有遇到过,如果有事怎么解决的? Failed to execute goal on project tonr2: Could not resolve dependencies for project org.springframew ork.security.oauth:tonr2:war:1.0.1.BUILD-SNAPSHOT: The following artifacts could not be resolved: org.spring framework.security.oauth:sparklr2:war:1.0.1.BUILD-SNAPSHOT, org.springframework.security.oauth:spring-securi ty-oauth2:jar:1.0.1.BUILD-SNAPSHOT: Failure to find org.springframework.security.oauth:sparklr2:war:1.0.1.BU ILD-SNAPSHOT in http://maven.springframework.org/milestone was cached in the local repository, resolution wi ll not be reattempted until the update interval of spring-milestone has elapsed or updates are forced ->

  7. #6 楼 · bukebuhao · 2012-12-21 09:23

    听听米 writes:

    您好,能不能加一下qq,关于OAuthe2.0的一些问题想向你请教一下,因为最近我们公司也有这样的需求,但是不太懂,谢谢!!!605196681

    谢谢关注,有什么问题,我看看能否解决。后来我们没采用oauth2.0啦,后来直接参考 http://stamen.iteye.com/blog/1625094实现方式

  8. #5 楼 · 听听米 · 2012-12-12 09:32

    您好,能不能加一下qq,关于OAuthe2.0的一些问题想向你请教一下,因为最近我们公司也有这样的需求,但是不太懂,谢谢!!!605196681

  9. #4 楼 · 百威 · 2012-06-20 22:04

    spring-security-oauth2都需要依赖那些jar包

  10. #3 楼 · tz_dream · 2012-06-14 14:55

    你好,可以加QQ交流下OAuth吗?Q号:476858605,谢谢!

  11. #2 楼 · bukebuhao · 2012-02-29 09:52

    niuky writes:

    请问有案例没?这个还是不怎么会使用,下载了看了下!希望交流下!

    可参考示例samples

  12. #1 楼 · niuky · 2012-02-28 16:51

    请问有案例没?这个还是不怎么会使用,下载了看了下!希望交流下!

评论功能维护中,当前仅展示历史评论。