2012-07-18 179 views
4

我的Web應用程序(使用Struts2的創建,包含2頁啓用基於角色的安全

  • 1)培養要求
  • 2)批准請求

)需要爲此應用程序啓用基於角色的安全性。我有兩個角色

1)用戶(誰可以提出請求)

2)審批

兩個具有不同credentials.I我不使用anyback結束進行身份驗證。如何通過web.xml和映射用戶使用websphere安全功能執行此操作。

+0

我不會建議你只使用基於服務器的身份驗證,並會強烈建議你使用一些安全框架,比如spring security – 2012-07-29 06:24:25

+0

我把後端解釋爲DB ......你的意思是你想要硬編碼所有的認證到容器的XML文件? – Quaternion 2012-07-30 17:16:16

回答

1

我邀請您閱讀JavaEE 6 Tutorial chapter "Getting Started Securing Web Applications",特別是提供的示例。

您的應用程序聲明兩個安全角色userapproverweb.xml具有保護servlet路徑感謝security-constraints

這裏是這樣的設置爲起點:

<security-constraint> 
    <display-name>Raise Request</display-name> 
    <web-resource-collection> 
     <web-resource-name>raiserequestservlet</web-resource-name> 
     <description/> 
     <url-pattern>/raiserequest</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <description/> 
     <role-name>user</role-name> 
    </auth-constraint> 
</security-constraint> 
<security-constraint> 
    <display-name>Approve Request</display-name> 
    <web-resource-collection> 
     <web-resource-name>approverequestservlet</web-resource-name> 
     <description/> 
     <url-pattern>/approverequest</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <description/> 
     <role-name>approver</role-name> 
    </auth-constraint> 
</security-constraint> 

<login-config> 
    <auth-method>BASIC</auth-method> 
    <realm-name>WebSphere</realm-name> 
</login-config> 

<security-role> 
    <description>Security Role required to raise a request</description> 
    <role-name>user</role-name> 
</security-role> 
<security-role> 
    <description>Security Role required to approve a request</description> 
    <role-name>approver</role-name> 
</security-role> 

對於第一測試,我所選擇的基本認證但也有其他的選擇。

然後,在將WAR包部署到WebSphere中時,嚮導將允許您將兩個應用程序角色映射到LDAP組,只要您使用LDAP作爲後端進行身份驗證和權限,強烈推薦。

其運行應用程序的服務器實例默認配置爲使用全球安全,但你可以爲你的服務器/應用夫婦使用專用後端創建一個專用的安全域。這裏是network deployment reference documentation security section指導你的方面。

+0

除了LDAP,您還可以在WAS中使用簡單的內置用戶註冊表。通常,我們將它用於開發和概念驗證,並在生產中使用LDAP。 – RajV 2012-08-03 20:19:59

+0

我知道,但我還沒有在InfoCenter中找到任何參考資料,但尚未將其包含在我的答案中。 – 2012-08-06 20:49:43