我所使用的cas5的功能包括数据库数据验证登录、OAuth2登录、JsonService存储信息、saml协议、ldap协议、rest协议、验证码,监控与审计。

一、cas的部署

进入{Casfile}/cas/build/目录

sudo vim build.gradle

编辑dependencies,添加依赖。

dependencies {
compile “org.apereo.cas:cas-server-support-pac4j-authentication:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-saml:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-webapp:${project.’cas.version’}@war
compile “org.apereo.cas:cas-server-support-jdbc:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-json-service-registry:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-oauth-webflow:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-ldap:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-rest-authentication:${project.’cas.version’}”
compile “org.apereo.cas:cas-server-support-rest:${project.’cas.version’}”
}

保存后回到{Casfile}/目录下

sudo ./gradlew clean build

重新构建,这样就会添加所有的依赖。需要说明的是,compile “org.apereo.cas:cas-server-support-jdbc:${project.’cas.version’}” 这个依赖基本上包括了常用的数据库的依赖,但是不包括oracle数据库的依赖,所以oracle的依赖ojdbc7.jar要单独下载。上面的依赖不包括验证码的依赖,关于验证码的问题会在以后的文章中说明。

构建成功后的cas.war文件位于{CAS-file}/cas/build/libs/目录下。将其拷贝到Tomcat的webapps文件夹,重启Tomcat。

sudo cp cas.war /var/lib/tomcat8/webapps/
sudo service tomcat8 restart

访问https://yourdomain:8443/cas/login 即可看到cas页面。

 

二、cas的属性配置

cas的所有配置均位于/var/lib/tomcat8/webapps/cas/WEB-INF/classes/目录下,其中ojdbc7.jar的依赖包需要放到/var/lib/tomcat8/webapps/cas/WEB-INF/lib/目录下。

配置目录下,application.properties 中配置所有的属性,并且将域名修改为自己的。

大概配置如下:

#以下是数据库部分,采用query的方式验证
cas.authn.jdbc.query[0].sql=select * from  where zjh=?        #数据库查询属性语句
cas.authn.jdbc.query[0].healthQuery=SELECT 1 from table #检查数据库是否可以访问的语句,只要能执行通就可以
cas.authn.jdbc.query[0].isolateInternalQueries=false
cas.authn.jdbc.query[0].url=                                                      #数据库连接语句
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.       #这个位置不同的数据库用不同的dialect
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].user=                                                  #登录数据库的用户名
cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
cas.authn.jdbc.query[0].password=                                        #登录数据库的密码
cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].driverClass=                                     #不同的数据库有不同的driver
cas.authn.jdbc.query[0].idleTimeout=5000
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT

#这里是密码加密 如果数据密码是md5加密的,这里选择default 下面写md5就可以。同时还支持
NONE|DEFAULT|STANDARD|BCRYPT|SCRYPT|PBKDF2|com.example.CustomPasswordEncoder
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=utf8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
cas.authn.jdbc.query[0].passwordEncoder.secret=

#以下是cas返回的属性,对应第一条select语句
cas.authn.attributeRepository.attributes.email=email
cas.authn.attributeRepository.attributes.xm=xm

#以下是获取用户属性
cas.authn.attributeRepository.jdbc.sql=                      #与上方一致
cas.authn.attributeRepository.jdbc.username= zjh        #需要一列做主键
cas.authn.attributeRepository.jdbc.healthQuery=SELECT 1 from table
cas.authn.attributeRepository.jdbc.isolateInternalQueries=false
cas.authn.attributeRepository.jdbc.url=                             #同上方
cas.authn.attributeRepository.jdbc.failFast=true
cas.authn.attributeRepository.jdbc.isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.attributeRepository.jdbc.dialect=                             #同上方
cas.authn.attributeRepository.jdbc.leakThreshold=10
cas.authn.attributeRepository.jdbc.propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.attributeRepository.jdbc.batchSize=1
cas.authn.attributeRepository.jdbc.user=                             #同上方
cas.authn.attributeRepository.jdbc.ddlAuto=update
cas.authn.attributeRepository.jdbc.password=                             #同上方
cas.authn.attributeRepository.jdbc.autocommit=false
cas.authn.attributeRepository.jdbc.driverClass=                             #同上方
cas.authn.attributeRepository.jdbc.idleTimeout=5000
cas.authn.attributeRepository.jdbc.pool.suspension=false
cas.authn.attributeRepository.jdbc.pool.minSize=6
cas.authn.attributeRepository.jdbc.pool.maxSize=18
cas.authn.attributeRepository.jdbc.pool.maxIdleTime=1000
cas.authn.attributeRepository.jdbc.pool.maxWait=2000

#以下是cas返回的属性,对应第一条select语句
cas.authn.attributeRepository.attributes.email=email
cas.authn.attributeRepository.attributes.xm=xm

#以下是用json存储service信息
cas.serviceRegistry.watcherEnabled=true
cas.serviceRegistry.repeatInterval=120000
cas.serviceRegistry.startDelay=15000
cas.serviceRegistry.initFromJson=true
cas.serviceRegistry.config.location=file:/etc/cas/services     #最好单独放一个不会被覆盖的目录

#以下是“Remember me”功能开启
cas.ticket.tgt.rememberMe.enabled=true
cas.ticket.tgt.rememberMe.timeToKillInSeconds=28800

#以下是基于ip和用户名的登录限制,一定时间内超过尝试次数即会跳转到错误页面
cas.authn.throttle.usernameParameter=username
cas.authn.throttle.startDelay=10000
cas.authn.throttle.repeatInterval=20000
cas.authn.throttle.appcode=CAS
cas.authn.throttle.failure.threshold=5
cas.authn.throttle.failure.code=AUTHENTICATION_FAILED
cas.authn.throttle.failure.rangeSeconds=60

#以下是监控与审计页面的开启  位于https://yourdomain:8443/cas/status/dashboard
endpoints.enabled=true
endpoints.sensitive=true
management.contextPath=/status
endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

/# IP address may be enough to protect all endpoints.
/# If you wish to protect the admin pages via CAS itself, configure the rest.
/# cas.adminPagesSecurity.ip=127.0.0.1      #这条是基于ip的审计页面访问限制
cas.adminPagesSecurity.loginUrl=
cas.adminPagesSecurity.service=https://yourdomain/status/dashboard
cas.adminPagesSecurity.users=file:/etc/cas/config/adminusers.properties   #这条添加有权限访问的账户id
cas.adminPagesSecurity.adminRoles=ROLE_ADMIN

cas.adminPagesSecurity.actuatorEndpointsEnabled=true
logging.config=file:/var/lib/tomcat8/webapps/cas/WEB-INF/classes/log4j2.xml      #这条设置cas的日志配置位置。

 

以上都设置好了之后 重启Tomcat,即可登录。

以上配置均可以在这里找到。