# CAS 集成LDAP

* [CAS 集成 ldap](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#cas-集成-ldap)
  * [环境介绍](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#环境介绍)
  * [参考](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#参考)
  * [重新构建CAS Server War](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#重新构建cas-server-war)
  * [生成CAS encryption and signing keys](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#生成cas-encryption-and-signing-keys)
  * [更新CMS Server 配置](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#更新cms-server-配置)
  * [单点登录 单点注销](https://www.selinux.tech/architecture/cas/pages/-MUwbzGm-vdR-UblgIg1#单点登录-单点注销)

接下来我们配置 CAS 与本地的LDAP 服务进行集成。

## 环境介绍

* CAS 6.4.0 (小版本之间差异也很大)
* CentOS 7.5
* JDK 11
* Tomcat 9.0

## 参考

* <https://apereo.github.io/cas/6.3.x/planning/Installation-Requirements.html>
* <https://apereo.github.io/cas-management/6.3.x/installation/Configuration-Properties.html#ldap-authorization>
* <https://apereo.github.io/tags/>
* <https://apereo.github.io/2019/02/18/cas61-saml2-idp-gitlab/>

## 重新构建CAS Server War

在 cas-overlay-template 项目中添加下面的依赖文件。

```
implementation "org.apereo.cas:cas-server-support-ldap:${project.'cas.version'}"
```

这是 引入了 LDAP 的support，执行 `gradle clean build` 重新构建，cas.war 包。

## 生成CAS encryption and signing keys

进入到 cas-overlay-template 项目目录下，执行下面的命令,就可以构建一个本地的cas shell 终端了。

```
gradlew downloadShell
java -jar build/libs/cas-server-support-shell-6.4.0-SNAPSHOT.jar
```

敲击 电脑的 tab 键，可以提示命令。

输入`help [COMMAND]` 可以显示命令帮助。

也可以参考 <https://apereo.github.io/2017/10/30/intro-cas-cli-shell/>

然后分别生成下面的四个参数。

```
cas.tgc.crypto.encryption.key=
cas.tgc.crypto.signing.key=
cas.webflow.crypto.signing.key=
cas.webflow.crypto.encryption.key=
```

如果 不会生成也没有关系，后面cas 启动的过程中，能够看到相应的日志，将自动生成的key 拷贝出来，添加到配置文件中，再重新启动也可以。

启动CAS 时候，注意观察 启动日志，会发现有下面这样的WARN 提醒。

![签名信息提醒](/files/-MUwc-B6MMwElfcMkYKY)

如果提前配置好了这些key，或者将日志中的key 复制添加到配置文件中并重新启动CAS之后，这时的日志输出是。

![正确配置key的日志输出](/files/-MUwc-B7CsL-nduqJl9b)

## 更新CMS Server 配置

* <https://apereo.github.io/cas/6.2.x/configuration/Configuration-Properties.html#ldap-authentication>
* <https://apereo.github.io/2019/02/18/cas61-saml2-idp-gitlab/>
* <https://apereo.github.io/cas/5.2.x/installation/Configuration-Properties.html#ldap-authentication-1>

以上三条可以供参考学习。

* **最重要** <https://apereo.github.io/cas/development/authentication/LDAP-Authentication.html#ldap-authentication>

  **提醒**： 这里笔者试了很长一段时间，在网上google了很多的资料，即便是借鉴了其他的成功案例，依然一直出错。直到最后认真查看了最新版本的帮助文档，仔细尝试了一下，才真正的实验成功。这里有一个小小的忠告，CAS的帮助文档，即便是一个小版本的变化，差异也非常大。例如这里部署的是CAS 6.4.0. 我看了 6.3.x 系列的帮助文档，一直出错，并且帮助文档写的不明不白。

编辑 `/etc/cas/config/cas.properties` 文件，添加如下的配置。

```
cas.server.name=http://10.0.41.74:8090
cas.server.prefix=${cas.server.name}/cas

logging.config: file:/etc/cas/config/log4j2.xml
server.port=8090
server.ssl.enabled=false


cas.service-registry.core.init-from-json=true
cas.serviceRegistry.json.location=file:/etc/cas/services

#cas.authn.oauth.grants.resourceOwner.requireServiceHeader=true
#cas.authn.oauth.userProfileViewType=NESTED
#
#cas.authn.policy.requiredHandlerAuthenticationPolicyEnabled=true
#
#cas.authn.attributeRepository.stub.attributes.email=casuser@example.org

# 开启debug 模式
logging.level.org.apereo.cas=DEBUG

# don't allow login of built-in users
cas.authn.accept.users=

## 下面这些key ，如果暂时没有生成，先不用填。启动过程中注意查看日志，会自动生成。
cas.tgc.crypto.encryption.key=xacTHFa9PP9dQPKH4MNiNP9BXoGU3HSsg665vZ_L3yE
cas.tgc.crypto.signing.key=CIcpiVI97d57UmHhiYCSozo2QC8spO6pywdSofBrSJoLxnxWnuVdEYOfevUol9Cu-29GN6eF4RQN-Q8ayR0knQ
cas.webflow.crypto.signing.key=oCFxNGwAcamNBDxNTdWAImA_ujdGXUTEedo8YDRp9iuiAb0spKxvSICRmbiGmCYWA-EbUkDaqXx-KaNJvaR18w
cas.webflow.crypto.encryption.key=mLgVHrGbp4jfj9Sp4_e9uQ

ldap-url=ldap://10.0.41.74:389
ldap-dnformat=uid=%s,ou=People,dc=sugon,dc=com
ldap-base-dn=dc=sugon,dc=com
ldap-bind-dn=cn=ldapadm,dc=sugon,dc=com
ldap-bind-credential=root;123

cas.authn.ldap[0].password-policy.groovy.location=
cas.authn.ldap[0].principal-transformation.groovy.location=
cas.authn.ldap[0].base-dn=${ldap-base-dn}
cas.authn.ldap[0].bind-dn=${ldap-bind-dn}
cas.authn.ldap[0].bind-credential=${ldap-bind-credential}
cas.authn.ldap[0].dn-format=${ldap-dnformat}
cas.authn.ldap[0].ldap-url=${ldap-url}
cas.authn.ldap[0].search-filter=(uid={user})
cas.authn.ldap[0].type=DIRECT
cas.authn.ldap[0].password-encoder.encoding-algorithm=
cas.authn.ldap[0].password-encoder.type=NONE
```

重新启动 CAS，然后就可以在 `http://10.0.41.74:8090/cas/login` 使用ldap用户去登录啦。

## 单点登录 单点注销

关于 单点登录，与单点注销 这里我们是通过 CAS Management 这个服务来进行的配置。

首先新增一个gitlab 服务，将gitlab服务注册到cas server 中。因为我们前面的配置只是设置了gitlab 通过CAS 来登录，还没有实现SSO的目的。

![添加gitlab服务](/files/-MVaM-NyelFrPyK8Z1LZ)

添加退出URL.

![添加退出URL](/files/-MVaM-Nz-Pr9iu97nlNG)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.selinux.tech/architecture/cas/cas-ldap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
