1.添加依賴
<dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-oauth-webflow</artifactId> <version>${cas.version}</version> </dependency>
2.application.properties添加以下屬性
##
# CAS_Service
#
cas.server.name=https://server.cas.com:${server.port}/cas
cas.server.prefix=${cas.server.name}
##
# OAuth
#
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
3.增加接入servcie的注冊文件:OAUTH-1001. json
{ "@class" : "org.apereo.cas.support.oauth.services.OAuthRegisteredService", "clientId": "abc", "clientSecret": "xyz", "serviceId" : "^(https|http|imaps)://app1.cas.com.*", "name" : "OAuthService", "id" : 1001 }
這里的serviceId是通過請求過來的url中的redirect_uri來進行限制的。
4.端點介紹
啟用OAuth支持后,將提供以下端點:
端點 | 描述 | 方法類型 |
/oauth2.0/authorize | 獲取authCode或者token | GET |
/oauth2.0/accessToken,/oauth2.0/token | 獲取accessToken | POST |
/oauth2.0/profile | 通過access_token參數獲取用戶信息 | GET |
5.測試
1.首先Oauth客戶端使用Get請求訪問以下地址,獲取AuthCode:
https://server.cas.com:8443/cas/oauth2.0/authorize?response_type=code&scope=openid&client_id=abc&redirect_uri=http://app1.cas.com
client_id:為之前service中的值
redirect_uri:認證通過后的跳轉地址,會根據service文件中的serviceId進行判斷是否注冊。
請求后,會跳轉登錄界面,登錄成功跳轉http://app1.cas.com,並返回AuthCode,如:
http://app1.cas.com/?code=OC-1-jkaGG16jVb-O8cUkMHI1RfgVuYKfs9zs
2.根據authCode獲取accessToken
https://server.cas.com:8443/cas/oauth2.0/accessToken?grant_type=authorization_code&client_id=abc&client_secret=xyz&code=OC-1-jkaGG16jVb-O8cUkMHI1RfgVuYKfs9zs&redirect_uri=http://app1.cas.com
請求URL,獲取響應:access_token=AT-1-TRYGjkhonoCqC5m-RKFef3ZrDBrzNUg-&expires_in=28800
3.根據AccerssToken獲取用戶信息
https://server.cas.com:8443/cas/oauth2.0/profile?access_token=AT-1-TRYGjkhonoCqC5m-RKFef3ZrDBrzNUg-
請求URL,獲取響應如下:
{ "service" : "http://app1.cas.com", "attributes" : { "credentialType" : "RememberMeUsernamePasswordCaptchaCredential" }, "id" : "admin", "client_id" : "abc" }
參考:https://blog.csdn.net/qq_34021712/article/details/82290876