需要一些包含声明的帮助。登录我的应用程序后,我正在验证用户ID的属性。
这在我们的测试环境中效果很好,但是当我们去制作时,我没有看到id的属性。测试和生产之间的区别在于,在测试中,我们使用具有两个属性的测试ID - 读取和写入。在生产中,我们使用我们的id,它具有与之相关的更多属性。
代码如下。我已经插入了一些println来试图找出代码的去向。在生产中,//4
行正在打印所有属性(包括代码中列出的2个属性),但它永远不会出现在//5
的代码块中。
public class LoginFilter implements Filter {
private Hashtable hashtable;
private String url;
private String searchBase;
private Log vmsLogger;
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException,
IOException {
HttpServletRequest req = (HttpServletRequest) request;
if (vmsLogger.isInfoEnabled()) {
vmsLogger.info("** Pre-login filter check **");
}
chain.doFilter(request, response);
if (vmsLogger.isInfoEnabled()) {
vmsLogger.info("** Post-login filter check **");
}
try {
Subject callerSubject;
WSCredential callerCred;
callerSubject = WSSubject.getCallerSubject();
if (callerSubject != null) {
callerCred = (WSCredential) callerSubject.getPublicCredentials(WSCredential.class).iterator().next();
callerCred.set("SpecialRole", "AutoAgent");
//System.out.println("callerCred = " + callerCred);
getAttributes(req, callerCred);
}
} catch (WSSecurityException wse) {
wse.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (vmsLogger.isInfoEnabled()) {
vmsLogger.info("** Post-login filter check end **");
}
}
public void init(FilterConfig config) throws ServletException {
vmsLogger = LogFactory.getLog(this.getClass().getName());
}
private String getLDAPUrl(){
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("authenticate.properties");
//System.out.println("AuthProperties = " + inputStream);
Properties props = new Properties();
try {
props.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url;
String serverName = props.getProperty("SERVER");
String port = props.getProperty("PORT");
url = "ldap://" + serverName + ":" + port;
//System.out.println("props = " + props);
//System.out.println("url = " + url);
return url;
}
private void getAttributes(HttpServletRequest request, WSCredential cred) {
try {
intialize(cred);
String attribute = "memberOf";
String[] attrIDs = new String[] { attribute };
Hashtable hshtbl = new Hashtable();
hshtbl = authWithData(cred.getUniqueSecurityName(), cred.getSecurityName(), request
.getParameter("j_password"), attrIDs);
Vector attributeValues = new Vector();
if ((hshtbl != null) && hshtbl.containsKey(attribute)) {
attributeValues = (Vector) hshtbl.get(attribute);
System.out.println("12 hshtbl not null = " + attributeValues); //3
//System.out.println("attribute attrIDs = " + attrIDs);
}
HttpSession session = request.getSession(false);
UserInfo userInfo = new UserInfo();
if (attributeValues != null) {
System.out.println("attributeValues not null = " + attributeValues); //4
if (attributeValues.
attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET") ||
attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET") ||
attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET")||
attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET")) {
//Not getting to below statement
System.out.println("AttributeValues out of first if" + attributeValues); //5
session.setAttribute("privPPA", Boolean.TRUE);
if (attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET") ||
attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET")) {
userInfo.setPrivPPAWrite(Boolean.TRUE);
} else {
userInfo.setPrivPPAWrite(Boolean.FALSE);
}
if (attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET") ||
attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET")) {
userInfo.setPrivPPARead(Boolean.TRUE);
System.out.println("2 TuserReadInfo = true");
} else {
userInfo.setPrivPPARead(Boolean.FALSE);
}
}
if (attributeValues.contains("BoatRead") || attributeValues.contains("BoatWrite")) {
session.setAttribute("privBoat", Boolean.TRUE);
if (attributeValues.contains("BoatWrite")) {
userInfo.setPrivBoatWrite(Boolean.TRUE);
} else {
userInfo.setPrivBoatWrite(Boolean.FALSE);
}
if (attributeValues.contains("BoatRead")) {
userInfo.setPrivBoatRead(Boolean.TRUE);
} else {
userInfo.setPrivBoatRead(Boolean.FALSE);
}
}
if (attributeValues.contains("MotorCycleRead") || attributeValues.contains("MotorCycleWrite")) {
session.setAttribute("privMotorCycle", Boolean.TRUE);
if (attributeValues.contains("MotorCycleWrite")) {
userInfo.setPrivMotorCycleWrite(Boolean.TRUE);
} else {
userInfo.setPrivMotorCycleWrite(Boolean.FALSE);
}
if (attributeValues.contains("MotorCycleRead")) {
userInfo.setPrivMotorCycleRead(Boolean.TRUE);
} else {
userInfo.setPrivMotorCycleRead(Boolean.FALSE);
}
}
userInfo.setUserID(request.getUserPrincipal().getName());
session.setAttribute("userInfo", userInfo);
System.out.println("userInfo out = " + userInfo);
}
} catch (Exception ex1) {
vmsLogger.error("exception", ex1);
}
}
private void intialize(WSCredential cred) throws Exception {
//ResourceBundle labels = ResourceBundle.getBundle("VMSPPAConfig");
// Set up default values for LDAP info
url = getLDAPUrl();
StringTokenizer stk = new StringTokenizer(cred.getUniqueSecurityName(), ",");
String baseDN = "";
String str = null;
while (stk.hasMoreTokens()) {
str = stk.nextToken();
if (str.startsWith("dc=")) {
if (baseDN.equals("")) {
baseDN = str;
} else {
baseDN += "," + str;
}
}
}
searchBase = baseDN;
// Set up LDAP config settings
hashtable = new Hashtable();
hashtable.put("java.naming.ldap.version", "3");
hashtable.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
hashtable.put("java.naming.security.authentication", "Simple");
hashtable.put("java.naming.security.protocol", "ssl");
hashtable.put("java.naming.referral", "follow");
hashtable.put("java.naming.provider.url", url);
}
private Hashtable authWithData(String userDN, String user, String pass, String[] attIDs) throws Exception {
Hashtable ht1;
NamingEnumeration namingenumeration;
label0: {
InitialDirContext initialdircontext = null;
hashtable.put("java.naming.security.principal", userDN);
hashtable.put("java.naming.security.credentials", pass);
String uid = "CN=" + user;
SearchControls searchcontrols = new SearchControls();
searchcontrols.setSearchScope(2);
//System.out.println("uid = " + uid);
//System.out.println("userDN = " + userDN);
//System.out.println("user = " + user);
//System.out.println("pass = " + pass);
//System.out.println("attIDs = " + attIDs);
if (attIDs != null) {
searchcontrols.setReturningAttributes(attIDs);
}
try {
try {
initialdircontext = new InitialDirContext(hashtable);
} catch (Exception ex) {
throw ex;
}
namingenumeration = initialdircontext.search(searchBase, uid, searchcontrols);
if ((namingenumeration != null) && namingenumeration.hasMore()) {
break label0;
}
hashtable = null;
} finally {
if (initialdircontext != null) {
try {
initialdircontext.close();
} catch (Exception ex) {
vmsLogger.error("Some internal error has occurred at this point", ex);
throw ex;
}
}
}
return hashtable;
}
hashtable = populateReturn(namingenumeration);
ht1 = hashtable;
System.out.println("ht1 Hashtable = " + ht1); //2
return ht1;
}
private Hashtable populateReturn(NamingEnumeration namingenumeration) throws Exception {
Hashtable ht = new Hashtable();
SearchResult searchresult = (SearchResult) namingenumeration.next();
Attributes attributes = searchresult.getAttributes();
String s;
Vector vector;
for (NamingEnumeration namingenumeration1 = attributes.getIDs(); (namingenumeration1 != null)
&& namingenumeration1.hasMore(); ht.put(s, vector)) {
s = (String) namingenumeration1.next();
vector = new Vector();
int i = 0;
for (NamingEnumeration namingenumeration2 = attributes.get(s).getAll(); (namingenumeration2 != null)
&& namingenumeration2.hasMore(); vector.addElement((String) namingenumeration2.next())) {
i++;
}
}
System.out.println("ht under Hashtable = " + ht); //1
if (ht.isEmpty()) {
return null;
} else {
return ht;
}
}
}
答案 0 :(得分:0)
这看起来不对我
NaN (Not a Number)
对于任何条件,您的if语句都不会返回true。试试这个简单的测试:
if (attributeValues != null) {
System.out.println("attributeValues not null = " + attributeValues); //4
if (**attributeValues.**
attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET") ||
attributeValues.contains("CN=nw-PPARead,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET") ||
attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIE,DC=NET")||
attributeValues.contains("CN=nw-PPAWrite,OU=LDAP,OU=NSC Managed,OU=Global,OU=Groups,DC=NWIEPILOT,DC=NET")) {
//Not getting to below statement
你应该点击// 5 sysout,所以我要仔细检查你的IF逻辑中的硬编码字符串。