CAS 单点登录 5.39 版本 LDAP 认证模式 AD域 返回用户信息给客户端

  1. 什么是单点登录就不用和大家说了,相比做个这个项目的童鞋都会去查阅一些资料
    放一张图吧
    .在这里插入图片描述

  2. 最近需要做一个CAS 单点登录返回用户信息的功能,而采用了LDAP认证AD域去返回用户信息,网上查阅了很多资料,因为涉及到版本可能有点高的问题,还有大部分是通过JDBC去连接数据,而AD域的方式去实现很少,没有什么可以参考的价值,废话不多说,这个功能只需要通过配置就可以返回不需要其他的操作.

  3. 5.x版本官方文档的配置,有兴趣的可以去阅读一下:
    https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties-Common.html#ldap-connection-settings

  4. LDAP 认证模式有四种:
    4.1 AD,AUTHENTICATED,DIRECT,ANONYMOUS,因为项目只是用到了AD,所以其他的三中模式不做试验.
    4.2 AD为微软的Active Directory 认证,使用 sAMAccountName 按预定的 dn 格式筛 选,dnFormat 是必备参数.
    4.3 AD是目录服务中的一种,也是以对象为单位,并采用层次结构来组织对象.
    4.4 AD是域的集合,AD可由一个或多个域组成,从域的角度来看,AD是由至少一个域所构成的集合,从AD的角度来看,域则是AD的分区单位.
    4.5 AD域是LDAP的一个应用实例,而不是LDAP本身,AD域的用户,权限管理使用LDAP存储一些数据用来解决域控这个具体问题.AD域提供了相关的用户接口,可以把AD域当做定制的LDAP服务器.AD先实现一个LDAP服务器,然后自己先用这个LDAP服务器实现一个具体应用.

在这里插入图片描述5.LDAP AD 认证主要参数在文件application.properties中配置

# 认证方式
cas.authn.ldap[0].order=0
cas.authn.ldap[0].name=Active Directory
cas.authn.ldap[0].type=AD
# LDAP服务地址,如果支持SSL,地址为 ldaps://xxx.x.x.x:xxx
cas.authn.ldap[0].ldapUrl=ldap://xx.xxx.xx.xxx:xxx
# 是否使用SSL
cas.authn.ldap[0].useSsl=false
cas.authn.ldap[0].useStartTls=false
cas.authn.ldap[0].connectTimeout=5000
# LDAP中基础DN
cas.authn.ldap[0].baseDn=DC=xxx,DC=xxx,DC=xxxx
cas.authn.ldap[0].poolPassivator=NONE
cas.authn.ldap[0].validatePeriod=270
cas.authn.ldap[0].searchFilter=sAMAccountName={user}
cas.authn.ldap[0].subtreeSearch=true
cas.authn.ldap[0].dnFormat=%s@xxxx.xxxx.xxx
cas.authn.ldap[0].bindDn=ldap@xxx.xxxx.xxx
cas.authn.ldap[0].bindCredential=xxxx@xxx
cas.authn.ldap[0].principalAttributeList=cn,objectCategory,accountExpires,distinguishedName
**#defaultAttributesToRelease 定义了 principalAttributeList 中的属性可以发放,即提供客户端获取**
cas.authn.attributeRepository.defaultAttributesToRelease=cn,objectCategory,accountExpires,distinguishedName

6.注册Service的HTTPSandIMAPS-10000001.json中配置返回信息的规则:
官方配置:
https://apereo.github.io/cas/development/integration/Attribute-Release-Policies.html
我并没有配置返回所有信息:
Return All(返回所有)
{
“@class” : “org.apereo.cas.services.RegexRegisteredService”,
“serviceId”: “^(https|imaps)😕/.*”,
“name” : “lxy”,
“id” : 10000001,
“description” : “lxy”,
“evaluationOrder” : 10000,
“attributeReleasePolicy” : {
“@class” : “org.apereo.cas.services.ReturnAllAttributeReleasePolicy”
}

Return Allowed(返回允许的部分属性)
{
“@class” : “org.apereo.cas.services.RegexRegisteredService”,
“serviceId”: “^(https|imaps)😕/.*”,
“name” : “lxy”,
“id” : 10000001,
“description” : “lxy”,
“evaluationOrder” : 10000,
“attributeReleasePolicy” : {
“@class” : “org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy”,
“allowedAttributes” : [ “java.util.ArrayList”, [ “cn”, “mail”, “sn” ] ]
}
}
而我的配置仅仅如此:

 {
  "@class": "org.apereo.cas.services.RegexRegisteredService",
  "serviceId": "^(https|imaps)://.*",
  "name": "lxy",
  "id": 10000001,
  "description": "lxy",
  "evaluationOrder": 10000
}

7.CAS服务端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>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>
                        </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.3.9</cas.version>
        <springboot.version>1.5.18.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>
    
    <dependencyManagement>
        <dependencies>
        	<!-- 依赖管理  CAS的每个版本都提供了它支持的依赖列表。
				在实践中,您不需要为构建配置中的任何依赖项提供一个版本,因为CAS发行版正在为您管理这些版本。
				当升级CAS本身时,这些依赖性也将以一致的方式升级。
				这个依赖列表包含第三方库的组件。这个列表可以作为标准物料清单(BOM)。
				若要将项目配置为从BOM继承,只需设置父级;
				
				并不是每个人都喜欢从BOM继承。您可能需要使用自己的企业标准父级,或者可能更喜欢显式声明所有Maven配置。
				如果不想使用cas-server-support-bom,仍然可以通过使用scope=import
			 -->
			 
			 <!--获取json格式的服务注册配置信息,service是以Json格式时添加此依赖-->
<dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-support-json-service-registry</artifactId>
    <version>${cas.version}</version>
</dependency> 
			 		 
            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-support-bom</artifactId>
                <version>${cas.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>    
    
    <dependencies>
    	<dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
        </dependency>
        <dependency>
		    <groupId>org.apereo.cas</groupId>
		    <artifactId>cas-server-support-jdbc</artifactId>
		</dependency>
		<!-- LDAP 支持 -->
		<dependency>
     <groupId>org.apereo.cas</groupId>
     <artifactId>cas-server-support-ldap</artifactId>
     </dependency>

		<dependency>
		    <groupId>org.apereo.cas</groupId>
		    <artifactId>cas-server-core-authentication</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.apereo.cas</groupId>
		    <artifactId>cas-server-core-authentication-api</artifactId>
		</dependency>
		 <dependency>
		    <groupId>org.apereo.cas</groupId>
		    <artifactId>cas-server-core-webflow</artifactId>
		</dependency>
		
		<dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-core-webflow-api</artifactId>
</dependency>
		
    </dependencies>


	<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>
</project>

8.配置完成进行测试:
在这里插入图片描述
输入用户名和密码:
在这里插入图片描述
成功登陆
启动客户端:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613135831820.png?![在这里插入图片描述](https://img-blog.csdnimg.cn/20200427155633640.png?x-oss-process=image/watermark,type_ZmFuZ3p在这里插入图片描述
成功返回了上面配置中需要返回的用户信息.

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值