CAS統一登入認證(11): 提供oauth2.0認證伺服器
CAS可以提供Oauth2.0的第三方認證服務,這個服務,cas可以是作為服務客戶端,如通過qq,微信,csdn,github等認證後登入cas,這時,qq互聯等是oauth服務的提供者,cas是oauth客戶端。另外一種功能是CAS作為第三方認證提供者,即Oauth伺服器,為單位的各應用提供第三方Ouath認證服務,這時對提供oauth服務來說,cas是服務端,接入應用軟體是Oauth服務的客戶端。
CAS作為Oauth客戶端,另文再介紹,本文中,CAS是Oauth2.0服務提供者。
1.基本原理,不多著解析,借用一張網路圖
2.cas伺服器配置
(1)修改pom.xml,編譯讓cas包含Oauth外掛
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-oauth</artifactId>
<version>${cas.version}</version>
</dependency>
(2) 編譯部署後,修改application.properties 增加
cas.server.name=https://author.linbsoft.com
cas.server.prefix=https://author.linbsoft.com/cas
######### oauth2.0 ###########
cas.authn.oauth.refreshToken.timeToKillInSeconds=2592000
cas.authn.oauth.code.timeToKillInSeconds=30
cas.authn.oauth.code.numberOfUses=1
cas.authn.oauth.accessToken.releaseProtocolAttributes=true
cas.authn.oauth.accessToken.timeToKillInSeconds=7200
cas.authn.oauth.accessToken.maxTimeToLiveInSeconds=28800
cas.authn.oauth.grants.resourceOwner.requireServiceHeader=true
cas.authn.oauth.userProfileViewType=NESTED
地址改為你自己的cas伺服器URL
(3)在services 增加一個json檔案,允許向特定應用提供oauth服務
{
"@class" : "org.apereo.cas.support.oauth.services.OAuthRegisteredService",
"clientId": "123666888",
"clientSecret": "b7cb58ffeba34a68bed632f8f2d55d2f",
"bypassApprovalPrompt": false,
"generateRefreshToken": false,
"serviceId" : "^(http|https)://.*",
"name" : "Linbsoft OAuth service ",
"id" : 101,
"attributeReleasePolicy" : {
"@class" : "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
}
}
重啟tomcat伺服器即可,在實際應用中可以單個應用授權。
3.CAS Oauth2.0客戶端應用demo
以下是我的一個測試demo
點擊發起第三方認證後,彈出視窗跳轉到
第二步會出現cas的登入介面(如果未登入),已登入直接跳過。
第三步cas伺服器請求授權
點允許後會返回code
http://aaa.linbsoft.com/casoauth/gettoken.aspx?code=OC-13-McilkUuZCyaCars1jC5RzXUtbokuPY2C
我把返回的code和其它請求資訊構建了一個form表單,提交換取Token
點選提交post表單到 https://author.linbsoft.com/cas/oauth2.0/accessToken
獲取Token
把token 提交給cas換取使用者資訊
實際是向cas傳送get請求
cas伺服器返回使用者資訊
客戶端可以根據這個資訊登入客戶端系統了。