Skip to content

Commit

Permalink
Merge pull request #179 from jamebal/develop
Browse files Browse the repository at this point in the history
fix: 修复ldap账号格式判断
  • Loading branch information
jamebal authored Nov 28, 2024
2 parents 040083f + ae6b2bb commit 2015931
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/main/java/com/jmal/clouddisk/service/impl/AuthServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ private static LdapContextSource loadLdapConfig(LdapConfigDTO ldapConfigDTO) {
if (isNotValidBaseDn(ldapConfigDTO.getBaseDN())) {
throw new CommonException(ExceptionType.WARNING.getCode(), "BaseDN格式错误, 应为 dc=xxx,dc=xxx");
}
if (isNotValidBaseDn(ldapConfigDTO.getUserDN())) {
throw new CommonException(ExceptionType.WARNING.getCode(), "账号格式错误, 应为 cn=xxx,ou=xxx,dc=xxx");
if (isNotValidDn(ldapConfigDTO.getUserDN())) {
throw new CommonException(ExceptionType.WARNING.getCode(), "账号格式错误, 应为 cn=xxx,ou=xxx,dc=xxx 或者 uid=xxx,ou=xxx,dc=xxx");
}
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://" + ldapConfigDTO.getLdapServer());
Expand Down Expand Up @@ -148,6 +148,29 @@ public static boolean isNotValidBaseDn(String baseDn) {
return baseDn == null || !baseDn.matches(regex);
}

/**
* 验证LDAP DN(Distinguished Name)字符串是否有效
*
* @param dn 要验证的DN字符串
* @return 如果DN无效,则为true;否则为false
*/
public static boolean isNotValidDn(String dn) {
if (dn == null || dn.trim().isEmpty()) {
return true;
}

// 支持常见的LDAP属性类型
String attrTypes = "(uid|cn|ou|dc|o|l|st|c)";
// 属性值允许包含除逗号外的任意字符
String attrValue = "([^,]+)";
// 完整的RDN(Relative Distinguished Name)格式
String rdn = attrTypes + "=" + attrValue;
// 完整的DN格式:一个或多个RDN,用逗号分隔
String regex = rdn + "(,\\s*" + rdn + ")*";

return !dn.matches(regex);
}

@Override
public ResponseResult<Object> logout(String token, HttpServletResponse response) {
Cookie cookie = new Cookie(AuthInterceptor.REFRESH_TOKEN, null);
Expand Down

0 comments on commit 2015931

Please sign in to comment.