上一篇文章咱们对Apereo CAS有了简要的了解,这篇文章咱们将动手练习Apereo CAS。主要是CAS单机版的搭设,用户信息存储到数据库,以及dashboard的使用作这些尝试的时候,Apereo CAS比较稳定的版本是5.3.x,使用若是想按照这个文章搭设的话,最好采用相同的版本html
Apereo CAS秉承耶鲁的自由文化传统,整个产品高度自由化,哪哪都提供了极其灵活的使用方式。好比单机版的部署,通常的软件提供的单机版都是下载一来,运行某个文件就直接开跑的。Apereo就不一样,即便是单机版,也要配置一些内容才能够运行的。mysql
不仅仅是配置,单机版的代码实现也是能够改的,并且还能够很优雅地改,就是能够在不修改原来代码的前提下进行修改。Apereo CAS采用了Maven的overlayer 特性,提供了一份CAS的overlayer或者叫template,咱们能够从下载一份layer ,而后在里面按照约定的方式,实现功能覆盖Apereo CAS提供的类,或者配置文件。git
git clone https://github.com/apereo/cas-overlay-template
这是Apereo CAS官方提供的一个overlay,你们也能够下载使用其余组织提供的overlay。该项目的目录结构以下:github
C:\githome\github\cas\cas-server>ls -l total 1220 -rw-r--r-- 1 NOTECH 1049089 11560 Jan 25 14:25 LICENSE.txt -rw-r--r-- 1 NOTECH 1049089 2768 Jan 25 14:28 README.md -rw-r--r-- 1 NOTECH 1049089 4353 Jan 25 14:28 build.cmd -rwxr-xr-x 1 NOTECH 1049089 5608 Jan 25 14:28 build.sh drwxr-xr-x 1 NOTECH 1049089 0 Jan 25 14:25 etc drwxr-xr-x 1 NOTECH 1049089 0 Jan 25 14:28 maven -rwxr-xr-x 1 NOTECH 1049089 7332 Jan 25 14:28 mvnw -rw-r--r-- 1 NOTECH 1049089 5839 Jan 25 14:28 mvnw.bat -rw-r--r-- 1 NOTECH 1049089 9458 Jan 28 10:15 pom.xml drwxr-xr-x 1 NOTECH 1049089 0 Jan 25 14:31 src
其实就是一个简单的maven项目,多了一个etc的目录,而后pom文件里面有一个cas-server-webapp
的overlayer依赖。这时咱们能够直接跑mvn package
, 同样会生成相应的cas包,只是这个包跑不起来,由于cas须要一些配置才能起来的。web
前面说了overlayer会按照目录路径进行覆盖,也就是若是overlayer的项目里面有文件路径相同,那么打包的时候就会进行覆盖。而上一篇blog说了,Apereo CAS是基于springboot的开发的,那么咱们要覆盖对应的配置文件,那就新建src\main\resources
目录。spring
首先,Apereo CAS做为一个安全的统一认证中心,那么自己也要安全的吧。全部它提供了HTTPS的连接方式,也就意味着咱们须要提供一个keystore。命令行打开目录cas-server/src/main/resources/etc/cas
,执行如下命令生成对应的keystoresql
keytool -genkey -keyalg RSA -alias thekeystore -keystore thekeystore -storepass changeit -validity 360 -keysize 2048
changeit
是这个keystore的密码,最好改为你本身的密码,固然,做为demo用这个也是能够的数据库
接着咱们要把这个keystore导成证书给客户端用:apache
keytool -export -alias thekeystore -file thekeystore.crt -keystore thekeystore
如今咱们要把这个导出来的证书导进去JVM里面安全
keytool -import -alias thekeystore -storepass changeit -file thekeystore.crt -keystore "C:\Program Files\Java\jdk1.8.0_101\jre\lib\security\cacerts" keytool -import -alias thekeystore -storepass changeit -file thekeystore.crt -keystore "C:\Program Files\Java\jre1.8.0_101\lib\security\cacerts"
接着咱们把证书的路径,CAS启动端口等信息配置到springboot标准的配置文件application.properties里面。
server.context-path=/cas server.port=6443 server.ssl.key-store=classpath:/etc/cas/thekeystore server.ssl.key-store-password=changeit server.ssl.key-password=changeit
好了,打包build package
,而后build run
跑一下看看。应该能够看到CAS的默认登陆页面 https://localhost:6443/cas
好了,到这时可能发现:天啦噜!用哪一个用户能够登陆啊? 从头至尾都没有用户信息的配置,而按照咱们所了解的Apereo CAS的尿性,它可不会有什么默认值的。为简单试玩一下,咱们能够直接在上面的application.properties文件里面,直接hardcode一个用户在里面,以下:
cas.authn.accept.users=casuser::Mellon
从新build package
,build run
,打开登陆页面 https://localhost:6443/cas, 输入用户名casuser,密码Mellon,应该就能够登陆成功了
把用户信息hardcode在配置文件显然是很helloworld的作法,CAS提供了不少用户信息存储方式,有各类DB,LDAP等。具体能够参考官网的配置文件,有很详细的说明 https://apereo.github.io/cas/...
此次咱们采用的是MYSQL的连接方式。首先咱们要在overlayers的pom文件里面添加JDBC的support,以下:
<dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-jdbc</artifactId> <version>${cas.version}</version> </dependency> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-jdbc-drivers</artifactId> <version>${cas.version}</version> </dependency>
而后仍是在原来的application.properties文件里面,把authentication的相关配置写上:
cas.authn.jdbc.query[0].sql=select * from cms_auth_user where user_name=? cas.authn.jdbc.query[0].healthQuery= cas.authn.jdbc.query[0].isolateInternalQueries=false cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/CASTEST?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false 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.MySQLDialect 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=root #cas.authn.jdbc.query[0].ddlAuto=create-drop cas.authn.jdbc.query[0].maxAgeDays=180 cas.authn.jdbc.query[0].password=123456 cas.authn.jdbc.query[0].autocommit=false cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver cas.authn.jdbc.query[0].idleTimeout=5000
这里只是列出了一些必要的配置,好比driver.class是什么,query的语句是什么,链接哪一个数据库等等。配置成功后,咱们能够在数据库里面插入一两个用户试试
Apereo CAS提供了一个监控中心, 当你们登陆CAS成功后,满怀但愿地点击dashboard
的连接时,展现给你们的是冷冷的 "Access Denied"页面! 无良啊无良,高度地自由是有代价的!!! CAS认为你们默认是不须要这个功能的,因此默认是关闭的! 咱们须要经过配置文件打开这个功能! 你觉得只要打开endpoints.enabled=true
就能够了吗? 咱们把CAS想简单的,这家伙龟毛到每个监控的功能都须要独立打开的!因此就有了下面长长的一个配置文件!
endpoints.enabled=true endpoints.sensitive=false endpoints.restart.enabled=false endpoints.shutdown.enabled=false management.security.enabled=true management.security.roles=ACTUATOR,ADMIN management.security.sessions=if_required management.context-path=/status management.add-application-context-header=false security.basic.authorize-mode=role security.basic.enabled=false security.basic.path=/cas/status/** cas.adminPagesSecurity.ip=.+ cas.monitor.endpoints.dashboard.enabled=true cas.monitor.endpoints.dashboard.sensitive=false cas.monitor.endpoints.discovery.enabled=true cas.monitor.endpoints.discovery.sensitive=false cas.monitor.endpoints.auditEvents.enabled=true cas.monitor.endpoints.auditEvents.sensitive=false cas.monitor.endpoints.authenticationEvents.enabled=true cas.monitor.endpoints.authenticationEvents.sensitive=false cas.monitor.endpoints.configurationState.enabled=true cas.monitor.endpoints.configurationState.sensitive=false cas.monitor.endpoints.healthCheck.enabled=true cas.monitor.endpoints.healthCheck.sensitive=false cas.monitor.endpoints.loggingConfig.enabled=true cas.monitor.endpoints.loggingConfig.sensitive=false cas.monitor.endpoints.metrics.enabled=true cas.monitor.endpoints.metrics.sensitive=false cas.monitor.endpoints.attributeResolution.enabled=true cas.monitor.endpoints.attributeResolution.sensitive=false cas.monitor.endpoints.singleSignOnReport.enabled=true cas.monitor.endpoints.singleSignOnReport.sensitive=false cas.monitor.endpoints.statistics.enabled=true cas.monitor.endpoints.statistics.sensitive=false cas.monitor.endpoints.trustedDevices.enabled=true cas.monitor.endpoints.trustedDevices.sensitive=false cas.monitor.endpoints.status.enabled=true cas.monitor.endpoints.status.sensitive=false cas.monitor.endpoints.singleSignOnStatus.enabled=true cas.monitor.endpoints.singleSignOnStatus.sensitive=false cas.monitor.endpoints.springWebflowReport.enabled=true cas.monitor.endpoints.springWebflowReport.sensitive=false cas.monitor.endpoints.registeredServicesReport.enabled=true cas.monitor.endpoints.registeredServicesReport.sensitive=false cas.monitor.endpoints.configurationMetadata.enabled=true cas.monitor.endpoints.configurationMetadata.sensitive=false
这里是实在受不了CAS的配置粒度细微到怀疑人生,因此adminSercurity没有打开,放开给全部的IP全部的用户,只要登陆成功后均可以访问