-2
需要一些包含語句的幫助。登錄到我的應用程序後,我正在驗證用戶標識的屬性。包含聲明不返回特定字符串
這在我們的測試環境中效果很好,但是當我們進入生產時,我沒有看到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;
}
}
}
缺少通過IDE進行調試,您是否嘗試過(可能是您的控制檯/ IDE中的顯示問題?),而不是打印出整個矢量,只需打印大小(attributeValues.size())即可排除控制檯不能正確顯示?如果沒有,我會調試它,看看你可以收集什麼。 – Mike
Vector attributeValues = new Vector(); 這將無法正常工作 - 代碼移動時出錯:1.4源代碼中不支持泛型(使用源5或更高版本來啓用泛型) –
FirePhoenix
剛剛移動了 attributeValues.add(「CN = nw-PPARead,OU = LDAP,OU = NSC託管,OU =全局,OU =組,DC = NWIE,DC = NET「); ,它的工作原理。 雖然拿出了Vector,因爲這不兼容。當我打印出屬性時,CN = nw-PPARead,OU = LDAP,OU = NSC Managed,OU = Global,OU =組,DC = NWIE,DC = NET與其他一些打印在一起。 –
FirePhoenix