Commit ccc34763 authored by Jason Song's avatar Jason Song Committed by GitHub

Merge pull request #1380 from nobodyiam/pandalin-ldap

Add ldap support
parents 4ea75826 1da45449
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
<github.path>${project.artifactId}</github.path> <github.path>${project.artifactId}</github.path>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-common</artifactId> <artifactId>apollo-common</artifactId>
......
package com.ctrip.framework.apollo.portal.entity.bo;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
/**
* @author xm.lin xm.lin@anxincloud.com
* @Description
* @date 18-8-9 下午4:43
*/
@Entry(base = "cn=Manager",objectClasses = {"inetOrgPerson"})
public class LdapUserInfo {
@Id
private Name id;
@Attribute(name = "cn")
private String username;
@Attribute(name = "sn")
private String realName;
@Attribute(name = "userPassword")
private String userPassword;
@Attribute(name = "mail")
private String mail;
public Name getId() {
return id;
}
public void setId(Name id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
package com.ctrip.framework.apollo.portal.spi.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import java.util.HashMap;
import java.util.Map;
/**
* @author xm.lin xm.lin@anxincloud.com
* @Description
* @date 18-8-9 下午4:36
*/
@ConfigurationProperties(prefix = "spring.ldap")
public class LdapProperties {
private static final int DEFAULT_PORT = 389;
/**
* LDAP URLs of the server.
*/
private String[] urls;
/**
* Base suffix from which all operations should originate.
*/
private String base;
/**
* Login username of the server.
*/
private String username;
/**
* Login password of the server.
*/
private String password;
/**
* Whether read-only operations should use an anonymous environment.
*/
private boolean anonymousReadOnly;
/**
* LDAP specification settings.
*/
private final Map<String, String> baseEnvironment = new HashMap<>();
private String userDnPatterns;
public String[] getUrls() {
return this.urls;
}
public void setUrls(String[] urls) {
this.urls = urls;
}
public String getBase() {
return this.base;
}
public void setBase(String base) {
this.base = base;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean getAnonymousReadOnly() {
return this.anonymousReadOnly;
}
public void setAnonymousReadOnly(boolean anonymousReadOnly) {
this.anonymousReadOnly = anonymousReadOnly;
}
public String getUserDnPatterns() {
return userDnPatterns;
}
public void setUserDnPatterns(String userDnPatterns) {
this.userDnPatterns = userDnPatterns;
}
public Map<String, String> getBaseEnvironment() {
return this.baseEnvironment;
}
public String[] determineUrls(Environment environment) {
if (ObjectUtils.isEmpty(this.urls)) {
return new String[]{"ldap://localhost:" + determinePort(environment)};
}
return this.urls;
}
private int determinePort(Environment environment) {
Assert.notNull(environment, "Environment must not be null");
String localPort = environment.getProperty("local.ldap.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
return DEFAULT_PORT;
}
}
package com.ctrip.framework.apollo.portal.spi.ldap;
import com.ctrip.framework.apollo.portal.entity.bo.LdapUserInfo;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.spi.UserService;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.ldap.query.SearchScope;
import org.springframework.util.CollectionUtils;
/**
* @author xm.lin xm.lin@anxincloud.com
* @Description
* @date 18-8-9 下午4:42
*/
public class LdapUserService implements UserService {
@Autowired
private LdapTemplate ldapTemplate;
@Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
List<LdapUserInfo> ldapUserInfoList = null;
if (Strings.isNullOrEmpty(keyword)) {
ldapUserInfoList = ldapTemplate.findAll(LdapUserInfo.class);
} else {
ContainerCriteria criteria = LdapQueryBuilder
.query().searchScope(SearchScope.SUBTREE)
.where("cn").like(keyword + "*")
.or("sn").like(keyword + "*");
ldapUserInfoList = ldapTemplate.find(criteria, LdapUserInfo.class);
}
return convertLdapUserToUserInfo(ldapUserInfoList);
}
@Override
public UserInfo findByUserId(String userId) {
ContainerCriteria criteria = LdapQueryBuilder.query().where("cn").is(userId);
LdapUserInfo ldapUser = ldapTemplate.findOne(criteria, LdapUserInfo.class);
UserInfo userInfo = new UserInfo();
userInfo.setUserId(ldapUser.getUsername());
userInfo.setName(ldapUser.getRealName());
userInfo.setEmail(ldapUser.getMail());
return userInfo;
}
@Override
public List<UserInfo> findByUserIds(List<String> userIds) {
if (!CollectionUtils.isEmpty(userIds)) {
LdapQueryBuilder ldapQueryBuilder = LdapQueryBuilder.query()
.searchScope(SearchScope.SUBTREE);
ContainerCriteria criteria = ldapQueryBuilder.where("cn").is(userIds.get(0));
userIds.stream().skip(1).forEach(userId -> {
criteria.or("cn").is(userId);
});
List<LdapUserInfo> ldapUserInfoList = ldapTemplate.find(criteria, LdapUserInfo.class);
return convertLdapUserToUserInfo(ldapUserInfoList);
}
return null;
}
private List<UserInfo> convertLdapUserToUserInfo(List<LdapUserInfo> ldapUserInfoList) {
List<UserInfo> userInfoList = Lists.newArrayList();
if (!CollectionUtils.isEmpty(ldapUserInfoList)) {
ldapUserInfoList.stream().map(p -> {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(p.getUsername());
userInfo.setName(p.getRealName());
userInfo.setEmail(p.getMail());
return userInfo;
}).forEach(userInfoList::add);
}
return userInfoList;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment