【CAS学习之二】部署CAS服务端
环境
apache-tomcat-8.5.45
cas:5.2.6
一、部署CAS
1、部署war包
将上一篇文章中构建生成的cas.war放到tomcat/webapps下
2、启动tomcat并访问 (官方的基础版本中配置了一个用户 casuser/Mellon)
2.1 CAS项目要求用https连接,如果使用http也行,也能登录成功,只是会提示不安全
访问连接:http://localhost:8080/cas
2.2 去除HTTPS
(1)修改WEB-INF/classess/application.properties,添加以下配置
#去除https验证 cas.tgc.secure=false #注册Services中的JSON验证 cas.serviceRegistry.initFromJson=true
(2)修改WEB-INF/classess/services/HTTPSandIMAPS-10000001.json
注册哪些客户端可以访问认证中心
{ "@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 "proxyPolicy" : { "@class" : "org.jasig.cas.services.RegexMatchingRegisteredServiceProxyPolicy", "pattern" : "^(https|imaps|http)://.*" } }
(3)如果客户端之前做过https配置,需要从客户端jre的证书仓库中删掉之前为做HTTPS单点登录加的那个证书
keytool -delete -alias cas -keystore C:/Java/jdk1.8.0_91/jre/lib/security/cacerts
参考:去除HTTPS 和 配置使用HTTP协议访问的服务端
2.3 使用https连接,需要先生成https的加密密匙,用jdk自带的keytool就行。
(1)使用jdk生成https证书
JDK中keytool是一个证书管理工具,可以生成自签名证书。 就是自己生成的证书,并不是官方生成的证书。除非是很正式的项目,否则使用自己签发的证书即可,因为官方生成证书是要花钱滴
命令解释
keytool -genkey -alias cas(别名) -keypass 123456(别名密码) -keyalg RSA(生证书的算法名称,RSA是一种非对称加密算法) -keysize 1024(密钥长度,证书大小) -validity 365(证书有效期,天单位) -keystore E:/cas/keystore/cas.keystore(指定生成证书的位置和证书名称) -storepass 123456(获取keystore信息的密码) - storetype (指定密钥仓库类型) -dname "CN=cas.example.org,(您的名字与姓氏是什么?) OU=example.com,(您的组织单位名称是什么?) O=cas,(您的组织名称是什么?) L=Shenzhen,(您所在的城市或区域名称是什么?) ST=Shenzhen,(您所在的省/市/自治区名称是什么?) C=CN"(该单位的双字母国家/地区代码是什么?)
进入jdk的bin目录下面,打开CMD命令行工具,执行命令:
keytool -genkey -alias cas -keyalg RSA -keysize 2048 -keypass 123456 -storepass 123456 -keystore E:/cas/keystore/cas.keystore -dname "CN=cas.example.org,OU=example.com,O=cas,L=Jinan,ST=Jinan,C=CN"
关于jdk证书,请参考:
(2)同时需要修改 tomcat 的配置文件 server.xml,把加密密匙的相关配置写进去。
<!-- 去掉 http 访问 8080 端口 -->
<!-- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
--> <!-- 用 https 访问 8443 端口 --> <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="200" scheme="https" secure="true" SSLEnabled="true" keystoreFile="E:/cas/keystore/cas.keystore" keystorePass="123456" clientAuth="false" sslProtocol="TLS"/>
修改完成后 重启Tomcat
(3)访问,用户 casuser/Mellon
访问连接:https://localhost:8443/cas
二、改用数据库验证用户名和密码
先来搞清楚application.properties配置文件中各个属性含义,请参考:application.properties
0、本地搭建一个mysql,创建一个数据库castest,新建一个表:cms_auth_user
1、我们这里要拷贝cas.war解压出来的一份cas\WEB-INF\classes\application.properties放到cas-overlay-template\src\main\resources\application.properties,然后作如下修改
## # CAS Server Context Configuration # server.context-path=/cas server.port=8443 # 这些放在services目录的json文件来注册服务了, # 先说一下这里的“服务” 其实指的就是客户端,更准确的说就是它的url,如果一个客户端没有注册到CAS的服务列表, # 那么你通过客户端跳转到CAS,CAS会拒绝这个跳转。 # war包解压出来的services里已经有了两个json文件,其中HTTPSandIMAPS-100xxxxxx.json中有一段: # "serviceId" : "^(https|imaps|http)://.*", # 后边这部分是个正则表达式,为了支持http协议的客户端,我在里面加了个http,代表所有URL符合这个正则表达式的服务都会被当成已经注册的服务 cas.serviceRegistry.initFromJson=true #STEP 签发证书,如果是用spring boot之类嵌入式的容器,则需要改这里的配置,如果是直接部在tomcat中,则需要把tomcat改成https的 #server.ssl.key-store=file:/etc/cas/thekeystore #server.ssl.key-store-password=changeit #server.ssl.key-password=changeit # server.ssl.ciphers= # server.ssl.client-auth= # server.ssl.enabled= # server.ssl.key-alias= # server.ssl.key-store-provider= # server.ssl.key-store-type= # server.ssl.protocol= # server.ssl.trust-store= # server.ssl.trust-store-password= # server.ssl.trust-store-provider= # server.ssl.trust-store-type= server.max-http-header-size=2097152 server.use-forward-headers=true server.connection-timeout=20000 server.error.include-stacktrace=ALWAYS server.compression.enabled=true server.compression.mime-types=application/javascript,application/json,application/xml,text/html,text/xml,text/plain server.tomcat.max-http-post-size=2097152 server.tomcat.basedir=build/tomcat server.tomcat.accesslog.enabled=true server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms) server.tomcat.accesslog.suffix=.log server.tomcat.max-threads=10 server.tomcat.port-header=X-Forwarded-Port server.tomcat.protocol-header=X-Forwarded-Proto server.tomcat.protocol-header-https-value=https server.tomcat.remote-ip-header=X-FORWARDED-FOR server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true ## # CAS Cloud Bus Configuration # spring.cloud.bus.enabled=false # spring.cloud.bus.refresh.enabled=true # spring.cloud.bus.env.enabled=true # spring.cloud.bus.destination=CasCloudBus # spring.cloud.bus.ack.enabled=true endpoints.enabled=false endpoints.sensitive=true 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 Web Application Session Configuration # server.session.timeout=300 server.session.cookie.http-only=true server.session.tracking-modes=COOKIE ## # CAS Thymeleaf View Configuration # spring.thymeleaf.encoding=UTF-8 #这个配置没有在官网找到说明,是自己摸索出来的,服务端的HTML页面使用的是thymeleaf编写(http://www.ultraq.net.nz/thymeleaf/layout) #thymeleaf默认会在启动的时候就把页面的静态内容给放到缓存里,这给我修改CAS默认登录页带来了很大的麻烦,每次修改一点点页面内容,刷新是看不到效果的,只得重启服务,为了方便开发, #我把这个配置设成false,就是说不要缓存。 # #如果你也想修改登录页这里给出几个建议: #1、如果不懂thyeleaf先简单了解一下thymeleaf官网的demo写法; #2、登录页的模板是layout.html,其中有几个js是从谷歌CDN上下载的,而谷歌我们国内又访问不了, #这就会导致登录页加载非常慢,建议先把这几个js本地化; #3、登录页主要是casLoginView.html和fragments目录下的loginform.html; #4、layout.html引入了好几个类似logo.html,footer.html的页面,这几个页面看名字就知道干什么的,建议对他们进行修改,改成自己的 spring.thymeleaf.cache=true spring.thymeleaf.mode=HTML ## # CAS Log4j Configuration # # logging.config=file:/etc/cas/log4j2.xml server.context-parameters.isLog4jAutoInitializationDisabled=true ## # CAS AspectJ Configuration # spring.aop.auto=true spring.aop.proxy-target-class=true ## # CAS Authentication Credentials # # 注释掉写死的用户 改用jdbc的用户 #cas.authn.accept.users=casuser::Mellon 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].password=123456 #cas.authn.jdbc.query[0].ddlAuto=create-drop cas.authn.jdbc.query[0].maxAgeDays=180 cas.authn.jdbc.query[0].autocommit=false cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver cas.authn.jdbc.query[0].idleTimeout=5000 # cas.authn.jdbc.query[0].credentialCriteria= # cas.authn.jdbc.query[0].name= # cas.authn.jdbc.query[0].order=0 # cas.authn.jdbc.query[0].dataSourceName= # cas.authn.jdbc.query[0].dataSourceProxy=false 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 #passwordEncoder.type可以自定义加密方式 #cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT #多属性 #CAS默认返回给客户端的只有用户ID,如果想返回更多的内容就要加上这段内容了,上面这个配置会返回cms_auth_user表下的所有字段。 #如何获取这些字段可以在客户端通过以下代码: #AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); #final Map attributes = principal.getAttributes(); cas.authn.attributeRepository.jdbc[0].singleRow=true cas.authn.attributeRepository.jdbc[0].order=0 cas.authn.attributeRepository.jdbc[0].url=jdbc:mysql://127.0.0.1:3306/castest?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false cas.authn.attributeRepository.jdbc[0].username=user_name cas.authn.attributeRepository.jdbc[0].user=root cas.authn.attributeRepository.jdbc[0].password=123456 cas.authn.attributeRepository.jdbc[0].sql=select * from cms_auth_user where {0} cas.authn.attributeRepository.jdbc[0].dialect=org.hibernate.dialect.MySQLDialect cas.authn.attributeRepository.jdbc[0].ddlAuto=none cas.authn.attributeRepository.jdbc[0].driverClass=com.mysql.jdbc.Driver cas.authn.attributeRepository.jdbc[0].leakThreshold=10 cas.authn.attributeRepository.jdbc[0].propagationBehaviorName=PROPAGATION_REQUIRED cas.authn.attributeRepository.jdbc[0].batchSize=1 cas.authn.attributeRepository.jdbc[0].healthQuery=SELECT 1 cas.authn.attributeRepository.jdbc[0].failFast=true
2、修改pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>org.apereo.cas</groupId> <artifactId>cas-overlay</artifactId> <packaging>war</packaging> <version>1.0</version> <build> <plugins> <!--注释掉无用组件 <plugin> <groupId>com.rimerosolutions.maven.plugins</groupId> <artifactId>wrapper-maven-plugin</artifactId> <version>0.0.4</version> <configuration> <verifyDownload>true</verifyDownload> <checksumAlgorithm>MD5</checksumAlgorithm> </configuration> </plugin> --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${springboot.version}</version> <configuration> <mainClass>${mainClassName}</mainClass> <addResources>true</addResources> <executable>${isExecutable}</executable> <layout>WAR</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warName>cas</warName> <failOnMissingWebXml>false</failOnMissingWebXml> <recompressZippedFiles>false</recompressZippedFiles> <archive> <compress>false</compress> <manifestFile>${manifestFileToUse}</manifestFile> </archive> <overlays> <overlay> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-webapp${app.server}</artifactId> <excludes> <exclude>WEB-INF/classes/application.properties</exclude> <!--<exclude>WEB-INF/classes/bootstrap.properties</exclude>--> <!--<exclude>WEB-INF/classes/log4j2.xml</exclude>--> <!--若要使resources修改的配置文件生效 在这里添加被覆盖的配置文件 带上配置文件在overlay中路径--> </excludes> </overlay> </overlays> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> </plugin> </plugins> <finalName>cas</finalName> </build> <properties> <cas.version>5.2.6</cas.version> <springboot.version>1.5.12.RELEASE</springboot.version> <!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver --> <app.server>-tomcat</app.server> <mainClassName>org.springframework.boot.loader.WarLauncher</mainClassName> <isExecutable>false</isExecutable> <manifestFileToUse> ${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF </manifestFileToUse> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 注释掉 在settings.xml配置阿里云镜像 速度会很快 用下面的地址会很慢 <repositories> <repository> <id>sonatype-releases</id> <url>http://oss.sonatype.org/content/repositories/releases/</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> <repository> <id>sonatype-snapshots</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>shibboleth-releases</id> <url>https://build.shibboleth.net/nexus/content/repositories/releases</url> </repository> </repositories> --> <profiles> <profile> <activation> <activeByDefault>true</activeByDefault> </activation> <id>default</id> <dependencies> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-webapp${app.server}</artifactId> <version>${cas.version}</version> <type>war</type> <scope>runtime</scope> </dependency> <!--STEP2 引入数据库认证相关 start--> <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> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--数据库认证相关 end--> </dependencies> </profile> <!--注释掉无用组件 <profile> <activation> <activeByDefault>false</activeByDefault> </activation> <id>exec</id> <properties> <mainClassName>org.apereo.cas.web.CasWebApplication</mainClassName> <isExecutable>true</isExecutable> <manifestFileToUse></manifestFileToUse> </properties> <build> <plugins> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.3.0</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>echo</goal> </goals> </execution> </executions> <configuration> <echos> <echo>Executable profile to make the generated CAS web application executable.</echo> </echos> </configuration> </plugin> </plugins> </build> </profile> --> <profile> <activation> <activeByDefault>false</activeByDefault> </activation> <id>bootiful</id> <properties> <app.server>-tomcat</app.server> <isExecutable>false</isExecutable> </properties> <dependencies> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-webapp${app.server}</artifactId> <version>${cas.version}</version> <type>war</type> <scope>runtime</scope> </dependency> </dependencies> </profile> <!--注释掉无用组件 <profile> <activation> <activeByDefault>false</activeByDefault> </activation> <id>pgp</id> <build> <plugins> <plugin> <groupId>com.github.s4u.plugins</groupId> <artifactId>pgpverify-maven-plugin</artifactId> <version>1.1.0</version> <executions> <execution> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <pgpKeyServer>hkp://pool.sks-keyservers.net</pgpKeyServer> <pgpKeysCachePath>${settings.localRepository}/pgpkeys-cache</pgpKeysCachePath> <scope>test</scope> <verifyPomFiles>true</verifyPomFiles> <failNoSignature>false</failNoSignature> </configuration> </plugin> </plugins> </build> </profile> --> </profiles> </project>
3、重新打包部署 然后登录认证
(1)打开新的登录页面不再提示:不安全连接和固定用户警告
(2)使用原先默认的casuser登录提示:认证信息无效
(3)使用数据库表里配置的用户和密码登录
(4)注销用户