完成ldap验证代码!需要将服务器url参数化!
This commit is contained in:
parent
824b0750fb
commit
bcef07f82d
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* @Author: Kane
|
||||
* @Date: 2022-05-25 19:32:18
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-08-28 00:53:09
|
||||
* @FilePath: \cpicxim-servlets\src\main\java\com\cpic\xim\ldap\CpicLDAP.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
package com.cpic.xim.ldap;
|
||||
|
||||
import javax.naming.CommunicationException;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class CpicLDAP
|
||||
{
|
||||
private static String ldapServerUrl = "ldap://10.39.0.205:389";
|
||||
|
||||
public static boolean ldapLogin( String serverURL, String userName, String password )
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
|
||||
if ( userName.endsWith( "@pr.intra.cpic.com.cn" ) == false)
|
||||
{
|
||||
userName += "@pr.intra.cpic.com.cn";
|
||||
}
|
||||
|
||||
DirContext ctx = null;
|
||||
Hashtable<String, String> ldap = new Hashtable<String, String>();
|
||||
|
||||
ldap.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
|
||||
ldap.put( Context.SECURITY_AUTHENTICATION, "simple" );
|
||||
ldap.put( Context.PROVIDER_URL, ldapServerUrl );
|
||||
ldap.put( Context.SECURITY_PRINCIPAL, userName );
|
||||
ldap.put( Context.SECURITY_CREDENTIALS, password );
|
||||
|
||||
try
|
||||
{
|
||||
ctx = new InitialDirContext( ldap );
|
||||
ctx.close();
|
||||
|
||||
result = true;
|
||||
}
|
||||
catch ( CommunicationException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
error.getMessage();
|
||||
|
||||
result = false;
|
||||
}
|
||||
catch ( NamingException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
/**
|
||||
/*
|
||||
* @Author: Kane
|
||||
* @date: 2022-08-27 22:20:38
|
||||
* @Date: 2022-08-27 22:20:38
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-08-27 23:32:36
|
||||
* @FilePath: \cpicxim-servlets\src\main\java\com\cpic\xim\servlets\LdapServlet.java
|
||||
* @LastEditTime: 2022-08-28 01:01:28
|
||||
* @FilePath: \cpicxim-servlets\src\main\java\com\cpic\xim\servlets\ldap\LdapServlet.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
|
||||
package com.cpic.xim.servlets;
|
||||
package com.cpic.xim.servlets.ldap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.BufferedReader;
|
||||
@ -18,6 +18,7 @@ import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
|
||||
public class LdapServlet extends HttpServlet
|
||||
{
|
||||
@ -40,6 +41,9 @@ public class LdapServlet extends HttpServlet
|
||||
writer.write( "{ \"returnMessage\": \"请用post请求调用本接口!\"}" );
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* 响应post请求,将请求体的json字符串转换后,用于ldap验证。
|
||||
*****************************************************/
|
||||
@Override
|
||||
public void doPost( HttpServletRequest request, HttpServletResponse response )
|
||||
throws ServletException, IOException
|
||||
@ -48,6 +52,8 @@ public class LdapServlet extends HttpServlet
|
||||
response.setCharacterEncoding( "UTF-8" );
|
||||
request.setCharacterEncoding( "UTF-8" );
|
||||
|
||||
PrintWriter writer = response.getWriter();
|
||||
|
||||
BufferedReader reader = request.getReader();
|
||||
String line;
|
||||
StringBuilder requestString = new StringBuilder();
|
||||
@ -57,5 +63,23 @@ public class LdapServlet extends HttpServlet
|
||||
requestString.append( line );
|
||||
}
|
||||
|
||||
LdapStuffInfo stuffInfo;
|
||||
|
||||
// 转换json
|
||||
try
|
||||
{
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
stuffInfo = mapper.readValue( requestString.toString(), LdapStuffInfo.class );
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
// 如果转换json失败,不抛出异常。
|
||||
// 返回请求内容解析失败的提示。
|
||||
writer.write( "{ \"returnMessage\": \"请求内容不是合法的json字符串!\"}" );
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* @Author: Kane
|
||||
* @Date: 2022-08-28 00:13:12
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-08-28 00:27:27
|
||||
* @FilePath: \cpicxim-servlets\src\main\java\com\cpic\xim\servlets\ldap\LdapStuffInfo.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
|
||||
package com.cpic.xim.servlets.ldap;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/*****************************************************
|
||||
* 太平洋保险ldap用户信息对象。
|
||||
* @author Kane
|
||||
* @version 1.0
|
||||
*****************************************************/
|
||||
public class LdapStuffInfo
|
||||
{
|
||||
public LdapStuffInfo( String ldapStuffName, String ldapStuffPassword)
|
||||
{
|
||||
this.ldapStuffName = ldapStuffName;
|
||||
this.ldapStuffPassword = ldapStuffPassword;
|
||||
}
|
||||
|
||||
public String getLdapStuffName()
|
||||
{
|
||||
return ldapStuffName;
|
||||
}
|
||||
|
||||
public void setLdapStuffName( String ldapStuffName )
|
||||
{
|
||||
this.ldapStuffName = ldapStuffName;
|
||||
}
|
||||
|
||||
public String getLdapStuffPassword()
|
||||
{
|
||||
return ldapStuffPassword;
|
||||
}
|
||||
|
||||
public void setLdapStuffPassword( String ldapStuffPassword )
|
||||
{
|
||||
this.ldapStuffPassword = ldapStuffPassword;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((ldapStuffName == null) ? 0 : ldapStuffName.hashCode());
|
||||
result = prime * result + ((ldapStuffPassword == null) ? 0 : ldapStuffPassword.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object obj )
|
||||
{
|
||||
if ( this == obj)
|
||||
return true;
|
||||
if ( obj == null)
|
||||
return false;
|
||||
if ( getClass() != obj.getClass())
|
||||
return false;
|
||||
LdapStuffInfo other = (LdapStuffInfo) obj;
|
||||
if ( ldapStuffName == null)
|
||||
{
|
||||
if ( other.ldapStuffName != null)
|
||||
return false;
|
||||
} else if ( !ldapStuffName.equals( other.ldapStuffName ))
|
||||
return false;
|
||||
if ( ldapStuffPassword == null)
|
||||
{
|
||||
if ( other.ldapStuffPassword != null)
|
||||
return false;
|
||||
} else if ( !ldapStuffPassword.equals( other.ldapStuffPassword ))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@JsonProperty( "stuff_name")
|
||||
private String ldapStuffName;
|
||||
|
||||
@JsonProperty( "stuff_password")
|
||||
private String ldapStuffPassword;
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user