spring3 oauth2

本文详细介绍了OAuth2.0中的Authorization Code授权流程,并提供了Spring Security OAuth2的配置示例,包括如何设置客户端和服务端的认证过程。

spring-security-oauth2

 

1 oauth2.0流程(摘自互联网)

具体的协议可参考http://oauth.net/2/

这里主要说明一下Authorization Grant,主要有四类Authorization Code,Implicit,Resource Owner PasswordCredentials,ClientCredentials。

这里主要介绍服务端Authorization Code授权流程,基本流程:1.请求用户授权,2.获取code,3.获取token,4.请求资源验证token。

 

1.1 请求用户授权和获取code

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

code关联类如下:

1.2 获取token

 

 

token相关的类图

 

 

1.3 请求资源验证token

 

 

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

 

2 下载代码

Svn:https://github.com/SpringSource/spring-security-oauth/trunk

 

3 编译

mvn install -P bootstrap

 

cd samples/oauth2/ sparklr
mvn war

 

cd samples/oauth2/tonr
mvn war

 

4 启动服务端

拷贝sparklr2到tomcat1\webapps下,启动服务器(配置端口为8080)

访问:http://localhost:8080/sparklr2/

 

5 启动客户端

拷贝tonr2到tomcat2\webapps下,启动服务器(配置端口为8888)

访问:http://localhost:8888/tonr2/

 

6 OpenApi授权机制

首先必须申请成为平台的有效用户;

其次必须申请App授权,平台提供应用ID和授权码。

 

登录分2部分:

1、  用户、密码验证登录价立方;

2、  App访问的授权验证。

 

7 例子改造说明

1、  创建相关JDBC授权认证的表:oauth_client_details

createtable oauth_client_details ( 

  client_id VARCHAR(50) PRIMARY KEY, 

  resource_ids VARCHAR(256), 

  client_secret VARCHAR(256), 

  scope VARCHAR(256), 

  authorized_grant_types VARCHAR(256), 

  web_server_redirect_uri VARCHAR(256), 

  authorities VARCHAR(256),

  access_token_validity VARCHAR(256),

  refresh_token_validity VARCHAR(256),

  additional_information VARCHAR(256)

);

 

insert intooauth_client_details(client_id,resource_ids,client_secret,scope,authorized_grant_types,authorities)  

values('client','sparklr','secret','read,write','authorization_code,implicit','ROLE_CLIENT'); 

 

也可以自己实现ClientDetailsService

 

2、  修改InMemoryClientDetailsService的为JdbcClientDetailsService

3、  如果Token要用JDBC存储,修改InMemoryTokenStore为JdbcMemoryTokenStore

4、  修改user-service为jdbc-user-service

<authentication-manageralias="authenticationManager"xmlns="http://www.springframework.org/schema/security">

   <authentication-provider>

     <jdbc-user-service id="userDetailsService"data-source-ref="dataSource"/>

    </authentication-provider>

</authentication-manager>

 

createtable users(

      username VARCHAR(50) not null primarykey,

      password VARCHAR(50) not null,

      enabled boolean not null);

 

createtable authorities (

      username VARCHAR(50) not null,

      authority VARCHAR(50) not null,

      constraint fk_authorities_users foreignkey(username) references users(username));

      create unique index ix_auth_username onauthorities (username,authority);

);

 

INSERTinto users(username,password,enabled)

values('marissa','koala',true);

INSERTinto authorities(username,authority)

values('marissa','ROLE_USER');

 

也可以自己实现user-service

org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl

 

<authentication-manager>

        <authentication-provideruser-service-ref='myUserDetailsService' />

    </authentication-manager>

 

    <b:beanid="myUserDetailsService"

       class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">

        <b:propertyname="dataSource" ref="dataS" />

     </b:bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值