一個web片斷是web.xml文件的部分或所有,被包含的庫或框架的JAR的 META-INF目錄。若是這個框架捆綁在當前項目的WEB-INF/lib目錄中, 容器將發現找到並配置框架,而無需開發人員
額外再作任何事情。 它能夠包括幾乎全部能夠在web.xml中指定的元素。配置文件必須是web-fragment.xml. xml.這個名稱。php
<web-fragment>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>org.example.MyFilter</filter-class>
<init-param>
<param-name>myInitParam</param-name>
<param-value>...</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-fragment>html
開發人員能夠指定在web.xml和web-fragment.xml.xml中的資源加載的順序。在web.xml中<absolute-ordering>元素 用於指定在其中應加載的資源的確切順序,在web-fragment.xml.xml的<order>也是用於指定相對排序。兩個命令是互斥的,而絕對順序覆蓋相對順序。 絕對順序包含一個或多個<NAME>元素指定的名稱、要加載的資源和順序。指定<others/>
規定在順序中沒有命名的其餘資源加載。java
<web-app>
<name>MyApp</name>
<absolute-ordering>
<name>MyServlet</name>
<name>MyFilter</name>
</absolute-ordering>
</web-app>web
在web.xml中指定的資源被加載順序依次是MyServlet和MyFilter。編程
<before> 和 <after> 能夠用在 <ordering>中,指定時間前後:安全
<web-fragment>
<name>MyFilter</name>
<ordering>
<after>MyServlet</after>
</ordering>
</web-fragment>app
指定在MyServlet後面加載資源MyFilter。框架
若是web.xml 設置 metadata-complete 爲true,web-fragment.xml不被處理。
ide
Servlet一般經過互聯網訪問,所以具備安全性要求,使用註釋或在web.xml中能夠指定servlet的安全模型,包括角色,訪問控制 和認證要求。 @ ServletSecurity用於指定的servlet實現安全約束,適合 類的全部方法或特定的doXXX方法。容器將強制由用戶指定的角色調用執行 對應的doXXX:ui
@WebServlet("/account")
@ServletSecurity(
value=@HttpConstraint(rolesAllowed = {"R1"}),
httpMethodConstraints={
@HttpMethodConstraint(value="GET", rolesAllowed="R2"),
@HttpMethodConstraint(value="POST", rolesAllowed={"R3", "R4"})
}
)
public class AccountServlet extends javax.servlet.http.HttpServlet {
//. . .
}
@ HttpMethodConstraint用於指定該doGet方法能夠是屬於R2角色的用戶訪問,doPost方法調用能夠由用戶是R3和R4角色時被調用。@ HttpConstraint指定全部其餘方法能夠被
屬於角色R1的用戶調用。
也能夠在web.xml中定義:
<security-constraint>
<web-resource-collection>
<url-pattern>/account/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>INTEGRITY</transport-guarantee>
</user-data-constraint>
</security-constraint>
這個部署描述符要求只有在/account/*的URL GET方法 被保護。此方法只能由一個要求內容完整的manager角色的用戶訪問,全部其餘的GET HTTP方法是不受保護。不指定GET等方法,就是指全部方法。
出如今Servlet3.1的新元素<deny-uncovered-http-methods>元素,能夠用於拒絕對未覆蓋的HTTP方法請求拒絕, 請求返回一個403(SC_FORBIDDEN)狀態碼:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<deny-uncovered-http-methods/>
<web-resource-collection>
<url-pattern>/account/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
. . .
</web-app>
該<deny-uncovered-http-methods>元素確保HTTP GET是 調用所需的安全認證,和全部其餘的HTTP方法被拒絕返回 一個403狀態碼。
@RolesAllowed, @DenyAll, @PermitAll, 和 @TransportProtected提供基於代碼方法的安全控制訪問:
@RolesAllowed("R2")
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//. . .
}
Serlvet 3.1提供了兩個預約義角色:
*映射到任何已定義的角色。
**映射到任何身份驗證的用戶獨立的做用。
@ RolesAllowed,@ DenyAll或@ PermitAll中只能有一個在目標指定。而@TransportProtected註釋可能出現的組合是@RolesAllowed 或@ PermitAll註解。
Servlet能被配置成HTTP Basic, HTTP Digest, HTTPS Client, 和 formbased authentication等方式。
<form method="POST" action="j_security_check">
<input type="text" name="j_username">
<input type="password" name="j_password" autocomplete="off">
<input type="button" value="submit">
</form>
這段代碼顯示瞭如何基於表單的身份驗證明現。登陸表單必須 包含用於輸入用戶名和密碼字段。這些字段必須命名爲 爲j_username和j_password。表單的Action老是j_security_check。
Servlet的3.1須要在密碼錶單字段自動完成設置「關閉」,進一步增強 基於servlet的形式的安全性。
HttpServletRequest的還提供了編程安全與登陸,日誌 出,並驗證方法。
一個校驗realm用來驗證用戶名和密碼,配置在ServletContext中。這可確保該容器中的請求的getUserPrincipal,getRemoteUser和getAuthType方法返回一個 有效值。
login方法能夠做爲一個替代基於表單的登陸。爲容器配置的登陸驗證機制將驗證這個發出要求登入請求的用戶。