CAS 5.3 的安装 与 对接LDAP

CAS安装

这里选择安装CAS的5.3版本

进入以下地址,下载模版。

https://github.com/apereo/cas-overlay-template/tree/5.3

将下载的压缩包解压,打开控制台终端,并进入解压后的文件夹路径。运行打包命令,即可生成一个可用的war包。

./build.cmd package

将打包完成的war包放入tomcat中,访问http://[ip]:[port]/cas,即可看到CAS登入界面。

CAS 5.3 的安装 与 对接LDAP

支持https

生成keystore

使用java自带的keystore生成keystore文件,需要注意的是**-dname参数中CN**的值必须是域名,且可以访问到cas服务器。

keytool -genkey -alias cas -keyalg RSA -keysize 2048 -k
eypass 123456 -storepass 123456 -keystore D:/cas.keystore -dname "CN=cas.top,OU=lanniu.top,O=lanniu,L=NanJing,ST=NanJing
,C=CN"

可以修改hosts文件,使得该域名可以被访问到。修改hosts文件后,需要在控制台运行刷新命令使域名生效。

ipconfig /flushdns

将生成好的配置文件上传到cas服务器中,备用。

配置tomcat的server.xml

修改server.xml文件,增加一个连接,需要增加的内容如***意其中的信息要与生成keystore时填写的信息保持一致。

    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="/root/cas.keystore" certificateKeystoreType="JKS" certificateKeystorePassword="123456" type="RSA" />
        </SSLHostConfig>
    </Connector>

修改完毕后,重启tomcat,在浏览器中输入https://[域名]:8443/cas,可以看到如下界面。

CAS 5.3 的安装 与 对接LDAP

与LDAP对接

可以参考官方文档 https://apereo.github.io/cas/5.3.x/installation/LDAP-Authentication.html#ldap-authentication

进入cas_template文件夹路径,找到pom.xml文件并打开,在其中找到这样一个注释。

<!--
    ...Additional dependencies may be placed here...
-->

在注释的上方添加如下的依赖,这个依赖就是为了使得cas可以支持ldap。

<dependency>
     <groupId>org.apereo.cas</groupId>
     <artifactId>cas-server-support-ldap</artifactId>
     <version>${cas.version}</version>
</dependency>

添加依赖并保存后运行打包命令,等待重新生成war包。

./build.cmd package

将war包上传至服务器,替换旧的war包,并重启tomcat。访问https://[域名]:8443/cas,查看cas是否成功启动。

如果启动失败推荐清空maven仓库,重新下载包,重新生成war包,推荐使用可以*的主机。

接下来配置LDAP的连接属性,进入tomcat的webapp文件夹下,如果上面步骤中打包的war包名字就是cas,那么在进入 webapps/cas/WEB-INF/classes 目录下,修改 application.properties 文件。在文件的最下方就是CAS认证相关的属性,首先注释掉CAS写死的用户名和密码,然后配置LDAP连接属性。

##
# CAS Authentication Credentials
#
# cas.authn.accept.users=casuser::Mellon

# 认证方式
cas.authn.ldap[0].type=AUTHENTICATED
# LDAP服务地址,如果支持SSL,地址为 ldaps://127.0.0.1:689
cas.authn.ldap[0].ldapUrl=ldap://127.0.0.1:389
# 是否使用SSL
cas.authn.ldap[0].useSsl=false
# LDAP中基础DN
cas.authn.ldap[0].baseDn=dc=example,dc=org
# 用户名匹配规则,简单的可以只写成uid={user}
cas.authn.ldap[0].searchFilter=(|(uid={user})(mail={user})(mobile={user}))
# CAS用于绑定的DN
cas.authn.ldap[0].bindDn=cn=admin,dc=example,dc=org
# CAS用于绑定的DN的密码
cas.authn.ldap[0].bindCredential=admin
# 登入成功后可以查看到的信息,此条可以不写
cas.authn.ldap[0].principalAttributeList=sn,cn:commonName,givenName,eduPersonTargettedId:SOME_IDENTIFIER

以上的属性已可以支持LDAP成功对接,完整的属性列表如下,每个属性的具体含义可以查看官网 。

https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties-Common.html#ldap-connection-settings
cas.authn.ldap[0].principalAttributeList=sn,cn:commonName,givenName,eduPersonTargettedId:SOME_IDENTIFIER
cas.authn.ldap[0].collectDnAttribute=false
cas.authn.ldap[0].principalDnAttributeName=principalLdapDn
cas.authn.ldap[0].allowMultiplePrincipalAttributeValues=true
cas.authn.ldap[0].allowMissingPrincipalAttributeValue=true
cas.authn.ldap[0].credentialCriteria=

cas.authn.ldap[0].ldapUrl=ldap://127.0.0.1:389
cas.authn.ldap[0].bindDn=cn=admin,dc=example,dc=org
cas.authn.ldap[0].bindCredential=admin

cas.authn.ldap[0].poolPassivator=NONE
cas.authn.ldap[0].connectionStrategy=
cas.authn.ldap[0].providerClass=org.ldaptive.provider.unboundid.UnboundIDProvider
cas.authn.ldap[0].connectTimeout=PT5S
cas.authn.ldap[0].trustCertificates=
cas.authn.ldap[0].keystore=
cas.authn.ldap[0].keystorePassword=
cas.authn.ldap[0].keystoreType=JKS
cas.authn.ldap[0].minPoolSize=3
cas.authn.ldap[0].maxPoolSize=10
cas.authn.ldap[0].validateOnCheckout=true
cas.authn.ldap[0].validatePeriodically=true
cas.authn.ldap[0].validatePeriod=PT5M
cas.authn.ldap[0].validateTimeout=PT5S
cas.authn.ldap[0].failFast=true
cas.authn.ldap[0].idleTime=PT10M
cas.authn.ldap[0].prunePeriod=PT2H
cas.authn.ldap[0].blockWaitTime=PT3S
cas.authn.ldap[0].useSsl=false
cas.authn.ldap[0].useStartTls=false
cas.authn.ldap[0].responseTimeout=PT5S
cas.authn.ldap[0].allowMultipleDns=false
cas.authn.ldap[0].allowMultipleEntries=false
cas.authn.ldap[0].followReferrals=false
cas.authn.ldap[0].binaryAttributes=objectGUID,someOtherAttribute
cas.authn.ldap[0].name=
cas.authn.ldap[0].type=AUTHENTICATED
cas.authn.ldap[0].baseDn=dc=example,dc=org
cas.authn.ldap[0].searchFilter=(|(uid={user})(mail={user})(mobile={user}))

登入成功后,可以看到成功界面。
CAS 5.3 的安装 与 对接LDAP