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


國內私募機構九鼎控股打造APP,來就送 20元現金領取地址:http://jdb.jiudingcapital.com/phone.html
內部邀請碼:C8E245J (不寫邀請碼,沒有現金送)
國內私募機構九鼎控股打造,九鼎投資是在全國股份轉讓系統掛牌的公眾公司,股票代碼為430719,為中國PE第一股,市值超1000億元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

目錄[-]

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的用途將在下面介紹。【終於搞明白了為何是這樣了,服務器間接獲取 ST】

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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM