我们正在努力的是一个简单的Xpage解决方案。此工具的目标是从我们的LDAP获取个人信息(全名和公司),其他功能是更改密码(密码重置)。 这是我的java类
package com.mycompany.utils;
import java.io.IOException;
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
import org.apache.directory.api.ldap.model.cursor.CursorException;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.entry.Attribute;
import org.apache.directory.api.ldap.model.entry.DefaultModification;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.entry.Modification;
import org.apache.directory.api.ldap.model.entry.ModificationOperation;
import org.apache.directory.api.ldap.model.exception.LdapAuthenticationException;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.api.ldap.model.password.PasswordUtil;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
public class LDAP {
/** The connection pool to use ldap connections from */
LdapConnection connection;
/** The LDAP schema attribute name for first name. Default is givenName. */
private boolean found = false;
/** The LDAP schema attribute name for first name. Default is givenName. */
private String firstNameAttr = "FirstName";
/** The LDAP schema attribute name for last name. Default is sn. */
private String lastNameAttr = "LastName";
/** THe LDAP schema attribute name for DistinguishedName. */
private String terDistinguishedNameAttr = "DistinguishedName";
/** THe LDAP schema attribute name for userPassword. */
private byte[] userPasswordAttr = null;
/** THe LDAP schema attribute name for terCustCompanyName. */
private String terCustCompanyNameAttr = null;
/** Default constructor */
public LDAP() {
try{
System.out.println("Line 42");
//this.connection = new LdapNetworkConnection("nordique.corp.mycompany.com",389);
this.connection = new LdapNetworkConnection("qa.ldap.mycompany.com",389);
this.connection.bind("cn=root", "xxxxxxxx");
System.out.println("Line 46");
System.out.println(this.connection.isConnected());
} catch (LdapAuthenticationException e) {
e.printStackTrace();
System.out.println("LDAP wrong Credentials");
System.exit(0);
} catch (LdapException e) {
System.out.println("LDAP Exception");
//System.exit(0);
} catch(Exception e){
e.printStackTrace();
//System.exit(0);
}
}
/**
* Method delegated to from searchForUID which connects to LDAP and populates internal variables for
* givenName, sn, and mail.
*
* @param userInfo the UID to search.
*/
public void searchForUID(String userInfo) {
this.setFound(false);
this.setFirstNameAttr("");
this.setLastNameAttr("");
this.setTerDistinguishedNameAttr("");
this.setUserPassword("##".getBytes());
this.setTerCustCompanyName("");
try {
EntryCursor cursor = this.connection.search( "ou=employees,dc=mycompany,dc=COM", "(&(objectclass=person)(uid="+userInfo+"*))", SearchScope.SUBTREE );
if (cursor.next()) {
Entry record = cursor.get();
//System.out.println("Record:"+record.toString());
this.setFound(true);
Attribute givenName = record.get("givenName");
Attribute sn = record.get("sn");
Attribute terDistinguishedName = record.get("terDistinguishedName");
Attribute userPassword = record.get("userPassword");
Attribute terCustCompanyName = record.get("cSiteName");
if ( givenName != null)
{
this.setFirstNameAttr(givenName.getString());
}
if (sn != null)
{
this.setLastNameAttr(sn.getString());
}
if (terDistinguishedName != null)
{
this.setTerDistinguishedNameAttr(terDistinguishedName.getString());
}
if (userPassword != null)
{
this.setUserPassword(userPassword.get().getBytes());
}
if (terCustCompanyName != null)
{
this.setTerCustCompanyName(terCustCompanyName.getString());
}
}
if (cursor.next()) {
System.out.println("found two matches for the user; " + userInfo);
}
cursor.close();
} catch (CursorException e) {
System.out.println("Cursor Exception: " + e);
} catch (LdapException e) {
System.out.println("LDAP Exception: " + e);
} catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
protected void closeConnection(){
try {
this.connection.close();
} catch (Exception e) {
System.out.println("LDAP Exception: " + e);
}
}
public boolean modifyAttribute(String Attribute, String newValue){
Modification replaceGn = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, Attribute, newValue );
boolean status = false;
try {
this.connection.modify( this.getTerDistinguishedNameAttr(), replaceGn );
status = true;
} catch (LdapException e) {
System.out.println("Modification fail");
}
return status;
}
public boolean modifyPasswordAttribute(byte[] newPassword){
Modification replaceGn = new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, "userPassword", PasswordUtil.createStoragePassword(newPassword, LdapSecurityConstants.HASH_METHOD_SHA256));
boolean status = false;
try {
this.connection.modify( this.getTerDistinguishedNameAttr(), replaceGn );
status = true;
} catch (LdapException e) {
System.out.println("Modification fail");
}
return status;
}
/**
* Returns the found status. This is the status of the ObjectClass.
*
* @return the status.
*/
public boolean getFound() {
return found;
}
/**
* Sets the found status. This is the status of the ObjectClass.
*
* @param status the Found to set
*/
public void setFound(boolean newFound) {
this.found = newFound;
}
/**
* Returns the terCustCompanyNameAttr field. This is the LDAP schema attribute of the ObjectClass to get terCustCompanyName.
*
* @return the terCustCompanyNameAttr field.
*/
public String getTerCustCompanyName() {
return terCustCompanyNameAttr;
}
/**
* Sets the terCustCompanyNameAttr field. This is the LDAP schema attribute of the ObjectClass to get terCustCompanyName.
*
* @param terCustCompanyName the terCustCompanyNameAttr to set
*/
public void setTerCustCompanyName(String terCustCompanyName) {
this.terCustCompanyNameAttr = terCustCompanyName;
}
/**
* Returns the userPasswordAttr field. This is the LDAP schema attribute of the ObjectClass to get password.
*
* @return the userPasswordAttr field.
*/
public byte[] getUserPassword() {
return userPasswordAttr;
}
/**
* Sets the userPasswordAttr field. This is the LDAP schema attribute of the ObjectClass to get user password.
*
* @param userPassword the userPasswordAttr to set
*/
public void setUserPassword(byte[] userPassword) {
this.userPasswordAttr = userPassword;
}
/**
* Returns the firstNameAttr field. This is the LDAP schema attribute of the ObjectClass to get first name.
*
* @return the firstNameAttr field.
*/
public String getFirstNameAttr() {
return firstNameAttr;
}
/**
* Sets the firstNameAttr field. This is the LDAP schema attribute of the ObjectClass to get first name.
*
* @param firstNameAttr the firstNameAttr to set.
*/
public void setFirstNameAttr(String firstNameAttr) {
this.firstNameAttr = firstNameAttr;
}
/**
* Returns the lastNameAttr field. This is the LDAP schema attribute of the ObjectClass to get last name.
*
* @return the lastNameAttr field.
*/
public String getLastNameAttr() {
return lastNameAttr;
}
/**
* Sets the lastNameAttr field. This is the LDAP schema attribute of the ObjectClass to get last name.
*
* @param lastNameAttr the lastNameAttr to set
*/
public void setLastNameAttr(String lastNameAttr) {
this.lastNameAttr = lastNameAttr;
}
/**
* Returns the terDistinguishedNameAttr. This is the LDAP schema attribute of the ObjectClass to get email.
*
* @return the terDistinguishedNameAttr.
*/
public String getTerDistinguishedNameAttr() {
return terDistinguishedNameAttr;
}
/**
* Sets the terDistinguishedNameAttr. This is the LDAP schema attribute of the ObjectClass to get email.
*
* @param terDistinguishedNameAttr the terDistinguishedNameAttr. This is the LDAP schema attribute of the ObjectClass to get email.
*/
public void setTerDistinguishedNameAttr(String terDistinguishedNameAttr) {
this.terDistinguishedNameAttr = terDistinguishedNameAttr;
}
}
在faces-config代码中我们定义
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>emailBean</managed-bean-name>
<managed-bean-class>com.ibm.xsp.utils.EmailBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>debugMode</property-name>
<value>true</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>LDAP</managed-bean-name>
<managed-bean-class>com.mycompany.utils.LDAP</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<!--AUTOGEN-START-BUILDER: Automatically generated by IBM Domino Designer. Do not modify.-->
<!--AUTOGEN-END-BUILDER: End of automatically generated section-->
</faces-config>
在Xpage中我定义了一个字段和一个按钮(此按钮将使用jave类从LDAP结构中获取信息)
该按钮包含以下代码
importPackage(com.mycompany.utils);
var UID = document1.getItemValueString("UID");
print(" ");
print(" ");
print("From xpages");
try{
var ldapConnection = new LDAP();
}catch(e){
print("Error generating dynamic LDAP: " + e.toString());
}
print("+=========================================1");
print (UID);
/*ldapConnection.searchForUID(UID);
if(ldapConnection.getFound()){
print("User :"+ldapConnection.getFirstNameAttr()+" "+ldapConnection.getLastNameAttr());
print("Company :"+ldapConnection.getTerCustCompanyName());
print("Passsword :"+new String(ldapConnection.getUserPassword(), StandardCharsets.UTF_8));
return ldapConnection.getTerCustCompanyName();
}else{
print("user not found!");
}*/
print("+=========================================2");
print(" ");
print(" ");
print("END xpages");
最后,当我们执行按钮时,我们会在控制台中收到以下消息。
[1434:000A-0AF0] 05/18/2017 12:54:02 PM HTTP JVM:
[1434:000A-0AF0] 05/18/2017 12:54:02 PM HTTP JVM:
[1434:000A-0AF0] 05/18/2017 12:54:02 PM HTTP JVM: From xpages
[1434:000A-0AF0] 05/18/2017 12:54:05 PM HTTP JVM: Line 42
[1434:0047-095C] 05/18/2017 12:54:05 PM HTTP JVM: Exception in thread "pool-5-thread-1"
[1434:0048-095C] 05/18/2017 12:54:05 PM HTTP JVM: java.lang.IllegalStateException: NotesContext not initialized for the thread
[1434:004A-095C] 05/18/2017 12:54:05 PM HTTP JVM: at com.ibm.domino.xsp.module.nsf.NotesContext.getCurrent(NotesContext.java:123)
[1434:004C-095C] 05/18/2017 12:54:05 PM HTTP JVM: at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:416)
[1434:004E-095C] 05/18/2017 12:54:05 PM HTTP JVM: at java.lang.ClassLoader.loadClass(ClassLoader.java:809)
[1434:0050-095C] 05/18/2017 12:54:05 PM HTTP JVM: at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:56)
[1434:0052-095C] 05/18/2017 12:54:05 PM HTTP JVM: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
[1434:0054-095C] 05/18/2017 12:54:05 PM HTTP JVM: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[1434:0056-095C] 05/18/2017 12:54:05 PM HTTP JVM: at java.lang.Thread.run(Thread.java:785)
[1434:000A-0AF0] 05/18/2017 12:54:35 PM HTTP JVM: LDAP Exceptionorg.apache.directory.ldap.client.api.exception.InvalidConnectionException: Cannot connect on the server, the connection is null
[1434:000A-0AF0] 05/18/2017 12:54:35 PM HTTP JVM: +=========================================1
[1434:000A-0AF0] 05/18/2017 12:54:35 PM HTTP JVM: com.ibm.xsp.webapp.FacesServlet$ExtendedServletException: com.ibm.xsp.exception.EvaluationExceptionEx: Error while executing JavaScript computed expression
[1434:000A-0AF0] 05/18/2017 12:54:35 PM HTTP JVM: CLFAD0134E: Exception processing XPage request. For more detailed information, please consult error-log-0.xml located in E:/notes/data/domino/workspace/logs
[1434:000C-143C] 05/18/2017 12:54:37 PM HTTP JVM: CLFAD0211E: Exception thrown. For more detailed information, please consult error-log-0.xml located in E:/notes/data/domino/workspace/logs
感谢您的帮助。 最诚挚的问候
答案 0 :(得分:0)
Apache Mina为LDAP连接创建了一个新线程,但使用了XPage引擎的类加载器。创建新线程时,此类加载器会检查是否存在NotesContext,而未对新线程进行初始化。这就是它失败的原因。