CAS3.5.x(x>1)支持OAuth2 server

来源:互联网 发布:南京seo 页面优化 编辑:程序博客网 时间:2024/05/16 18:33

原文地址: http://my.oschina.net/sayi/blog/200278

目录[-]

  • OAuth support
  • 关于接入的一些背景:
  • step1. 应用配置,获得client_id和client_secret
  • step2. Oauth client 构造url,获取authorization_code
  • step3. 授权码交换access_token
  • step4. 根据access_token获取用户信息
  • 总结

OAuth support

CAS3.5.x提供了oauth的支持,包括客户端和服务端,cas-server-support-oauth依赖架包

scribe-1.3.5.jar 
scribe-up-1.2.0.jar 
jackson-core-2.3.0.jar,jackson-databind-2.3.0.jar。

CAS默认提供了三个服务: 
/oauth2.0/authorize 
Input GET parameters required : client_id and redirect_uri. 
/oauth2.0/accessToken 
Input GET parameters required : client_id, redirect_uri, client_secret and code. 
/oauth2.0/profile 
Input GET parameter required : access_token.

关于接入的一些背景:

1.cas的web登录访问路径为https://cas.sayi.com:8443/cas/login 
2.回调地址为http://www.doubannote.org/(虚拟地址,实际不存在) 
3.client_Id为key 
4.client_secret为secret 
5.应用名称为DoubanNote 
6.核心类为org.jasig.cas.support.oauth.web.OAuth20WrapperController

下面配置cas server支持oauth2 server,我们从oauth2 client向cas接入为步骤来分析每一步的配置:

step1. 应用配置,获得client_id和client_secret

在成熟的系统中,通常提供页面供用户申请应用,然后提供用户client_id和client_secret,并允许用户配置回调地址,那么oauthserver端(即CAS Server)首先考虑的就是需要持久化这些配置。默认在文件deployerConfigContext.xml的serviceRegistryDao中配置应用服务,实际使用中,我们可以将申请的应用信息存储在数据库中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<bean
    id="serviceRegistryDao"
    class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
        <property name="registeredServices">
            <list>
                <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                    <property name="id" value="1" />
                    <property name="name" value="HTTP" />
                    <property name="description" value="oauth wrapper callback url" />
                    <property name="serviceId" value="${server.prefix}/oauth2.0/callbackAuthorize" />
                </bean>
               <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                <property name="id" value="2" />
                <property name="name" value="key" />
                <property name="description" value="secret" />
                <property name="serviceId" value="http://www.doubannote.org/" />
                <property name="theme" value="DoubanNote" />
              </bean>
              ......

如代码所示,我们新注册了两个bean,关于应用的配置在第二个bean中,name为client_id,description为client_secret,serviceId为回调地址,theme为应用名称。 
关于第一个bean的用途将在下面介绍。

step2. Oauth client 构造url,获取authorization_code

通常客户端构造的url可能如下(参数可以参照标准的oauth2 protocol,但是不同的oauth server通常提供了自己的标准):

1
https://cas.sayi.com:8443/cas/oauth2.0/authorize?client_id=key&redirect_uri=http://www.doubannote.org/&response_type=code

在这里就要求cas server能对/oauth2.0/authorize的url进行处理,那么就需要配置映射,在web.xml中配置如下:

1
2
3
4
<servlet-mapping>
    <servlet-name>cas</servlet-name>
    <url-pattern>/oauth2.0/*</url-pattern>
</servlet-mapping>

在cas-servlet.xml中配置映射:

1
2
3
4
5
6
7
<prop key="/oauth2.0/*">oauth20WrapperController</prop>
...
...
<bean id="oauth20WrapperController"
    class="org.jasig.cas.support.oauth.web.OAuth20WrapperController"
    p:loginUrl="${server.prefix}/login" p:servicesManager-ref="servicesManager"
    p:ticketRegistry-ref="ticketRegistry" p:timeout="7200" />

如上配置了之后,我们获取授权码的链接会转向login页面,此时的service地址就是step1中配置的第一个bean的serviceId,通过这个默认提供的地址间接的获取到ST。

1
https://cas.sayi.com:8443/cas/login?service=https%3A%2F%2Fcas.sayi.com%3A8443%2Fcas%2Foauth2.0%2FcallbackAuthorize

认证成功之后,就会携带值为ST的参数跳转到callbackAuthorize页面,此时生成的ST即为授权码,回调地址、服务名称通过session传递过来。

1
https://cas.sayi.com:8443/cas/oauth2.0/callbackAuthorize?ticket=ST-5-ywMLFaXQFnDeFI7erFy7-cas.sayi.com

默认授权码只能使用一次,且有效时间为10s,可以通过票根过期策略进行配置时间。

step3. 授权码交换access_token

构造的URL如下:

1
2
3
https://cas.sayi.com:8443/cas/oauth2.0/accessToken?client_id=key&client_secret=secret&grant_type=authorization_code&redirect_uri=http://www.doubannote.org/&code=ST-1-3jLuZnhcAvLiLdy7R6ft-cas.sayi.com
 
access_token=TGT-2-qWkLyEbeoby043q05p5GHXfBg7qtdPZjEUhfemgg3UKbxAyB5s-cas.sayi.com&expires=7143

通过返回的值可以获得access_token.

step4. 根据access_token获取用户信息

构造URL如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
https://cas.sayi.com:8443/cas/oauth2.0/profile?access_token=TGT-1-gn3p9EMfFEajKOJ9DdNqd2PefJdIbIeXuESyzU4EctMtBqITRG-cas.sayi.com
 
{
"id":"sayi",
    "attributes":[
        {
            "uid":"uid"
        },
        {
            "eduPersonAffiliation":"eduPersonAffiliation"
        },
        {
            "groupMembership":"groupMembership"
        }
    ]
}

总结

cas server支持oauth2 server,无非就是考虑对/authorize、/accessToken、/profile的请求的处理,在服务端进行应用配置后,对接入的应用进行校验,比如回调地址、client_secret等。在与cas server的融合中,主要就是cas认证与/authorize的融合。在这里使用的是callbackAuthorize的方式,cas默认提供了/oauth2.0/callbackAuthorize的service地址,通过此地址cas认证成功之后生成ST,此值即为授权码,传递给应用的回调地址即可。 
总体来说oauth2的支持在cas3.5.x中并不完善,而且OAuth2的实现也不是标准的,对于3.5.x版本我们需要扩展OAuth20WrapperController来进一步融合oauth2 protocol。

0 0
原创粉丝点击