清理Java

时间:2016-12-08 12:33:45

标签: java regex ldap openldap ldap-query

有人可以在下面的代码中指导我,

public String escapeDN (String name) {
       //From RFC 2253 and the / character for JNDI
       final char[] META_CHARS = {'+', '"', '<', '>', ';', '/'};
       String escapedStr = new String(name);
       //Backslash is both a Java and an LDAP escape character, so escape it first
       escapedStr = escapedStr.replaceAll("\\\\\\\\","\\\\\\\\"); 
       //Positional characters - see RFC 2253
       escapedStr = escapedStr.replaceAll("\^#","\\\\\\\\#");
       escapedStr = escapedStr.replaceAll("\^ | $","\\\\\\\\ ");
       for (int i=0;i < META_CHARS.length;i++) {
           escapedStr = escapedStr.replaceAll("\\\\"+META_CHARS[i],"\\\\\\\\" + META_CHARS[i]);
       }
       return escapedStr;
   }


 public String escapeSearchFilter (String filter) {
       //From RFC 2254
       String escapedStr = new String(filter);
       escapedStr = escapedStr.replaceAll("\\\\\\\\","\\\\\\\\5c");
       escapedStr = escapedStr.replaceAll("\\\\\*","\\\\\\\\2a");
       escapedStr = escapedStr.replaceAll("\\\\(","\\\\\\\\28");
       escapedStr = escapedStr.replaceAll("\\\\)","\\\\\\\\29");
       escapedStr = escapedStr.replaceAll("\\\\"+Character.toString('\\u0000'), "\\\\\\\\00");
       return escapedStr;
   } 

在上面的代码中,我可以理解LDAP baseDN和searchFilter值在执行之前已经过清理,但是我无法理解这些方法为什么以及如何工作......

提前致谢!!!

1 个答案:

答案 0 :(得分:1)

以通用方式解析DN是一项艰巨的任务。我建议您使用特定的LDAP API,例如UnboundIDApache LDAP

如果您希望看到一些复杂性,请查看:https://docs.ldap.com/ldap-sdk/docs/javadoc/src-html/com/unboundid/ldap/sdk/DN.html#line.280

-Jim