CAS3.5.x提供了oauth的支持,包括客戶端和服務端,cas-server-support-oauth依賴架包web
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.session
##關於接入的一些背景: 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.OAuth20WrapperControllerapp
下面配置cas server支持oauth2 server,咱們從oauth2 client向cas接入爲步驟來分析每一步的配置:ui
在成熟的系統中,一般提供頁面供用戶申請應用,而後提供用戶client_id和client_secret,並容許用戶配置回調地址,那麼oauthserver端(即CAS Server)首先考慮的就是須要持久化這些配置。默認在文件deployerConfigContext.xml的serviceRegistryDao中配置應用服務,實際使用中,咱們能夠將申請的應用信息存儲在數據庫中:url
<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的用途將在下面介紹。code
一般客戶端構造的url可能以下(參數能夠參照標準的oauth2 protocol,可是不一樣的oauth server一般提供了本身的標準):server
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中配置以下:xml
<servlet-mapping> <servlet-name>cas</servlet-name> <url-pattern>/oauth2.0/*</url-pattern> </servlet-mapping>
在cas-servlet.xml中配置映射:token
<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。
https://cas.sayi.com:8443/cas/login?service=https%3A%2F%2Fcas.sayi.com%3A8443%2Fcas%2Foauth2.0%2FcallbackAuthorize
認證成功以後,就會攜帶值爲ST的參數跳轉到callbackAuthorize頁面,此時生成的ST即爲受權碼,回調地址、服務名稱經過session傳遞過來。
https://cas.sayi.com:8443/cas/oauth2.0/callbackAuthorize?ticket=ST-5-ywMLFaXQFnDeFI7erFy7-cas.sayi.com
默認受權碼只能使用一次,且有效時間爲10s,能夠經過票根過時策略進行配置時間。
構造的URL以下:
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.
構造URL以下:
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。