先放上官网文档地址:.1.x/index.html
环境:jdk1.8、tomcat8.5、maven3.3、windows/mac操作系统
1、下载Overlay
通过阅读官网文档(.1.x/planning/Getting-Started.html)了解到官方建议我们:
It is recommended to build and deploy CAS locally using the WAR Overlay method.
通过使用一个名叫Overlay的项目来生成一个可以直接用的war包,来部署服务端,于是我们先下载这个项目,官网给出了两个构筑格式的:
我这里使用Maven的,下载地址:,或者使用我上传的网盘地址下载:
链接:
提取码:fm2o
2、构筑Overlay
下载下来的Overlay默认配置就可以直接构筑能用的war包,直接使用它下边的build脚本执行
build.sh package
第一次构筑比较慢,可以在pom的repositories里加一个ali源,构筑会快一些。
构筑完后在target下找到一个war包,放到你的tomcat8.5(官方建议8.0以上版本,我建议使用8.5,如果你用8.0跑不起来,记得换成8.5以上版本)下跑起来试试吧:
localhost:8080/cas/login 默认账号:casuser 默认密码:Mellon 目前的配置仅有这一个用户
第一次会有两个红色警告,一个就是说你没用HTTPS登录,另一个就是你现在只有一个写死的用户,目前这个服务端只能看看,没什么实际用途。
别急,我们下边开始解决这两个问题。
3、生成真正有用的服务端
这一节里面我们有两件事情要做:
第一个就是做一个keystore,Tomcat配置HTTPS访问的时候会用到;
第二个就是把我们的用户改成从数据库的表中读取的。
先说keystore:
使用jdk自带的keytool即可生成keystore,命令如下:
keytool -genkey -alias cas -keyalg RSA -keysize 2048 -keypass 123456 -storepass 123456 -keystore D:/liuyx.keystore -dname "CNample,OU=liuyx,O=liuyx,L=JiNan,ST=JiNan,C=CN"
别名密码什么的都可以改,红色地方可以自行修改,需要指出的是:
CNample这段内容,后边配置客户端的时候需要用到,一定要确保能通过这个域名访问到你的CAS服务端。
并且不能使用IP作为域(上文的CN),使用IP虽然可以生成keystore,但是在客户端使用的时候,如果服务端地址配置成IP会报错。
这里我的CAS服务端是部署在本地的,所以需要做一个本地映射——
用管理员身份修改C:WindowsSystem32driversetchosts文件,mac系统在/etc/hosts下修改,在其最后加上以下内容:
127.0.0.1 ample
这样,我们在本地访问这个域名,其实访问的就是我们本机了。
再说https:
Tomcat8.5配置https的方式相较之前的版本有所调整,不过也差不太多,修改l文件,如下:
<Connector port="8443" protocol="http11.Http11NioProtocol"maxThreads="150" SSLEnabled="true"><SSLHostConfig><Certificate certificateKeystoreFile="D:/liuyx.keystore"type="RSA" certificateKeystoreType="JKS" certificateKeystorePassword="123456"/></SSLHostConfig></Connector>
注意,别忘了把8080端口的Connector注释掉:
<!-- <Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />-->
最后是从数据库中读取用户:
这里我们要做这么几件事:
1、在pom中引入数据库相关的jar包,注释掉用不到的jar包
pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .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><!--STEP1 注释掉无用组件<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>org.springframework.boot.loader.WarLauncher</mainClass><addResources>true</addResources></configuration></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>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF</manifestFile></archive><overlays><overlay><groupId>org.apereo.cas</groupId><artifactId>cas-server-webapp${app.server}</artifactId></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><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></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.36</version></dependency><!--数据库认证相关 end--><!--<dependency><groupId>org.jasig.cas</groupId><artifactId>cas-server-core-authentication</artifactId><version>4.2.7</version></dependency>--><!--<dependency><groupId>org.apereo.cas</groupId><artifactId>cas-server-core-util</artifactId><version>${cas.version}</version></dependency>--></dependencies><properties><cas.version>5.1.3</cas.version><!--STEP3 修改版本,高版本目前暂时没有相应的JDBC支持--><springboot.version>1.5.3.RELEASE</springboot.version><!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver --><app.server>-tomcat</app.server> <mavenpiler.source>1.8</mavenpiler.source><mavenpiler.target>1.8</mavenpiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><repositories><repository><id>sonatype-releases</id><url>/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>sonatype-snapshots</id><url>/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository><repository><id>shibboleth-releases</id><url>;/url></repository><repository><id>spring-milestones</id><url>;/url></repository></repositories><!--STEP4 注释掉无用组件<profiles><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</pgpKeyServer><pgpKeysCachePath>${settings.localRepository}/pgpkeys-cache</pgpKeysCachePath><scope>test</scope><verifyPomFiles>true</verifyPomFiles><failNoSignature>false</failNoSignature></configuration></plugin></plugins></build></profile></profiles>-->
</project>
注意这里我用的cas.version是5.1.3,是因为现在的maven仓库里没有更高版本的cas-server-support-jdbc和cas-server-support-jdbc-drivers,为了保持统一选用的5.1.3,你如果能处理好这些jar包,也可以用高版本。
2、修改配置文件
观察刚才war包解压出的文件,可以在WEB-INF下发现很多配置文件,
通过阅读:.1.x/installation/Configuration-Server-Management.html 得知,我们可以通过修改其中一个application.properties配置文件来使服务端支持从数据库的某张表来验证用户。
具体的配置信息也可以从.1.x/installation/Configuration-Server-Management.html 来了解。
为了防止再次打war包的时候,修改的配置被覆盖,我在cas-overlay这个maven项目下新建了src,resources等目录,然后把配置文件复制到相应的目录进行修改,结构如下:
当然你也可以直接在war包解压出来的内容上改。
修改后的application.properties内容如下(在原基础上修改,注释掉一部分用不到的东西):
#STEP 3 在TOMCAT8.5中跑一个模板然后将其war包中解压出来的的application.properties复制出来,放到手动创建的src下的resources里面##
# CAS Server Context Configuration
#
t-path=/cas
server.port=8443#STEP 5添加认证服务
cas.serviceRegistry.initFromJson=true#STEP 4签发证书,如果是用spring boot之类嵌入式的容器,则需要改这里的配置,如果是直接部在tomcat中,则需要把tomcat改成https的
server.ssl.key-store=file:/etc/cas/thekeystore/a.ketstore
server.ssl.key-store-password=123456
server.ssl.key-password=123456
# server.ssl.ciphers=
# server.ssl.client-auth=
abled=true
# server.ssl.key-alias=
# server.ssl.key-store-provider=
# server.ssl.key-store-type=
# server.ssl.protocol=
# ust-store=
# ust-store-password=
# ust-store-provider=
# ust-store-type=#server.max-http-header-size=2097152
#server.use-forward-headers=true
#tion-timeout=20000
#include-stacktrace=at.max-http-post-size=2097152
#at.basedir=build/tomcat
#abled=true
#at.accesslog.pattern=%t %a "%r" %s (%D ms)
#at.accesslog.suffix=.log
#at.max-threads=10
#at.port-header=X-Forwarded-Port
#at.protocol-header=X-Forwarded-Proto
#at.protocol-header-https-value=https
#-ip-header=X-FORWARDED-FOR
#at.uri-encoding=ding.charset=UTF-8
abled=true
ding.force=true##
# CAS Cloud Bus Configuration
#
spring.abled=false
# spring.abled=true
# spring.v.enabled=true
# spring.cloud.bus.destination=CasCloudBus
# spring.cloud.abledabled=false
endpoints.sensitiveabled=false
abled=abled=true
les=ACTUATOR,ADMIN
management.security.sessions=if_required
t-path=/status
management.add-application-context-header=falsesecurity.basic.authorize-mode=role
abled=false
security.basic.path=/cas/status/**##
# CAS Web Application Session Configuration
#
server.session.timeout=300
kie.http-only=true
acking-modes=COOKIE##
# CAS Thymeleaf View Configuration
#
ding=UTF-8
spring.thymeleaf.cache=false
de=HTML
##
# CAS Log4j Configuration
#
# fig=file:/etc/l
t-parameters.isLog4jAutoInitializationDisabled=true##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true##
# CAS Authentication Credentials
#
#STEP4 注释掉写死的用户 改用jdbc的用户 START
#cas.authn.accept.users=casuser::Melloncas.authn.jdbc.query[0].sql=select password 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].driverClasssql.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#cas.authn.jdbc.query[0].pe=DEFAULT
#cas.authn.jdbc.query[0].peample.CustomPasswordEncoder
#cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
#cas.authn.jdbc.query[0].dingAlgorithm=MD5#cas.authn.jdbc.query[0].passwordEncoder.secret=
#cas.authn.jdbc.query[0].passwordEncoder.strength=16# cas.authn.jdbc.query[0].principalTransformation.suffix=
# cas.authn.jdbc.query[0].principalTransformation.caseConversion=NONE|UPPERCASE|LOWERCASE
# cas.authn.jdbc.query[0].principalTransformation.prefix=
# STEP4 END##
# CAS Delegated Authentication
#
#cas.authn.pac4j.bitbucket.clientName=Bitbucket
#cas.authn.pac4j.dropbox.clientName=Dropbox
#cas.authn.pac4j.facebook.clientName=Facebook
#cas.authn.pac4j.foursquare.clientName=Foursquare
#cas.authn.pac4j.github.clientName=Github
#cas.le.clientName=Google
#cas.authn.pac4j.linkedIn.clientName=LinkedIn
#cas.authn.pac4j.paypal.clientName=PayPal
#cas.authn.pac4j.twitter.clientName=Twitter
#cas.authn.pac4j.yahoo.clientName=Yahoo
#cas.authn.pac4j.windowsLive.clientName=Windows Live
#cas.authn.pac4j.wordpress.clientName=WordPress#多属性
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 password 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].driverClasssql.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
其中加粗标蓝的都是我修改的内容,简单说一下:
cas.authn.accept.users=casuser::Mellon这个配置记得删掉,这就是那个写死的用户
cas.authn.jdbc.query[0]这些配置就是数据库验证相关的内容
在cas.authn.jdbc.query[0].sql中,程序会把你登录时输入的用户名作为参数传进去
cas.authn.jdbc.query[0].fieldPassword则是指明那一列对应的是你输入的密码,目前没有做MD5,怎么做MD5下一篇一起说。
到此为止删除tomcat下的war,重新package到tomcat,修改application.properties,启动即可
注意:
1.新的tomcat没有mysql的jar包mysql-connector-java-8.0.11放入tomcat/lib下面
2.高版本的需要在application.properties更改驱动为sql.cj.jdbc.Driver
3.需要加上时区:&serverTimezone=Asia/Shanghai
4.可能mac的etc目录需要权限,我们可以赋予最大权限:sudo chmod -R 777 目录名
5.默认google是http,我们已经做了证书,访问需要:8443/cas/login,不然出现一下错误
Bad Request This combination of host and port requires TLS
6.未认证授权的服务:
CAS 5.x 默认情况下不支持HTTP的客户端接入,建议客户端采用HTTPS协议。
不过,确实无法升级为HTTP的,那也可以把CAS Server开启支持HTTP的客户端接入。
具体开发的方法如下:
·1. 修改http支持的配置
打开文件:casWEB-INFclassesservicesHTTPSandIMAPS-10000001.json
1 { 2 "@class" : "org.apereo.cas.services.RegexRegisteredService", 3 "serviceId" : "^(https|imaps)://.*", 4 "name" : "HTTPS and IMAPS", 5 "id" : 10000001, 6 "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.", 7 "evaluationOrder" : 10000 8 }
第三行改为 (https|http|imaps)
即修改后的 文件为
1 { 2 "@class" : "org.apereo.cas.services.RegexRegisteredService", 3 "serviceId" : "^(https|http|imaps)://.*", 4 "name" : "HTTPS and IMAPS", 5 "id" : 10000001, 6 "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.", 7 "evaluationOrder" : 10000 8 }
·2. 启用记载JSON配置文件
打开文件casWEB-INFclassesapplication.properties
增加如下配置
重新启动cas就ok了
本文发布于:2024-02-03 00:21:58,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170689091647426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |