2015-05-12 111 views
2

我想遍歷ServletRequest中的所有可用屬性。但不知何故,當這樣做,並不是所有可用的屬性都顯示出來。這似乎是因爲當請求枚舉中未顯示的特定屬性時,該值將被正確打印。遍歷所有ServletRequest屬性

代碼迭代屬性和用於示出特定屬性

HttpServletRequest request = this.getHttpServletRequest(); 
Enumeration en = request.getAttributeNames(); 
while (en.hasMoreElements()) 
{ 
    Object currentElem = en.nextElement(); 
    System.out.println("currentElem.getClass(): " + currentElem.getClass()); 
    System.out.println("currentElem.toString(): " + currentElem); 
} 
Object specificAttrValue = request.getAttribute("Shib-Identity-Provider"); 
System.out.println("\nspecific attr: " + specificAttrValue); 

輸出:

currentElem.getClass():類java.lang.String
currentElem.toString():corsFilter。過濾
currentElem.getClass():類java.lang.String
currentElem.toString():org.springframework.web.context.request.RequestContextListener.REQUE ST_ATTRIBUTES
currentElem.getClass():類java.lang.String
currentElem.toString():__spring_security_scpf_applied
currentElem.getClass():類java.lang.String
currentElem.toString():__spring_security_session_mgmt_filter_applied
currentElem .getClass():類java.lang.String
currentElem.toString():org.springframework.security.web.FilterChainProxy.APPLIED
currentElem.getClass():類java.lang.String
currentElem.toString( ):__spring_security_filterSecurityInterceptor_filterApplied

具體ATTR:https://idp.testshib.org/idp/shibboleth

爲什麼迭代不顯示Shib-身份提供商作爲一個可用的屬性?

如何迭代「隱藏」的實際可用屬性?

注:我想訪問的屬性由Shibboleth服務提供商設置。請求首先發送到Apache服務器,然後發送到Shibboleth,然後發送到testshib.org身份提供程序,返回Shibboleth,並根據內容的不同,請求通過一些屬性(這些是我需要訪問的)獲得豐富,然後它被路由到Tomcat servlet。

+1

服務器端錯誤配置'getAttributeNames()'實際上返回'枚舉',使用這將使您的代碼示例更容易閱讀,即不打印屬性名稱類名稱(無論如何它們都是字符串)。 –

+0

我在Javadoc中讀取了返回字符串Enumeration的方法,但是當將該變量聲明爲Enumeration 時,編譯器警告說需要對字符串進行未經檢查的轉換。 – backendev

+0

顯然,Tomcat和Glassfish都會發生這種情況。 Shibboleth名單上的人似乎認爲這是一個錯誤:http://shibboleth.net/pipermail/users/2015-June/022260.html –

回答

1

如果我看到這個帖子裏面有同樣的問題:這個容器的 Retrieving Shibboleth attributes from AJP connector request

它看起來像getAttributeNames()沒有很好出於某種原因實施,並且不返回所有屬性的

這個鏈接可以幫助過:

+0

謝謝。所以這三個帖子表明直接通過名稱獲取屬性,設置後自己可以工作,但通過迭代獲取所有屬性不起作用。就像你一樣,我也會得出結論,容器的getAttributeNames()中似乎存在一個錯誤。我會盡力找到一種方法來改變這種狀況。我會接受你的答案,如果我沒有找到一個(我只是不想放棄希望:)) – backendev

+0

我真的不知道這個框架,所以我不能得出結論,因爲我沒有測試它。如果我是你,我會仔細看看你的配置,因爲它可能是最簡單的解決方案(實際上是唯一的解決方案)。 getAttributeNames()不起作用的事實不是一個解決方案,但更可能是一個[「功能」](http://fr.wiktionary.org/wiki/it%E2%80%99s_not_a_bug,_it%E2 %80%99s_a_feature)Shibboleth –

+0

我沒有時間進一步檢查此問題,並使用傳遞請求的替代方法來代替屬性映射,以便該方法可以直接訪問請求。對於每個想要進一步檢查這個問題的人,可能會檢查使用的Request類中的內部值(取決於Framework,對我來說是這樣的:http://grepcode.com/file/repo1.maven.org/maven2/org。 apache.geronimo.ext.tomcat/catalina/7.0.39.2/org/apache/catalina/connector/RequestFacade.java#RequestFacade.getAttributeNames%28%29)和使用的超類。只是一個想法 - 也可能是一個死衚衕。 – backendev