Merge branch 'master' of https://github.com/metersphere/server
This commit is contained in:
commit
a9f81bc59f
|
@ -2,56 +2,38 @@ package io.metersphere.ldap;
|
||||||
|
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
import io.metersphere.controller.request.LoginRequest;
|
import io.metersphere.controller.request.LoginRequest;
|
||||||
import org.apache.shiro.realm.ldap.LdapUtils;
|
import io.metersphere.i18n.Translator;
|
||||||
import org.springframework.ldap.core.LdapTemplate;
|
import org.springframework.ldap.CommunicationException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.naming.directory.DirContext;
|
|
||||||
import javax.naming.ldap.LdapContext;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LdapService {
|
public class LdapService {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private LdapTemplate ldapTemplate;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private PersonRepoImpl personRepo;
|
private PersonRepoImpl personRepo;
|
||||||
|
|
||||||
public boolean authenticate(LoginRequest request) {
|
public boolean authenticate(LoginRequest request) {
|
||||||
// String userDn, String credentials
|
|
||||||
DirContext ctx = null;
|
|
||||||
String dn = null;
|
String dn = null;
|
||||||
String username = request.getUsername();
|
String username = request.getUsername();
|
||||||
String credentials = request.getPassword();
|
String credentials = request.getPassword();
|
||||||
|
|
||||||
List user = personRepo.findByName(username);
|
|
||||||
|
|
||||||
if (user.size() > 0) {
|
|
||||||
dn = personRepo.getDnForUser(username);
|
|
||||||
} else {
|
|
||||||
MSException.throwException("no such user");
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
ctx = ldapTemplate.getContextSource().getContext(dn, credentials);
|
// select user by sAMAccountName
|
||||||
// ldapTemplate.authenticate(dn, credentials);
|
List user = personRepo.findByName(username);
|
||||||
// Take care here - if a base was specified on the ContextSource
|
|
||||||
// that needs to be removed from the user DN for the lookup to succeed.
|
if (user.size() == 1) {
|
||||||
// ctx.lookup(userDn);
|
dn = personRepo.getDnForUser(username);
|
||||||
return true;
|
} else if (user.size() == 0){
|
||||||
} catch (Exception e) {
|
MSException.throwException(Translator.get("user_not_exist") + username);
|
||||||
// Context creation failed - authentication did not succeed
|
} else {
|
||||||
System.out.println("Login failed: " + e);
|
MSException.throwException("Found multiple users");
|
||||||
MSException.throwException("login failed...");
|
}
|
||||||
return false;
|
} catch (CommunicationException e) {
|
||||||
} finally {
|
MSException.throwException("LDAP Server connection failed!");
|
||||||
// It is imperative that the created DirContext instance is always closed
|
|
||||||
LdapUtils.closeContext((LdapContext) ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return personRepo.authenticate(dn, credentials);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package io.metersphere.ldap;
|
package io.metersphere.ldap;
|
||||||
|
|
||||||
|
|
||||||
|
import io.metersphere.commons.exception.MSException;
|
||||||
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.ldap.domain.Person;
|
import io.metersphere.ldap.domain.Person;
|
||||||
|
import org.apache.shiro.realm.ldap.LdapUtils;
|
||||||
|
import org.springframework.ldap.AuthenticationException;
|
||||||
import org.springframework.ldap.NamingException;
|
import org.springframework.ldap.NamingException;
|
||||||
import org.springframework.ldap.core.*;
|
import org.springframework.ldap.core.*;
|
||||||
import org.springframework.ldap.core.support.AbstractContextMapper;
|
import org.springframework.ldap.core.support.AbstractContextMapper;
|
||||||
|
@ -9,10 +13,9 @@ import org.springframework.ldap.query.LdapQuery;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.naming.directory.Attributes;
|
import javax.naming.directory.Attributes;
|
||||||
|
import javax.naming.directory.DirContext;
|
||||||
|
import javax.naming.ldap.LdapContext;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
import static org.springframework.ldap.query.LdapQueryBuilder.query;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -36,6 +39,38 @@ public class PersonRepoImpl implements PersonRepo {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean authenticate(String dn, String credentials) {
|
||||||
|
DirContext ctx = null;
|
||||||
|
try {
|
||||||
|
ctx = ldapTemplate.getContextSource().getContext(dn, credentials);
|
||||||
|
// ldapTemplate.authenticate(dn, credentials);
|
||||||
|
// Take care here - if a base was specified on the ContextSource
|
||||||
|
// that needs to be removed from the user DN for the lookup to succeed.
|
||||||
|
// ctx.lookup(userDn);
|
||||||
|
return true;
|
||||||
|
} catch (AuthenticationException e) {
|
||||||
|
LogUtil.error("ldap authenticate failed..." + e);
|
||||||
|
System.out.println("Login failed: " + e);
|
||||||
|
MSException.throwException("用户认证失败!");
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Context creation failed - authentication did not succeed
|
||||||
|
LogUtil.error("ldap authenticate failed..." + e);
|
||||||
|
System.out.println("Login failed: " + e);
|
||||||
|
MSException.throwException("login failed...");
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
// It is imperative that the created DirContext instance is always closed
|
||||||
|
LdapUtils.closeContext((LdapContext) ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Person> getAllPersons() {
|
||||||
|
ldapTemplate.setIgnorePartialResultException(true);
|
||||||
|
return ldapTemplate.search(query()
|
||||||
|
.where("objectclass").is("person"), getContextMapper());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List findByName(String name) {
|
public List findByName(String name) {
|
||||||
ldapTemplate.setIgnorePartialResultException(true);
|
ldapTemplate.setIgnorePartialResultException(true);
|
||||||
|
@ -74,7 +109,9 @@ public class PersonRepoImpl implements PersonRepo {
|
||||||
public Person doMapFromContext(DirContextOperations context) {
|
public Person doMapFromContext(DirContextOperations context) {
|
||||||
Person person = new Person();
|
Person person = new Person();
|
||||||
person.setCommonName(context.getStringAttribute("cn"));
|
person.setCommonName(context.getStringAttribute("cn"));
|
||||||
person.setSuerName(context.getStringAttribute("sn"));
|
person.setSurName(context.getStringAttribute("sn"));
|
||||||
|
person.setUsername(context.getStringAttribute("sAMAccountName"));
|
||||||
|
person.setEmail(context.getStringAttribute("mail"));
|
||||||
return person;
|
return person;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,13 @@ package io.metersphere.ldap.domain;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.ldap.odm.annotations.Attribute;
|
import org.springframework.ldap.odm.annotations.Attribute;
|
||||||
import org.springframework.ldap.odm.annotations.DnAttribute;
|
import org.springframework.ldap.odm.annotations.DnAttribute;
|
||||||
|
import org.springframework.ldap.odm.annotations.Entry;
|
||||||
import org.springframework.ldap.odm.annotations.Id;
|
import org.springframework.ldap.odm.annotations.Id;
|
||||||
|
|
||||||
import javax.naming.Name;
|
import javax.naming.Name;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@Entry(objectClasses = {"person", "top"})
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -17,6 +19,10 @@ public class Person {
|
||||||
@Attribute(name = "cn")
|
@Attribute(name = "cn")
|
||||||
private String commonName;
|
private String commonName;
|
||||||
@Attribute(name = "sn")
|
@Attribute(name = "sn")
|
||||||
private String suerName;
|
private String surName;
|
||||||
private String userPassword;
|
@Attribute(name = "sAMAccountName")
|
||||||
|
private String username;
|
||||||
|
@Attribute(name = "mail")
|
||||||
|
private String email;
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue