1. 程式人生 > >將宣告性安全用於 Web 應用程式

將宣告性安全用於 Web 應用程式

下列主題描述了在 web.xmlweblogic.xml 檔案中用於定義 Web 應用程式的安全要求的部署描述符元素:

web.xml 部署描述符

WebLogic Server 支援以下與 web.xml 安全相關的部署描述符元素:

auth-constraint

可選的 auth-constraint 元素定義哪些組或委託人有權訪問在此安全約束中定義的 Web 資源集合。

下表描述可以在 auth-constraint 元素中定義的元素。

表 3-1 auth-constraint 元素
元素 必需/
可選
描述
<description> 可選 此安全約束的文字描述。
<role-name> 可選 定義哪些安全形色可以訪問在此 <security-constraint>
中定義的資源。使用 <security-role-ref> 元素將安全形色名稱對映到委託人請參閱 security-role-ref
使用位置

security-constraint 元素中使用 auth-constraint 元素。

示例

有關如何在 web.xml 檔案中使用 auth-constraint 元素的示例,請參閱清單 3-10

security-constraint

security-constraint 元素在 web.xml 檔案中用於定義對 web-resource-collection 元素定義的資源集合的訪問許可權。

下表描述可在 security-constraint 元素中定義的元素。

表 3-2 security-constraint元素
元素 必需/
可選
描述
<web-resource-
collection>
必需
<auth-constraint> 可選 定義哪些組或委託人有權訪問在此安全約束中定義的 Web 資源的集合。有關詳細資訊,請參閱 auth-constraint
<user-data-
constraint>
可選 定義應如何保護客戶端和伺服器之間的通訊資料。有關詳細資訊,請參閱 user-data-constraint
示例

清單 3-10 說明如何使用 security-constraint 元素定義 web.xml 檔案中的 SecureOrdersEast 資源安全。

清單 3-10 安全約束示例
web.xml entries:
<security-constraint>
     <web-resource-collection>
          <web-resource-name>SecureOrdersEast</web-resource-name>
          <description>
             Security constraint for
             resources in the orders/east directory
          </description>
          <url-pattern>/orders/east/*</url-pattern>
          <http-method>POST</http-method>
          <http-method>GET</http-method>
     </web-resource-collection>
     <auth-constraint>
          <description>
           constraint for east coast sales
          </description>
          <role-name>east</role-name>
          <role-name>manager</role-name>
     </auth-constraint>
 <user-data-constraint>
          <description>SSL not required</description>
          <transport-guarantee>NONE</transport-guarantee>
     </user-data-constraint>
</security-constraint>
...

security-role

security-role 元素包含安全形色的定義。該定義包含安全形色的可選描述和安全形色名稱。

下表描述可在 security-role元素中定義的元素。

表 3-3 security-role 元素
元素 必需/
可選
描述
<description> 可選 此安全形色的文字描述。
<role-name> 必需 角色名。此處使用的名稱在 WebLogic 特定的部署描述符 weblogic.xml 中必須有對應的條目,該部署描述符將角色對映到安全領域中的委託人。有關詳細資訊,請參閱 security-role-assignment
示例

有關如何在 web.xml 檔案中使用 security-role 元素的示例,請參閱清單 3-13

security-role-ref

security-role-ref 元素將 <security-role> 定義的安全形色名連結到使用 Servlet 邏輯進行硬編碼的備用角色名。使用此附加提取層,可以在部署時配置 Servlet,且不必更改 Servlet 程式碼。

下表描述可在 security-role-ref元素中定義的元素。

表 3-4 security-role-ref 元素
元素 必需/
可選
描述
<description> 可選 角色的文字描述。
<role-name> 必需 定義在 Servlet 程式碼中使用的安全形色或委託人的名稱。
<role-link> 必需 定義稍後將在部署描述符中的 <security-role> 元素中定義的安全形色的名稱。
示例

有關如何在 web.xml 檔案中使用 security-role-ref 元素的示例,請參閱清單 3-16

user-data-constraint

user-data-constraint 元素定義應如何保護客戶端和伺服器之間的通訊資料。

下表描述了可以在 user-data-constraint 元素中定義的元素。

表 3-5 user-data-constraint 元素
元素 必需/
可選
描述
<description> 可選 文字描述。
<transport-
guarantee>
必需 指定客戶端和伺服器之間的通訊的資料安全要求。 值範圍: NONE- 應用程式不需要任何傳輸保證。 INTEGRAL- 應用程式要求資料在客戶端和伺服器之間傳輸的過程中不會被更改。 CONFIDENTIAL- 應用程式要求傳輸資料以防止其他實體觀察到傳輸的內容。 當使用 INTEGRALCONFIDENTIAL 傳輸保證對使用者進行身份驗證時,WebLogic Server 會建立安全套介面層 (SSL) 連線。
使用位置

security-constraint 元素中使用 user-data-constraint 元素。

示例

有關如何在 web.xml 檔案中使用 user-data-constraint 元素的示例,請參閱清單 3-10

web-resource-collection

web-resource-collection 元素標識了一個資源子集,以及在 Web 應用程式中針對這些資源且將應用安全約束的 HTTP 方法。如果未指定 HTTP 方法,則該安全約束應用於所有 HTTP 方法。

下表描述了可以在 web-resource-collection 元素中定義的元素。

表 3-6 web-resource-collection 元素
元素 必需/
可選
描述
<web-resource-name> 必需 此 Web 資源集合的名稱。
<description> 可選 Web 資源的文字描述。
<url-pattern> 必需 Web 資源集合的對映或位置。 模式 <url-pattern>/</url-pattern> 將安全約束應用於整個 Web 應用程式。
<http-method> 可選 當客戶端嘗試訪問 Web 資源集合時應用安全約束的 HTTP 方法。如果未指定 HTTP 方法,則安全約束應用於所有 HTTP 方法。
使用位置

security-constraint 元素中使用 web-resource-collection 元素。

示例

有關如何在 web.xml 檔案中使用 web-resource-collection 元素的示例,請參閱清單 3-10

weblogic.xml 部署描述符

WebLogic Server 支援以下與 weblogic.xml 安全相關的部署描述符元素:

externally-defined

externally-defined 元素使您可以明確指出,希望由 web.xml 部署描述符的 role-name 元素定義的安全形色使用在管理控制檯指定的對映。通過該元素,您可以不必為在特定 Web 應用程式的部署描述符中定義的每個安全形色指定特定的安全形色對映。所以,在同一安全領域中,部署描述符可用於指定和修改某些應用程式的安全性,而管理控制檯可用於指定和修改其他應用程式的安全性。

伺服器的角色對映行為取決於在管理控制檯上選擇哪個安全部署模型。有關安全部署模型的資訊,請參閱“使用角色和策略確保 WebLogic 資源安全”中的。

注意: 指定安全形色名稱時,請遵循以下約定和限制:
  • 不要使用空格、逗號、連字元或以下逗號分隔列表中的任何字元:/t、< >、#、|、&、~、?、( )、{ }。
  • 安全形色名區分大小寫。
  • BEA 建議對安全形色名使用以下約定:它們應該獨一無二。
使用位置

security-role-assignment 元素中使用 externally-defined 元素。

示例

清單 3-11清單 3-12 以比較的形式說明如何在 weblogic.xml 檔案中使用 externally-defined 元素。在 清單 3-12 中,指定 weblogic.xml 中的“webuser” externally-defined 元素的規範意味著,要針對 getReceipts 方法正確配置安全,必須在管理控制檯中為 webuser 建立委託人。

注意: 如果需要列出較多數量的委託人,則考慮指定組,而不是指定使用者。如果您指定的使用者過多,則會引起效能問題。
清單 3-11 使用 web.xml 和 weblogic.xml 檔案將安全形色和委託人對映到安全領域
web.xml entries:
<web-app>
...
<security-role>
<role-name>webuser</role-name>
</security-role>
...
</web-app>
<weblogic.xml entries:
<weblogic-web-app>
     <security-role-assignment>
<role-name>webuser</role-name>
<principal-name>myGroup</principal-name>
<principal-name>Bill</principal-name>
<principal-name>Mary</principal-name>
</security-role-assignment>
</weblogic-web-app>
清單 3-12 在 Web 應用程式部署描述符中使用 externally-defined 標記
web.xml entries:
<web-app>
...
<security-role>
<role-name>webuser</role-name>
</security-role>
...
</web-app>
<weblogic.xml entries:
<weblogic-web-app>
     <security-role-assignment>
<role-name>webuser</role-name>
<externally-defined/>
</security-role-assignment>

run-as-principal-name

run-as-principal-name 元素指定委託人的名稱,以用於由配套檔案 web.xml 中的 run-as 元素定義的安全形色。

使用位置

run-as-role-assignment 元素中使用 run-as-principal-name 元素。

示例

有關如何使用 run-as-principal-name 元素的示例,請參閱清單 3-13

run-as-role-assignment

run-as-role-assignment 元素將配套檔案 web.xml 中的 role-name 元素定義的給定角色名對映到系統中的有效使用者名稱。對於給定 Servlet,此值可由 Servlet 描述符中的 run-as-principal-name 元素替代。如果給定角色名缺少 run-as-role-assignment 元素,則 Web 應用程式容器會選擇在 security-role-assignment 元素中定義的第一個委託人名稱。

下表描述可在 run-as-role-assignment元素中定義的元素。

表 3-7 run-as-role-assignment 元素
元素 必需
可選
描述
<role-name> 必需 指定在配套檔案 web.xml 中的 run-as 元素中定義的安全形色名。
<run-as-principal-name> 必需 指定在配套檔案 web.xml 中的 run-as 元素中定義的安全形色名稱的委託人。
示例:

清單 3-13 說明如何使用 run-as-role-assignment 元素讓 SnoopServlet 始終作為使用者 joe 執行。

清單 3-13 run-as-role-assignment 元素示例
web.xml:
  <servlet>
<servlet-name>SnoopServlet</servlet-name>
<servlet-class>extra.SnoopServlet</servlet-class>
<run-as>
<role-name>runasrole</role-name>
</run-as>
</servlet>
<security-role>
<role-name>runasrole</role-name>
</security-role>
weblogic.xml:
  <weblogic-web-app>
<run-as-role-assignment>
<role-name>runasrole</role-name>
<run-as-principal-name>joe</run-as-principal-name>
</run-as-role-assignment>
</weblogic-web-app>

security-permission

security-permission 元素指定與 J2EE 沙盒相關聯的安全許可權。

示例

有關如何使用 security-permission 元素的示例,請參閱清單 3-14

security-permission-spec

security-permission-spec 元素根據安全策略檔案語法指定單個安全許可權。有關 Sun 的安全許可權規範的實現,請參閱以下 URL:

注意: 放棄可選的 codebase 和 signedBy 子句。
使用位置

security-permission 元素中使用 security-permission-spec 元素。

示例

清單 3-14 說明如何使用 security-permission-spec 元素將許可權授予 java.net.SocketPermission 類。

清單 3-14 security-permission-spec 元素示例
<weblogic-web-app>
<security-permission>
<description>Optional explanation goes here</description>
<security-permission-spec>
<!—
A single grant statement following the syntax of http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html#FileSyntax, without the "codebase" and "signedBy" clauses, goes here. For example:
-->
grant {
permission java.net.SocketPermission "*", "resolve";
};
</security-permission-spec>
</security-permission>
</weblogic-web-app>

清單 3-14 中,許可權 java.net.SocketPermission 是許可權類名,“*”表示目標名稱,resolve 表示操作(解析主機/IP 名稱服務查詢)。

security-role-assignment

security-role-assignment 元素宣告一個安全形色和 WebLogic Server 安全領域中的一個或多個委託人 s 之間的對映。

注意: 有關將 weblogic-application.xml 部署描述符中的 security-role-assignment 元素用於企業應用程式的資訊,請參閱“使用 WebLogic Server 開發應用程式”中的。
示例

清單 3-15 說明如何使用 security-role-assignment 元素為委託人分配 PayrollAdmin 角色。

注意: 如果需要列出較多數量的委託人,則考慮指定組,而不是指定使用者。如果您指定的使用者過多,則會引起效能問題。
清單  3-15 Security-role-assignment 元素示例
<weblogic-web-app>
<security-role-assignment>
<role-name>PayrollAdmin</role-name>
<principal-name>Tanya</principal-name>
<principal-name>Fred</principal-name>
<principal-name>system</principal-name>
</security-role-assignment>
</weblogic-web-app>

將程式設計安全用於 Web 應用程式

您可以在 Servlet 程式碼中編寫 Servlet 以訪問使用者和安全形色。要執行此操作,請在 servlet 程式碼中使用下列方法:javax.servlet.http.HttpServletRequest.getUserPrincipaljavax.servlet.http.HttpServletRequest.isUserInRole(String role) 方法。

getUserPrincipal

使用 getUserPrincipal() 方法確定 Web 應用程式的當前使用者。如果當前使用者中存在一個 WLSUser Principal,此方法會將其返回。如果有多個 WLSUser Principals,則此方法返回由 Subject.getPrincipals().iterator()方法定義的排序中的第一個。如果沒有WLSUser Principals,則 getUserPrincipal()方法返回第一個非 WLSGroup Principals。如果沒有Principals,或者所有Principals的型別都是 WLSGroup,則此方法返回 null。此行為與 weblogic.security.SubjectUtils.getUserPrincipal() 方法的語義相同。

isUserInRole

javax.servlet.http.HttpServletRequest.isUserInRole(String role) 方法返回一個布林型值,指出是否通過身份驗證的使用者被授予了指定的邏輯安全“角色”。如果使用者沒有通過身份驗證,該方法返回 false。

isUserInRole() 方法將安全形色對映到安全領域中的組名稱。清單 3-16 給出的元素和 <servlet> 元素一起使用,以便在 web.xml 檔案中定義安全形色。

清單 3-16 IsUserInRole web.xml 元素和 weblogic.xml 元素
Begin web.xml entries:
...
<servlet>
<security-role-ref>
<role-name>user-rolename</role-name>
<role-link>rolename-link</role-link>
</security-role-ref>
</servlet>
<security-role>
<role-name>rolename-link</role-name>
</security-role>
...
Begin weblogic.xml entries:
...
<security-role-assignment>
<role-name>rolename-link</role-name>
<principal-name>groupname</principal>
<principal-name>username</principal>
</security-role-assignment>
...

字串 role 對映到在 <role-name> 元素中提供的名稱,該元素巢狀在 web.xml 部署描述符中 <servlet>宣告的 <security-role-ref> 元素內。<role-name> 元素用於在 Servlet 程式碼中定義安全形色或委託人(使用者或組)的名稱。<role-link> 元素對映到在 weblogic.xml 部署描述符中的 <security-role-assignment> 元素中定義的 <role-name>

注意: 指定安全形色名稱時,請遵循以下約定和限制:
  • 不要使用空格、逗號、連字元或以下逗號分隔列表中的任何字元:/t、< >、#、|、&、~、?、( )、{ }。
  • 安全形色名區分大小寫。
  • BEA 建議對安全形色名使用以下約定:它們應該獨一無二。

例如,如果客戶端已作為安全形色為 manager 的使用者 Bill 成功登入,則以下方法將返回 true:

request.isUserInRole("manager")
清單 3-17 安全形色對映示例
Servlet code: 
out.println("Is the user a Manager? " +
                 request.isUserInRole("manager"));
web.xml entries:
<servlet>
. . .
   <role-name>manager</role-name>
   <role-link>mgr</role-link>
. . .
</servlet>
<security-role>
   <role-name>mgr</role-name>
</security-role>
weblogic.xml entries:
<security-role-assignment>
<role-name>mgr</role-name>
<principal-name>bostonManagers</principal-name>
<principal-name>Bill</principal-name>
<principal-name>Ralph</principal-name>
</security-role-ref>

使用程式設計身份驗證 API

有一些應用程式適合使用程式設計身份驗證。

WebLogic Server 提供伺服器端 API。該 API 支援從 Servlet 應用程式中進行程式設計身份驗證:

weblogic.servlet.security.ServletAuthentication

通過使用該 API,可以編寫 Servlet 程式碼來對使用者執行身份驗證、讓使用者登入,以及將使用者與當前會話相關聯,以便在預設(活動)安全領域中註冊該使用者。完成登入後,看上去就會像是使用者使用標準機制登入一樣。

您可以選擇將 WebLogic 提供的兩個類中的任意一個與 ServletAuthentication API 配合使用:weblogic.security.SimpleCallbackHandler 類或 weblogic.security.URLCallbackHandler 類。有關這些類的詳細資訊,請參閱

清單 3-18 顯示了使用 SimpleCallbackHandler 的示例。清單 3-19 顯示了使用 URLCallbackHandler 的示例。

清單 3-18 使用 SimpleCallbackHandler 類的程式設計身份驗證程式碼片段
CallbackHandler handler = new SimpleCallbackHandler(username,
                                                               password);
Subject mySubject =
weblogic.security.services.Authentication.login(handler);
weblogic.servlet.security.ServletAuthentication.runAs(mySubject, request);
// 該處請求為 httpservletrequest 物件。
清單 3-19 使用 URLCallbackHandler 類的程式設計身份驗證程式碼片段
CallbackHandler handler = new URLCallbackHandler(username,
                                                           password);
Subject mySubject =
weblogic.security.services.Authentication.login(handler);
weblogic.servlet.security.ServletAuthentication.runAs(mySubject, request);
// 該處請求httpservletrequest 物件。