CAS5.3.8 oauth2 環境搭建

環境信息

JDK:1.8
CAS:5.3.8 overlay
tomcat:8.5

cas部署

下載cas overlay 代碼 編譯部署

添加如下依賴

            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-support-oauth-webflow</artifactId>
                <version>5.3.8</version>
            </dependency>
            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-support-jdbc</artifactId>
                <version>${cas.version}</version>
            </dependency>

第一個依賴支持oauth2 第二個依賴支持數據庫驗證登錄

部署後修改/apache-tomcat-8.5.38/webapps/cas/WEB-INF/classes下面的application.properties
屏蔽 #cas.authn.accept.users=casuser::Mellon
停用靜態用戶登錄
改用數據庫密碼登錄
在Linux /etc/cas/config 下面添加cas.properties 如下:
//此處改正你的地址 test.com是在系統hosts文件裏面配置的
cas.server.name: https://test.com:8443
cas.server.prefix: https://test.com:8443/cas

cas.adminPagesSecurity.ip=127.0.0.1

logging.config: file:/etc/cas/config/log4j2.xml
cas.tgc.secure=false //去掉HTTPS驗證
#cas.tgc.httpOnly=true
cas.warningCookie.secure=false //去掉HTTPS驗證
#cas.warningCookie.httpOnly=true
cas.serviceRegistry.initFromJson=true //這個一定要改應該是告訴系統從json文件加載服務

#cas.authn.jdbc.query[0].credentialCriteria=
#cas.authn.jdbc.query[0].name=
#cas.authn.jdbc.query[0].order=0

cas.authn.jdbc.query[0].sql=SELECT * FROM users WHERE username=?
cas.authn.jdbc.query[0].fieldPassword=password
cas.authn.jdbc.query[0].fieldExpired=
cas.authn.jdbc.query[0].fieldDisabled=
cas.authn.jdbc.query[0].principalAttributeList=sn,cn:commonName,givenName

cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=ww103088
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://192.168.2.129:3306/test?useSSL=false&serverTimezone=UTC
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].failFastTimeout=1
#cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].healthQuery=select 1
#cas.authn.jdbc.query[0].isolateInternalQueries=false
#cas.authn.jdbc.query[0].leakThreshold=10
#cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
#cas.authn.jdbc.query[0].batchSize=1
#cas.authn.jdbc.query[0].defaultCatalog=
#cas.authn.jdbc.query[0].defaultSchema=
#cas.authn.jdbc.query[0].ddlAuto=create-drop
#cas.authn.jdbc.query[0].physicalNamingStrategyClassName=org.apereo.cas.jpa.CasHibernatePhysicalNamingStrategy

cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].idleTimeout=1000
cas.authn.jdbc.query[0].properties.propertyName=propertyValue
cas.authn.jdbc.query[0].pool.suspension=false
cas.authn.jdbc.query[0].pool.minSize=6
cas.authn.jdbc.query[0].pool.maxSize=18
cas.authn.jdbc.query[0].pool.maxWait=9000
cas.authn.jdbc.query[0].pool.timeoutMillis=1000

as.authn.oauth.refreshToken.timeToKillInSeconds=25999

cas.authn.oauth.code.timeToKillInSeconds=300
cas.authn.oauth.code.numberOfUses=1

cas.authn.oauth.accessToken.releaseProtocolAttributes=true
cas.authn.oauth.accessToken.timeToKillInSeconds=7200
cas.authn.oauth.accessToken.maxTimeToLiveInSeconds=2880

cas.authn.oauth.grants.resourceOwner.requireServiceHeader=true

cas.authn.oauth.userProfileViewType=NESTED

修改 apache-tomcat-8.5.38/webapps/cas/WEB-INF/classes/services 下面的 HTTPSandIMAPS-10000001.json 文件,添加http支持 也就是不用HTTPS訪問 在serviceid裏面添加http:

{
“@class” : “org.apereo.cas.services.RegexRegisteredService”,
“serviceId” : “^(https|imaps|http)😕/.*”,
“name” : “HTTPS and IMAPS”,
“id” : 10000001,
“description” : “This service definition authorizes all application urls that support HTTPS and IMAPS protocols.”,
“evaluationOrder” : 10000
}

在apache-tomcat-8.5.38/webapps/cas/WEB-INF/classes/services
下面添加 OAuthService-1000001.json文件
{
“@class” : “org.apereo.cas.support.oauth.services.OAuthRegisteredService”,
“clientId”: “100001”,
“clientSecret”: “100001abcdeft”,
“serviceId” : “^(https|http|imaps)😕/.*”,
“name” : “OAuthService”,
“id” : 1000001
}

採用authorization_code方式授權,首先獲取code
https://test.com:8443/cas/oauth2.0/authorize?response_type=code&client_id=100001&redirect_uri=http://www.baidu.com

這一個步驟如果報空指針異常,看一下代碼是不是registerserver是不是空的,如果是這樣,那就是你上面配置的json service 沒有生效,如果生效
你能在tomcat啓動日誌裏面看到加載service的信息

還要確認下你的clientid是不是跟配置的一致,否則也會報錯。

成功後會跳轉到百度,在url後面會帶code參數

然後在用code 去獲取token
https://test.com:8443/cas/oauth2.0/accessToken?grant_type=authorization_code&client_id=100001&client_secret=100001abcdeft&code=OC-1-JclMChjB1blOaFVVMmEZ6Gkrt76rbUiv&redirect_uri=http://www.baidu.com

這個URL應該是Post獲取的,可以用瀏覽器直接訪問,如果瀏覽器返回401讓你登錄,那一定是你的client_id 或者client_secret有問題,我就是client_secret弄錯了 浪費了我一下午的時間,這個參數就是在上面的json文件裏面配置的.

獲取到token以後就可以獲取user_profile了。
有問題請留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章