本文由@iflab-FZ贡献

cas5.0版本采用gradle-over-lay的形式部署。外加界面的简单定制,验证码,cas-management的配置。

由于是凭着之前配置的印象来写文章,难免会有疏漏,欢迎指正。

首先简介CAS。cas是耶鲁大学研发的一套单点登录系统,为多个应用进行统一的用户认证提供了很好地解决方案。它在https协议下,以安全的方式对用户的合法性进行认证,同时与需要用户认证的各应用进行交互,使多个应用共用一套程序进行统一认证。

以下是部署cas可能会用到的网站:

https://apereo.github.io/cas/5.1.x/index.html
Github:https://github.com/apereo
Maillist:https://www.mail-archive.com/[email protected]/

服务器配置

Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-62-generic x86_64)

Tomcat8


Apache相关文件在

SSL配置

 

cas的部署与构建

https://cas.yourdomain.com:8443/cas 即可看到最初的cas界面了。

我所使用的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,即可登录。

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

以下主要讲的是使用CAS Service Management 对使用cas的应用进行管理。

CAS Service Management的部署在这里有详细的步骤。

配置CAS Service Management与已有CAS关联

部署成功之后,前往

目录下

这里写上需要具有访问Service Manager权限的账号,例如

username=notused,ROLE_admin

然后编辑配置文件

vim management.properties

目录下

其余配置与以上的要求基本相同。

Service Management 应用信息配置

添加一个Service

Service ID为目标应用的URL,支持正则表达式。

Service Name为自己定义的应用名称。

Description为应用的说明。

Attribute Policy Options 区域可以选择提供给该应用哪些用户信息。