我的目标是让我的应用程序登录名/密码与LDAP匹配。 一切都有效,但似乎变量没有初始化。
这是一个使用Spring,Struts2和Maven的Java应用程序。映射看起来很好,因为它确实如此:
控制台:
2015-02-12 16:13:46,955 INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.loadProperties:178 - Loading properties file from URL [file:d:/Users/****/properties/match.properties]
2015-02-12 16:13:46,971 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons:414 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10309a9: defining beans [project-properties,applicationGlobal]; root of factory hierarchy
2015-02-12 16:13:46,982 INFO org.springframework.web.context.ContextLoader.initWebApplicationContext:209 - Root WebApplicationContext: initialization completed in 341 ms
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
applicationContext.xml:
<bean id="project-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="searchSystemEnvironment" value="true" />
<property name="locations">
<list>
<value>file:/home/tomcat/properties/match-aaaa.properties</value>
<value>file:${user.home}/properties/match.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
<!-- ================================================ -->
<bean id="applicationGlobal" class="com.tutorialspoint.struts2.Constants" scope="singleton" >
<property name="aDURI" value="${ADURI}"/>
<property name="aDBaseDN" value="${ADBaseDN}"/>
<property name="aDRootUser" value="${ADRootUser}"/>
<property name="aDRootPassword" value="${ADRootPassword}"/>
<property name="aDGroupeAdmin" value="${ADGroupeAdmin}"/>
<property name="aDGroupeSuperviseur" value="${ADGroupeSuperviseur}"/>
<property name="aDGroupeUser" value="${ADGroupeUser}"/>
<property name="userProfile" value="${UserProfile}"/>
<property name="token" value="${TOKEN}"/>
</bean>
我的 match.properties:(由于显而易见的原因,值已被更改)
# ACTIVE DIRECTORY -- Authentification & Roles
ADURI=***
ADBaseDN=***
ADRootUser=***
ADRootPassword=***
ADGroupeAdmin=***
ADGroupeSuperviseur=***
ADGroupeUser=***
UserProfile=***
TOKEN=***
好吧它确实会转到 match.properties 的位置,但它不会将值放在我的变量中......
我期待着一些帮助!
以下是完整的控制台:
2015-02-12 16:46:11,736 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.ActionSupport@1edd647, com.opensymphony.xwork2.DefaultTextProvider@d712ed], property=org]
2015-02-12 16:46:11,743 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-12 16:46:11,747 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - after Locale=fr_FR
2015-02-12 16:46:11,747 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - intercept }
2015-02-12 16:46:12,010 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-12 16:46:18,456 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@d712ed], property=struts]
2015-02-12 16:46:18,457 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-12 16:46:18,457 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:57 - Creating an DefaultActionProxy for namespace / and action name Login
2015-02-12 16:46:18,459 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - intercept '//Login' {
2015-02-12 16:46:18,459 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - applied invocation context locale=fr_FR
2015-02-12 16:46:18,459 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - before Locale=fr_FR
2015-02-12 16:46:18,460 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.tutorialspoint.struts2.action.LoginAction@1b1e91b, com.opensymphony.xwork2.DefaultTextProvider@d712ed], property=struts]
2015-02-12 16:46:18,474 DEBUG org.apache.struts2.interceptor.FileUploadInterceptor.debug:57 - Bypassing //Login
2015-02-12 16:46:18,475 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:57 - Setting static parameters {}
2015-02-12 16:46:18,475 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params NONE
2015-02-12 16:46:18,475 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:57 - Setting params login => [ ***** ] pass => [ ***** ]
2015-02-12 16:46:18,476 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Property: login
2015-02-12 16:46:18,477 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Class: com.tutorialspoint.struts2.action.LoginAction
2015-02-12 16:46:18,483 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - converter is null for property login. Mapping size: 0
2015-02-12 16:46:18,484 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - field-level type converter for property [login] = none found
2015-02-12 16:46:18,484 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - global-level type converter for property [login] = none found
2015-02-12 16:46:18,484 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - falling back to default type converter [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@1b9295f]
2015-02-12 16:46:18,485 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Property: pass
2015-02-12 16:46:18,486 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - Class: com.tutorialspoint.struts2.action.LoginAction
2015-02-12 16:46:18,486 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - field-level type converter for property [pass] = none found
2015-02-12 16:46:18,486 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - global-level type converter for property [pass] = none found
2015-02-12 16:46:18,487 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter.debug:57 - falling back to default type converter [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@1b9295f]
2015-02-12 16:46:18,487 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:57 - Validating //Login with method execute.
2015-02-12 16:46:18,497 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:57 - Invoking validate() on action com.tutorialspoint.struts2.action.LoginAction@1b1e91b
2015-02-12 16:46:18,498 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:57 - cannot find method [validateExecute] in action [com.tutorialspoint.struts2.action.LoginAction@1b1e91b]
2015-02-12 16:46:18,499 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:57 - cannot find method [validateDoExecute] in action [com.tutorialspoint.struts2.action.LoginAction@1b1e91b]
2015-02-12 16:46:18,499 DEBUG com.opensymphony.xwork2.DefaultActionInvocation.debug:57 - Executing action method = null
2015-02-12 16:46:18,515 INFO com.tutorialspoint.struts2.action.LoginAction.execute:108 - ERREUR: java.lang.NullPointerException
2015-02-12 16:46:18,516 INFO com.tutorialspoint.struts2.action.LoginAction.execute:124 - ERREUR: L'utilisateur **** n'est pas user
2015-02-12 16:46:18,516 WARN com.tutorialspoint.struts2.action.LoginAction.execute:141 - AD Authentification FAILED!!!
2015-02-12 16:46:18,517 DEBUG org.apache.struts2.dispatcher.ServletRedirectResult.debug:57 - Redirecting to finalLocation /HelloWorldStruts2/context/errorLog.jsp
2015-02-12 16:46:18,518 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - after Locale=fr_FR
2015-02-12 16:46:18,518 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:57 - intercept }
2015-02-12 16:46:18,523 DEBUG com.opensymphony.xwork2.config.ConfigurationManager.debug:57 - Checking ConfigurationProviders for reload.
2015-02-12 16:46:18,524 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:57 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@d712ed], property=org]
根据要求 LoginAction.java:
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();
//bouchon
/*
if("admin".equals(getLogin())){
userProfile.setLogin(getLogin());
userProfile.setAdmin(true);
ActionContext.getContext().getSession().put(getApplicationGlobal().getUserProfile(), userProfile);
return SUCCESS;
}
if("superviseur".equals(getLogin())){
userProfile.setSuperviseur(true);
userProfile.setLogin(getLogin());
ActionContext.getContext().getSession().put(getApplicationGlobal().getUserProfile(), userProfile);
return SUCCESS;
}
if("user".equals(getLogin())){
userProfile.setUser(true);
userProfile.setLogin(getLogin());
ActionContext.getContext().getSession().put(getApplicationGlobal().getUserProfile(), userProfile);
return SUCCESS;
}
*/
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);
----->line108 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";
}
}
/**
* Retourne la premiere valeur d'un chaine de type ldap (csv)
*
* Exemple : CN=GG_Appli_Comptage_Production,OU=Atelier_Comptage,OU=Appli,OU=Groups,OU=Nanterre,OU=Altares,OU=Corp,DC=altares,DC=loc retourne
* GG_Appli_Comptage_Production
*
*/
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 Map authenticate(String user, String pass) {
String returnedAtts[] = { "sn", "givenName", "mail" };
String searchFilter = "(&(objectClass=user)(sAMAccountName=" + user + "))";
logger.info("authenticate(" + user + ", xxx)");
// Create the search controls
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(returnedAtts);
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapHost);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user + "@" + domain);
env.put(Context.SECURITY_CREDENTIALS, pass);
LdapContext ctxGC = null;
try {
ctxGC = new InitialLdapContext(env, null);
// Search objects in GC using filters
NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
Map amap = null;
if (attrs != null) {
amap = new HashMap();
NamingEnumeration ne = attrs.getAll();
while (ne.hasMore()) {
Attribute attr = (Attribute) ne.next();
amap.put(attr.getID(), attr.get());
}
ne.close();
}
return amap;
}
} catch (NamingException ex) {
logger.warn("ERREUR LDAP:" + ex.getMessage());
}
return null;
}*/
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;
}
}