为了让用户用好新零售风险防控中心,尽可能给用户提供方便……
说吧,要什么?
就是阿里云邮箱的那个通讯录,能够查询邮件组的接口。
又是烦阿里云邮的同学,你要的目前没有,HSF、HTTP也都没有,不过感谢同学还是给我指了个方向,开放的LDAP协议,我的f**k,书到用时方恨少,各种百度谷歌……
概念性的百科知识那些什么的大家请自学哈。
ldap java api
http://directory.apache.org/api/java-api.html
⭐️UnboundID LDAP sdk for java
https://docs.ldap.com/ldap-sdk/docs/index.html
我这次用的是我直接借力UnboundID LDAP去实现的,下面直接把结果和代码晒给大家(springboot工程):
个人邮箱查询结果
邮件组信息查询结果
application.properties
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
Bean
@Component
public class LdapClient {
private Logger logger = LoggerFactory.getLogger(LdapClient.class);
@Value("${spring.ldap.ldapBindDN}")
private String ldapBindDN;
@Value("${spring.ldap.ldapPassword}")
private String ldapPassword;
@Value("${spring.ldap.domain}")
private String ldapHost;
@Value("${spring.ldap.port}")
private int ldapPort;
private LDAPConnectionPool connectionPool = null;
@Bean
public LDAPConnectionPool initLDAPConnectionPool() {
try {
SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager());
LDAPConnection connection = new LDAPConnection(sslUtil.createSSLSocketFactory(), ldapHost, ldapPort);
BindResult bindResult = connection.bind(ldapBindDN, ldapPassword);
this.connectionPool = new LDAPConnectionPool(connection, 10);
} catch (LDAPException | GeneralSecurityException e) {
e.printStackTrace();
}
return this.connectionPool;
}
public LDAPConnection getConnection() {
try {
return connectionPool.getConnection();
} catch (LDAPException e) {
e.printStackTrace();
}
return null;
}
}
ServiceImpl
@Service
public class LdapServiceImpl implements LdapService {
@Value("${spring.ldap.baseDN}")
private String baseDN;
@Resource
private LdapClient ldapClient;
@Override
public JSONArray queryCn(String keyword) {
LDAPConnection connection = ldapClient.getConnection();
try {
SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.SUB,
Filter.createEqualityFilter("cn", keyword));
searchRequest.addControl(new SubentriesRequestControl());
SearchResult searchResult = connection.search(searchRequest);
if (searchResult.getResultCode() == ResultCode.SUCCESS) {
JSONArray jsonArray = new JSONArray();
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
JSONObject jsonObject = new JSONObject();
entry.getAttributes().forEach((k) -> {
jsonObject.put(k.getName(), k.getValue());
});
jsonArray.add(jsonObject);
}
return jsonArray;
} else {
return null;
}
} catch (LDAPException e) {
e.printStackTrace();
}
return null;
}
}
谢谢阅读,欢迎大家指正。