六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 155|回复: 0

管理OAuth中的令牌

[复制链接]

升级  56%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
28
 楼主| 发表于 2013-2-7 20:49:44 | 显示全部楼层 |阅读模式
记录一下关于OAuth中关于令牌的管理

这里用到OAuth for Spring Security,同时支持OAuth1.0和OAuth2.0

对于OAuth还不了解的朋友可以先从搜狐开放平台网易开放平台新浪开放平台腾讯开放平台人人网开放平台了解

OAuth1.0相关配置
OAuth1.0用户手册中可以看到这样一段关于管理令牌的文字

如果我想实现自己的令牌服务类,则需要继承RandomValueProviderTokenServices

由上图可见,OAuth for Spring Security框架的OAuth1.0的实现中,默认只提供了基于内存的实现方式
使用基于内存的实现方式比较简单,只需要在配置文件中如下配置即可
<oauth:provider consumer-details-service-ref="consumerDetails"token-services-ref="tokenServices"request-token-url="/oauth/request_token"authenticate-token-url="/oauth/authorize"authentication-failed-url="/oauth/confirm_access"access-granted-url="/request_token_authorized.jsp"access-token-url="/oauth/access_token"require10a="false"token-id-param="oauth_token"callback-url-param="oauth_callback"/><oauth:consumer-details-service id="consumerDetails"><oauth:consumer name="Tonr.com" key="tonr-consumer-key"secret="SHHHHH!!!!!!!!!!"resourceName="Your Photos"resourceDescription="Your photos that you have uploaded to sparklr.com." /></oauth:consumer-details-service><oauth:token-services id="tokenServices"/>为了实现将令牌持久化至数据库,所以,我们需要继承RandomValueProviderTokenServices,并且,实现readToken、storeToken、removeToken三个方法用于管理令牌。到这个步骤就容易了,接下来就是根据令牌的数据结构创建表,然后实现一系列的增删查改的操作即可。

OAuth2.0相关配置
OAuth2.0用户手册中可以看到这样一段关于管理令牌的文字

如果我想实现自己的令牌服务类,则需要继承RandomValueOAuth2ProviderTokenServices

由上图可见,OAuth for Spring Security框架的OAuth2.0的实现中,不仅有基于内存的实现,也提供了基于数据库的实现,这样就好办了。
首先,来看看基于内存的实现方式,非常简单,只需要如下配置即可:
<oauth:provider client-details-service-ref="clientDetails" token-services-ref="jdbcTokenServices" >    <oauth:verification-code user-approval-page="/oauth/confirm_access"/></oauth:provider><oauth:client-details-service id="clientDetails"><oauth:client clientId="my-trusted-client" authorizedGrantTypes="password,authorization_code,refresh_token"/><oauth:client clientId="my-trusted-client-with-secret" authorizedGrantTypes="password,authorization_code,refresh_token" secret="somesecret"/><oauth:client clientId="my-less-trusted-client" authorizedGrantTypes="authorization_code"/><oauth:client clientId="tonr" authorizedGrantTypes="authorization_code"/></oauth:client-details-service><beans:bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices"><beans:property name="supportRefreshToken" value="true"/></beans:bean>
接下来看看基于数据库的方式如何实现,首先,我们来看看基于数据库的实现源代码,这个源代码太给力了。从这个类中就可以完整的了解到将令牌持续化到数据库中所需要的所有操作,如果懒得写代码,甚至可以直接根据该类中的表名,列名创建相应的表,然后将所有的关于数据库的操作交给这个类来完成,我就是这么做的,哈哈


两张表创建完成,配置一下就可以了
<oauth:provider client-details-service-ref="clientDetails" token-services-ref="tokenServices" >    <oauth:verification-code user-approval-page="/oauth/confirm_access"/></oauth:provider><oauth:client-details-service id="clientDetails"><oauth:client clientId="my-trusted-client" authorizedGrantTypes="password,authorization_code,refresh_token"/><oauth:client clientId="my-trusted-client-with-secret" authorizedGrantTypes="password,authorization_code,refresh_token" secret="somesecret"/><oauth:client clientId="my-less-trusted-client" authorizedGrantTypes="authorization_code"/><oauth:client clientId="tonr" authorizedGrantTypes="authorization_code"/></oauth:client-details-service><beans:bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.JdbcOAuth2ProviderTokenServices"><beans:constructor-arg index="0" ref="dataSource" /><beans:property name="supportRefreshToken" value="true" /></beans:bean>
OK,大功告成!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表