Maven依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>LdapSSO</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LdapSSO</name>
<description>LdapSSO</description>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
<spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2022.0.0.0-RC2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2022.0.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.ldapsso.LdapSsoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

配置文件 bootstrap.properties

# 应用服务 WEB 访问端口
server.port=8787
# 配置
spring.ldap.urls=ldap://localhost:389/
spring.ldap.username=uid=guotong,ou=people,dc=example,dc=com
spring.ldap.password=%tgb6YHN
spring.ldap.base=dc=example,dc=com
# 开启ldap的数据源支持
security.ldap.enable=true
# 配置Username的所属--唯一字段
spring.ldap.ldapAttrKeyName=uid
logging.level.org.springframework.ldap.core=DEBUG spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=nacos
spring.cloud.nacos.config.contextPath=/nacos
# 设置配置中心服务端地址
spring.cloud.nacos.config.server-addr=localhost:8848
# Nacos 配置中心的namespace。需要注意,如果使用 public 的 namcespace ,请不要填写这个值,直接留空即可
spring.cloud.nacos.config.namespace=1c5ae1c6-cac5-40ef-b090-b4e9677ea2aa
# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
spring.application.name=ldapsso-client
spring.profiles.active=dev
spring.cloud.nacos.discovery.group=cloud-test
spring.cloud.nacos.config.group=cloud-test
spring.cloud.nacos.config.file-extension=properties
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=1c5ae1c6-cac5-40ef-b090-b4e9677ea2aa

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class LdapSsoApplication { public static void main(String[] args) {
SpringApplication.run(LdapSsoApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} }

全局异常处理

package com.example.ldapsso.config;

import com.example.ldapsso.pojo.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import java.sql.SQLException; /**
* @description: 全局异常处理:
* @author: GuoTong
* @createTime: 2022-11-27 11:17
* @since JDK 1.8 OR 11
**/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler { // 全局异常拦截
@ExceptionHandler
public Resp handlerException(Exception e) {
e.printStackTrace();
log.error("服务执行异常---->{}", e.getMessage());
return Resp.error(e.getMessage());
} @ExceptionHandler(value = SQLException.class)
public Resp msgMySQLExecuteError(Exception e) {
e.printStackTrace();
log.error("Mysql执行异常");
String message = e.getMessage();
return Resp.error(message);
} @ExceptionHandler(value = HttpMessageNotReadableException.class)
public Resp msgNotFind(Exception e) {
e.printStackTrace();
log.error("请求错误");
String message = e.getMessage();
return Resp.error("请求内容未传递" + message);
}
} ### LDAP配置 ldapConfig ```java
package com.example.ldapsso.config; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource; import java.util.HashMap;
import java.util.Map; @Configuration
public class ldapConfig { @Autowired
private Environment environment; @Bean("ldapContextSource")
public LdapContextSource ldapContextSourceLoad( ) {
Map<String, Object> config = new HashMap();
LdapContextSource ldapContextSource = new LdapContextSource();
// 解决 乱码 的关键一句
config.put("java.naming.ldap.attributes.binary", "objectGUID");
ldapContextSource.setUrl(environment.getProperty("spring.ldap.urls"));
ldapContextSource.setBase(environment.getProperty("spring.ldap.base"));
ldapContextSource.setUserDn(environment.getProperty("spring.ldap.username"));
ldapContextSource.setPassword(environment.getProperty("spring.ldap.password"));
ldapContextSource.setAnonymousReadOnly(false);
ldapContextSource.setPooled(true);
ldapContextSource.setBaseEnvironmentProperties(config);
return ldapContextSource;
} /*
* SpringLdap的javaConfig注入方式
*/
@Bean
public LdapTemplate ldapTemplate() {
LdapTemplate ldapTemplate =null;
try {
System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");
System.setProperty("java.naming.ldap.attributes.binary", "objectSid");
LdapContextSource contextSource = ldapContextSourceLoad();
ldapTemplate = new LdapTemplate(contextSource);
ldapTemplate.setIgnorePartialResultException(true);
}catch (Exception e){
e.printStackTrace();
}
return ldapTemplate;
} }

登录认证接口

@RestController
@RequestMapping("/index")
public class IndexController {
@Autowired
private LdapTemplate ldapTemplate; @GetMapping("/ldap")
public Resp ldapLogin(@RequestParam("loginName") String loginName, @RequestParam("pwd") String pwd) {
EqualsFilter filter = new EqualsFilter("uid", loginName);
boolean authenticate = ldapTemplate.authenticate("", filter.toString(), pwd);
if (!authenticate) {
throw new RuntimeException("Ldap 认证不通过");
}
return Resp.Ok();
}
}

SpringBoot集成LDAP认证登录的更多相关文章

  1. Mantis集成 LDAP 认证

    mantis的用户认证函数Authentication中相关有 $g_login_method MD5 LDAP PLAIN CRYPT CRYPT_FULL_SALT BASIC_AUTH Some ...

  2. C# LDAP认证登录

    LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...

  3. C# LDAP认证登录类参考

    public class LDAPHelper     {         private DirectoryEntry _objDirectoryEntry;         /// <sum ...

  4. SpringBoot集成CAS单点登录,SSO单点登录,CAS单点登录(视频资料分享篇)

    单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很早期的公司 ...

  5. LDAP落地实战(四):Jenkins集成OpenLDAP认证

    前几篇分文章分别介绍了OpenLDAP的部署管理和维护以及svn.git的接入,今天我们再下一城接入jenkins. 前情提要:LDAP系列文章 LDAP落地实战(一):OpenLDAP部署及管理维护 ...

  6. LDAP落地实战(三):GitLab集成OpenLDAP认证

    上一篇文章介绍了svn集成OpenLDAP认证,版本控制除了svn外,git目前也被越来越多的开发者所喜爱,本文将介绍GitLab如何集成openldap实现账号认证 GitLab集成OpenLDAP ...

  7. Django集成OpenLDAP认证

    本文详细介绍了django-auth-ldap的使用方法,参数含义,并提供了示例代码 版本说明 Django==2.2 django-auth-ldap==1.7.0 集成过程 Django集成LDA ...

  8. 本地docker搭建gitlab, 并配置ldap认证

    基于Docker在Mac OS X系统中的部署和设置GitLab的具体过程如下:   1. 安装Docker for Mac (参见https://docs.docker.com/docker-for ...

  9. 基于Springboot集成security、oauth2实现认证鉴权、资源管理

    1.Oauth2简介 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OAu ...

  10. LDAP落地实战(二):SVN集成OpenLDAP认证

    上一篇文章我们介绍了LDAP的部署以及管理维护,那么如何接入LDAP实现账号统一认证呢?这篇文章将带你完成svn的接入验证 subversion集成OpenLDAP认证 系统环境:debian8.4 ...

随机推荐

  1. [转载]「服务」WCF中NetNamedPipeBinding的应用实例

    「服务」WCF中NetNamedPipeBinding的应用实例 WCF中有很多种绑定,根据官方的说法,NetNamedPipeBinding是适用于同一台主机中不同进程之间的通信的. 今天终于实现了 ...

  2. 编写高性能java程序

    为什么你写的程序运行效率不高:为什么你的程序会OutOfMemory:为什么你的程序会经常OutOfMemory:看看你的编程习惯是否符合36计1.JVM 管理两种类型的内存:堆内存(heap),栈内 ...

  3. java基础知识回顾之java Thread类学习(四)--线程的状态以及转化使用的方法介绍

    java基础知识回顾之java Thread类学习(十)--线程的状态以及转化使用的方法介绍      线程的概述:         线程是程序的多个执行路径,执行调度的单位,依托于进程存在.线程不仅 ...

  4. Java多进程多线程处理详解

    在Java编程中,多进程和多线程是两种常见的并发编程技术,用于提高程序的执行效率和响应速度.本文将详细介绍Java中的多进程和多线程处理,包括理论概述和代码示例.通过本文,你将了解如何在Java中实现 ...

  5. moectf2023 web wp

    gas!gas!gas! 直接跑脚本 import requests session=requests.Session() url="http://127.0.0.1:14447" ...

  6. 使用Chainlit快速构建一个对话式人工智能应用体验DeepSeek-R1

    Chainlit是一个开源的 Python 包,用于构建可用于生产的对话式人工智能. DeepSeek-R1 是一款强化学习(RL)驱动的推理模型,解决了模型中的重复性和可读性问题.在 RL 之前,D ...

  7. CBAM注意力模型介绍

    本文分享自天翼云开发者社区<CBAM注意力模型介绍>,作者:Liuzijia 近年来,注意力机制在各项深度学习任务中表现出色.研究表明,人类视觉感知过程中,注意力机制发挥了积极的效果,可以 ...

  8. 智算引领 AI启航,中国电信天翼云助推辽宁数智发展!

    近日,中国电信辽宁公司"智算引领 AI启航"新质生产力赋能辽宁新时代"六地"建设大会在沈阳圆满落幕.辽宁省工业和信息化厅,省国资委,省数据局,省农业农村厅,沈阳 ...

  9. Nacos 从入门到精通-手把手教会你

    Nacos 使用教程 Nacos 是阿里巴巴开源的动态服务发现.配置管理和服务管理平台,旨在帮助您更容易地构建.交付和管理微服务平台.它集成了服务注册与发现.动态配置管理.动态 DNS 服务和服务及元 ...

  10. Flink内存解释

    一.JobManager内存 JobManager 是 Flink 集群的控制单元. 它由三种不同的组件组成:ResourceManager.Dispatcher 和每个正在运行作业的 JobMast ...