AD域登录验证
AD域登录验证
作者:Grey
原文地址:http://www.cnblogs.com/greyzeng/p/5799699.html
需求
系统在登录的时候,需要根据用户名和密码验证连接域服务器进行验证此用户是否为域用户。
条件
- 域服务器地址:x.x.x.x
- 域验证端口:xxx
- AD域为:DC=adservice,DC=com
- 某个域用户是:abc@adservice.com 密码:abc123.
实现
Java版
ADAuthJava.java
package com.hui.advalidationdemo;
import static com.hui.advalidationdemo.constant.ApplicationConstants.buildADPath;
import static com.hui.advalidationdemo.constant.ApplicationConstants.getConfig;
import static javax.naming.Context.INITIAL_CONTEXT_FACTORY;
import static javax.naming.Context.PROVIDER_URL;
import static javax.naming.Context.SECURITY_AUTHENTICATION;
import static javax.naming.Context.SECURITY_CREDENTIALS;
import static javax.naming.Context.SECURITY_PRINCIPAL;
import java.util.Hashtable;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class ADAuthJava {
public static boolean authenticate(String username, String password) {
DirContext ctx = null;
Hashtable<String, String> HashEnv = initADServer(username, password);
try {
ctx = new InitialDirContext(HashEnv);
System.out.println("Authenticate Success!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (null != ctx) {
try {
ctx.close();
ctx = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private static Hashtable<String, String> initADServer(String username, String password) {
String adPath = buildADPath(username);
Hashtable<String, String> HashEnv = new Hashtable<String, String>();
HashEnv.put(SECURITY_AUTHENTICATION, "simple");
HashEnv.put(SECURITY_PRINCIPAL, adPath);
HashEnv.put(SECURITY_CREDENTIALS, password);
HashEnv.put(INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
HashEnv.put("com.sun.jndi.ldap.connect.timeout", "3000");
HashEnv.put(PROVIDER_URL, getConfig("ad.url"));
return HashEnv;
}
}
单元测试:ADAuthJavaTest.java
package com.hui.advalidationdemo;
import static com.hui.advalidationdemo.ADAuthJava.authenticate;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ADAuthJavaTest {
@Test
public void testAuthenticate() {
assertTrue(authenticate("abc", "abc123."));
}
}
Spring版(附源码,Maven项目)
- Spring 版本:3.2.3.RELEASE
- spring-ldap-core 版本:2.0.2.RELEASE
- JDK1.7+
pom.xml
<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>com.hui</groupId>
<artifactId>advalidationdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>advalidationdemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.acegisecurity</groupId>
<artifactId>acegi-security</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
applicationContext-ldap.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>classpath:config.properties</value></property>
</bean>
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${ad.url}" />
<property name="base" value="${ad.base}" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="adDao" class="com.hui.advalidationdemo.ADAuthSpring">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
</beans>
ADAuthSpring.java
package com.hui.advalidationdemo;
import static com.hui.advalidationdemo.constant.ApplicationConstants.buildADPath;
import static org.acegisecurity.ldap.LdapUtils.closeContext;
import javax.naming.directory.DirContext;
import org.springframework.ldap.core.LdapTemplate;
public class ADAuthSpring {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public boolean authenticate(String userName, String password) {
DirContext ctx = null;
String distinguishedName = null;
distinguishedName = buildADPath(userName);
System.out.println("userName:" + userName + " map distinguishedName:" + distinguishedName);
try {
distinguishedName = buildADPath(userName);
System.out.println("userName:" + userName + " map distinguishedName:" + distinguishedName);
ctx = ldapTemplate.getContextSource().getContext(distinguishedName, password);
System.out.println("authenticate success distinguishedName:" + distinguishedName + " userName:" + userName);
return true;
} catch (Exception e) {
System.out.println("authenticate fail distinguishedName:" + distinguishedName + " userName:" + userName);
return false;
} finally {
closeContext(ctx);
}
}
}
config.properties
# AD Validation#
ad.url=ldap://x.x.x.x:xxx
ad.base=DC=adservice,DC=com
ad.path.template=%s@adservice.com
单元测试:
ADAuthSpringTest.java
package com.hui.advalidationdemo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-ldap.xml" })
public class ADAuthSpringTest {
@Autowired
public ADAuthSpring adValidation;
@Test
public void testAuth() {
Assert.assertTrue(adValidation.authenticate("abc", "123abc."));
}
}
ApplicationConstants.java
package com.hui.advalidationdemo.constant;
import static java.lang.String.format;
import static java.lang.Thread.currentThread;
import static org.apache.commons.lang3.StringUtils.isBlank;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
public class ApplicationConstants {
private static final String CONFIG_FILE = "config.properties";
private static Map<String, Object> configs = new HashMap<String, Object>();
private static final Logger log = Logger.getLogger(ApplicationConstants.class);
static {
InputStream in = null;
Properties p = new Properties();
try{
in = currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE);
p.load(in);
for(Object k : p.keySet()){
String key = (String) k;
configs.put( key, p.getProperty(key));
}
log.info("config.properties is loaded!" );
} catch (IOException e){
log.error("Unable to read config.properties");
} finally{
if(in != null)
try {
in.close();
} catch (IOException e) {
log.error("Unable to close inputstream");
}
}
}
public static String getConfig(String key){
return (String) configs.get(key);
}
public static String buildADPath(String userName) {
String adPathTemplate = getConfig("ad.path.template");
if (isBlank(adPathTemplate)) {
log.error("ad.path template do not exist in config.properties please config it");
return null;
}
log.debug("ad.path template is "+adPathTemplate);
try {
String adPath = format(adPathTemplate, userName);
log.debug("adPath is:"+adPath);
return adPath;
} catch (Exception e) {
log.error("ad path template format error");
return null;
}
}
}
注意:在测试的时候需要将x.x.x.x,xxx,abc,123abc.替换成相应的域服务器ip,域服务器端口,域用户名,域用户密码
源码:https://github.com/GreyZeng/demo/tree/master/advalidation
更多:http://docs.spring.io/spring-ldap/docs/1.2.0/reference/
AD域登录验证的更多相关文章
- AD用户登录验证,遍历OU(LDAP)
先安装python-ldap模块 1.验证AD用户登录是否成功 import sqlite3,ldap domainname='cmr\\' username='zhangsan' ldapuser ...
- ASP.NET Core AD 域登录
在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...
- ASP.NET Core AD 域登录 (转载)
在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...
- AD域账号验证
public partial class _Default : Page { [DllImport("advapi32.dll")] private static extern b ...
- java集成微软的ad域,实现单点登录
1.ad域介绍: windos server 2008R2服务器下的ad域,见下图(我是在虚拟机安装到windos server) 2.连接ad域代码:(里面代码自行修改) public Result ...
- JAVA 通过LDAP获取AD域用户及组织信息
因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...
- Linux AD 身份统一验证(SSO)
http://www.toxingwang.com/linux-unix/linux-admin/584.html Linux+samba-winbind+AD实现统一认证 2013年04月27日 ⁄ ...
- Samba通过ad域进行认证并限制空间大小《转载》
本文实现了samba服务被访问的时候通过windows域服务器进行用户名和密码验证;认证通过的用户可以自动分配500M的共享空间;在用户通过windows域登陆系统的时候可以自动把这块空间映射成一块硬 ...
- springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、
流程: 1.用户调登录接口,传用户名和密码2.用户名和密码在ad验证,验证通过后,返回当前用户的相关信息.(注:ldap为java自带的api不需要maven引入其他的)3.根据返回的用户信息,实现自 ...
随机推荐
- js判断只能输入数字和只能输入
JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'' ...
- java学习之面向对象(2)
在描述事物的时候,该事物已存在就具备的一些内容,这是我们就可以把它们都定义在构造函数中,那么什么是构造函数呢? 构造函数就是构建创造对象时调用的函数,它可以给对象进行初始化.构造函数与类名相同,第一个 ...
- 非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛
非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛 Glenn Berry 大牛会对这个脚本持续更新 -- SQL Server 2012 Diagnost ...
- 简单明了区分escape、encodeURI和encodeURIComponent
一.前言 讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它们不是同一类 简单来说,escape是对字符串(string)进行编码(而另外两种 ...
- WPF RichTextBox 做内容展示框 滚动条控制判定是否阅读完成
一.项目背景: 最近,做项目,因为是金融项目,客户登录交易的时候,有一个提示框,就是告知客户要“入市需谨慎”等等,想必大家都遇到这样的场景,当然,这种提示是没人会看的,不过作为交易所,这样的提示又必不 ...
- SQL Server 深入解析索引存储(下)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/非聚集索引 概述 非聚集索引与聚集索引具有相同的 B 树结构,它们之间的显著差别在于以下两点: 基础表的数据行不按非 ...
- Module Zero安装
返回<Module Zero学习目录> 使用模板创建(自动方式) 手动安装 核心(领域)层 基础设施层 展示层 这里需要抱歉的是,这里使用的博客园的Markdown语法,代码显示不是很好看 ...
- PHPCMS后台密码忘记解决办法
什么是PHPCMS? PHPCMS是一款网站管理软件.该软件采用模块化开发,支持多种分类方式,使用它可方便实现个性化网站的设计.开发与维护.它支持众多的程序组合,可轻松实现网站平台迁移,并可广泛满足各 ...
- Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.
1.最近使用SVN工具时,Checkout出项目到本地后后,然后将其导入到Intellij idea中开发,在提交svn代码的时候,出现这样的错误:Can't use Subversion comma ...
- CSS学习笔记2-2d变换和过渡属性
前言:今天又是一个周末,心情不错,趁着闲暇之余,把剩下来的CSS3学习的内容全部整理出来,练习用的源码也稍微整理了一下. 2D转换 transform:translate||rotate||scale ...