劉罡 发表于 2013-2-7 20:49:44

管理OAuth中的令牌

记录一下关于OAuth中关于令牌的管理

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

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

OAuth1.0相关配置
在OAuth1.0用户手册中可以看到这样一段关于管理令牌的文字
http://dl.iteye.com/upload/attachment/494528/a7c796c9-39e7-365f-b6ec-d14a1c3e3853.png
如果我想实现自己的令牌服务类,则需要继承RandomValueProviderTokenServices
http://dl.iteye.com/upload/attachment/494584/978384d1-7dcd-3b53-bb41-ff68fb0754ed.png
由上图可见,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用户手册中可以看到这样一段关于管理令牌的文字
http://dl.iteye.com/upload/attachment/494639/44b3b16c-677c-3171-8380-0107d3c7d87f.png
如果我想实现自己的令牌服务类,则需要继承RandomValueOAuth2ProviderTokenServices
http://dl.iteye.com/upload/attachment/494641/20595748-97cc-3470-9664-53bab12b19ea.png
由上图可见,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>
接下来看看基于数据库的方式如何实现,首先,我们来看看基于数据库的实现的源代码,这个源代码太给力了。从这个类中就可以完整的了解到将令牌持续化到数据库中所需要的所有操作,如果懒得写代码,甚至可以直接根据该类中的表名,列名创建相应的表,然后将所有的关于数据库的操作交给这个类来完成,我就是这么做的,哈哈
http://dl.iteye.com/upload/attachment/494649/491366ec-827d-378a-819f-a9c1d803c7cf.png
http://dl.iteye.com/upload/attachment/494651/29f4f3a2-a2b4-3f12-b0ed-78123a3206b3.png
两张表创建完成,配置一下就可以了
<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,大功告成!
页: [1]
查看完整版本: 管理OAuth中的令牌