当我在表单中验证登录名和密码时,我收到此错误 java.lang.NullPointerException 。但我不知道出了什么问题,因为我已经搜索了所有相同的错误消息,我的代码中的某些内容似乎发送了 Null 值。
LoginAction.java:
package com.tutorialspoint.struts2.action;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionContext;
import com.tutorialspoint.struts2.UserProfile;
public class LoginAction extends BaseAction {
/**
* Serial Version UID
*/
private static final long serialVersionUID = 1L;
private String login;
private String pass;
private static Log logger = LogFactory.getLog(LoginAction.class);
public String execute() {
boolean auth = false;
UserProfile userProfile = new UserProfile();
String token = getLogin() + "#" + getPass();
String searchFilter = "(&(objectClass=user)(sAMAccountName="
+ getLogin() + "))";
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String domain = getApplicationGlobal().getaDURI();
domain = domain.substring(domain.indexOf(".") + 1);
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, getApplicationGlobal().getaDURI());
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, getLogin() + "@" + domain);
env.put(Context.SECURITY_CREDENTIALS, pass);
try {
LdapContext ctx = new InitialLdapContext(env, null);
NamingEnumeration sr = ctx.search(getApplicationGlobal()
.getaDBaseDN(), searchFilter, searchCtls);
while (sr.hasMoreElements()) {
SearchResult r = (SearchResult) sr.next();
logger.debug("LDAP Info:" + r.getNameInNamespace());
Attribute members = r.getAttributes().get("memberOf");
logger.debug("LDAP Number of Group for this user:"
+ members.size());
NamingEnumeration<String> emembers = (NamingEnumeration<String>) members
.getAll();
while (emembers.hasMore()) {
String ldapGroup = emembers.next();
String myLdapGroup = getFirstLdapValue(ldapGroup, null);
logger.debug("LDAP Group:" + myLdapGroup);
if (myLdapGroup != null) {
myLdapGroup = myLdapGroup.toLowerCase();
if (myLdapGroup.equals(getApplicationGlobal()
.getaDGroupeAdmin().toLowerCase())) {
userProfile.setAdmin(true);
} else if (myLdapGroup.equals(getApplicationGlobal()
.getaDGroupeSuperviseur().toLowerCase())) {
userProfile.setSuperviseur(true);
} else if (myLdapGroup.equals(getApplicationGlobal()
.getaDGroupeUser().toLowerCase())) {
userProfile.setUser(true);
}
}
}
}
} catch (Exception e) {
// throw new
// FunctionalException(FunctionalException.AUTHENTICATION_ERROR, e,
// ERROR);
logger.info("ERREUR: " + e);
}
// on ne retient que les roles "utiles"
if (userProfile.getIsAdmin()) {
logger.info("L'utilisateur " + login + " est admin");
userProfile.setSuperviseur(false);
userProfile.setUser(false);
auth = true;
} else {
if (userProfile.getIsSuperviseur()) {
logger.info("L'utilisateur " + login + " est superviseur");
userProfile.setUser(false);
auth = true;
} else {
if (!userProfile.isUser()) {
logger.info("ERREUR: L'utilisateur " + login
+ " n'est pas user");
} else {
auth = true;
logger.info("L'utilisateur " + login + " est user");
}
}
}
if (auth) {
userProfile.setLogin(getLogin());
ActionContext.getContext().getSession()
.put(getApplicationGlobal().getUserProfile(), userProfile);
ActionContext.getContext().getSession()
.put(getApplicationGlobal().getToken(), token);
logger.warn("AD Authentification SUCCESS!");
return SUCCESS;
} else {
logger.warn("AD Authentification FAILED!!!");
return "notLogged";
}
}
private String getFirstLdapValue(final String ldapCsvValues,
final Integer position) {
String myLdapCsvValues = ldapCsvValues;
Integer myPosition = position;
if (position == null) {
myPosition = 0;
}
if (myLdapCsvValues == null) {
return null;
}
myLdapCsvValues = myLdapCsvValues.replace("\\,", "");
String values[] = myLdapCsvValues.split(",");
String value0[] = values[myPosition].split("=");
return value0[1];
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="main" namespace="/" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="Accueil">
<result >/baseLayout.jsp</result>
</action>
<action name="Accueil" class="com.opensymphony.xwork2.ActionSupport">
<result name="success" type="tiles">accueil</result>
</action>
<action name="Login" class="com.tutorialspoint.struts2.action.LoginAction">
<result name="success" type="redirectAction">HelloWorld.jsp</result>
<result name="notLogged" type="tiles">notLogged.tiles</result>
</action>
</package>
</struts>
控制台:
2015-02-11 10:44:22,872 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-11 10:44:22,873 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@9267d9], property=struts]
2015-02-11 10:44:22,873 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-11 10:44:22,874 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:57 - Creating an DefaultActionProxy for namespace / and action name Login
2015-02-11 10:44:22,874 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - intercept '//Login' {
2015-02-11 10:44:22,874 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - applied invocation context locale=fr_FR
2015-02-11 10:44:22,875 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - before Locale=fr_FR
2015-02-11 10:44:22,875 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.tutorialspoint.struts2.action.LoginAction@590bf4, com.opensymphony.xwork2.DefaultTextProvider@9267d9], property=struts]
2015-02-11 10:44:22,876 DEBUG org.apache.struts2.interceptor.FileUploadInterceptor.debug:57 - Bypassing //Login
2015-02-11 10:44:22,876 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:57 - Setting static parameters {}
2015-02-11 10:44:22,876 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params NONE
2015-02-11 10:44:22,877 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params login => [ test1 ] pass => [ test2 ]
2015-02-11 10:44:22,877 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Property: login
2015-02-11 10:44:22,877 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Class: com.tutorialspoint.struts2.action.LoginAction
2015-02-11 10:44:22,878 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - field-level type converter for property [login] = none found
2015-02-11 10:44:22,878 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - global-level type converter for property [login] = none found
2015-02-11 10:44:22,878 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - falling back to default type converter [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@1adfb84]
2015-02-11 10:44:22,879 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Property: pass
2015-02-11 10:44:22,879 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Class: com.tutorialspoint.struts2.action.LoginAction
2015-02-11 10:44:22,880 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - field-level type converter for property [pass] = none found
2015-02-11 10:44:22,880 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - global-level type converter for property [pass] = none found
2015-02-11 10:44:22,880 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - falling back to default type converter [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@1adfb84]
2015-02-11 10:44:22,881 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:57 - Validating //Login with method execute.
2015-02-11 10:44:22,894 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:57 - Invoking validate() on action com.tutorialspoint.struts2.action.LoginAction@590bf4
2015-02-11 10:44:22,895 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:57 - cannot find method [validateExecute] in action [com.tutorialspoint.struts2.action.LoginAction@590bf4]
2015-02-11 10:44:22,895 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:57 - cannot find method [validateDoExecute] in action [com.tutorialspoint.struts2.action.LoginAction@590bf4]
2015-02-11 10:44:22,896 DEBUG com.opensymphony.xwork2.DefaultActionInvocation.debug:57 - Executing action method = null
2015-02-11 10:44:22,896 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
错误消息:
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
File: com/tutorialspoint/struts2/action/LoginAction.java
Line number: 59
--------
Stacktraces
java.lang.NullPointerException (...)
如果您需要其他文件或详细信息,请与我们联系!
感谢任何帮助!
答案 0 :(得分:0)
我认为NamingEnumeration sr
可能存在问题。如果为null或空,则在此变量上调用while循环将返回空指针异常。至少我已经发生了几次这种情况。我建议调试你的代码,看看哪个变量得到异常。希望这会有所帮助:)