`

web.xml中<security-constraint>和四种认证类型

 
阅读更多
<security-constraint> 的子元素 <http-method> 是可选的,如果没有 <http-method> 元素,这表示将禁止所有 HTTP 方法访问相应的资源。
子元素 <auth-constraint> 需要和 <login-config> 相配合使用,但可以被单独使用。如果没有 <auth-constraint> 子元素,这表明任何身份的用户都可以访问相应的资源。也就是说,如果 <security-constraint> 中没有 <auth-constraint> 子元素的话,配置实际上是不起中用的。如果加入了 <auth-constraint> 子元素,但是其内容为空,这表示所有身份的用户都被禁止访问相应的资源。
web.xml:
Xml代码 复制代码 收藏代码
  1. <security-constraint>     
  2.   <display-name>     
  3.   baseporject</display-name>     
  4.   <web-resource-collection>     
  5.    <web-resource-name>baseproject</web-resource-name>     
  6.    <url-pattern>*.jsp</url-pattern>     
  7.    <url-pattern>*.do</url-pattern>     
  8.    <http-method>GET</http-method>     
  9.    <http-method>PUT</http-method>     
  10.    <http-method>HEAD</http-method>     
  11.    <http-method>TRACE</http-method>     
  12.    <http-method>POST</http-method>     
  13.    <http-method>DELETE</http-method>     
  14.    <http-method>OPTIONS</http-method>     
  15.   </web-resource-collection>     
  16.   <auth-constraint>     
  17.    <description>     
  18.    baseproject</description>     
  19.    <role-name>All Role</role-name>     
  20.   </auth-constraint>     
  21.   <user-data-constraint>     
  22.    <transport-guarantee>NONE</transport-guarantee>     
  23.   </user-data-constraint>     
  24. </security-constraint>     
  25. <login-config>     
  26.   
  27. <security-constraint>    
  28.   <display-name>    
  29.   baseporject</display-name>    
  30.   <web-resource-collection>    
  31.    <web-resource-name>baseproject</web-resource-name>    
  32.    <url-pattern>*.jsp</url-pattern>    
  33.    <url-pattern>*.do</url-pattern>    
  34.    <http-method>GET</http-method>    
  35.    <http-method>PUT</http-method>    
  36.    <http-method>HEAD</http-method>    
  37.    <http-method>TRACE</http-method>    
  38.    <http-method>POST</http-method>    
  39.    <http-method>DELETE</http-method>    
  40.    <http-method>OPTIONS</http-method>    
  41.   </web-resource-collection>    
  42.   <auth-constraint>    
  43.    <description>    
  44.    baseproject</description>    
  45.    <role-name>All Role</role-name>    
  46.   </auth-constraint>    
  47.   <user-data-constraint>    
  48.    <transport-guarantee>NONE</transport-guarantee>    
  49.   </user-data-constraint>    
  50. </security-constraint>    
  51. <login-config>Xml代码    
  52. <!--四种验证方式,附在最后有说明-->     
  53.   <auth-method>FORM</auth-method>     
  54.   <form-login-config>     
  55.    <form-login-page>/login.html</form-login-page>     
  56.    <form-error-page>/error.html</form-error-page>     
  57.   </form-login-config>     
  58. </login-config>     
  59. <security-role>     
  60.   <role-name>All Role</role-name>     
  61. </security-role>     
  62.   
  63. <!--四种验证方式,附在最后有说明-->    
  64.   <auth-method>FORM</auth-method>    
  65.   <form-login-config>    
  66.    <form-login-page>/login.html</form-login-page>    
  67.    <form-error-page>/error.html</form-error-page>    
  68.   </form-login-config>    
  69. </login-config>    
  70. <security-role>    
  71.   <role-name>All Role</role-name>    
  72. </security-role>   
<security-constraint>  
  <display-name>  
  baseporject</display-name>  
  <web-resource-collection>  
   <web-resource-name>baseproject</web-resource-name>  
   <url-pattern>*.jsp</url-pattern>  
   <url-pattern>*.do</url-pattern>  
   <http-method>GET</http-method>  
   <http-method>PUT</http-method>  
   <http-method>HEAD</http-method>  
   <http-method>TRACE</http-method>  
   <http-method>POST</http-method>  
   <http-method>DELETE</http-method>  
   <http-method>OPTIONS</http-method>  
  </web-resource-collection>  
  <auth-constraint>  
   <description>  
   baseproject</description>  
   <role-name>All Role</role-name>  
  </auth-constraint>  
  <user-data-constraint>  
   <transport-guarantee>NONE</transport-guarantee>  
  </user-data-constraint>  
</security-constraint>  
<login-config>  

Xml代码 
<!--四种验证方式,附在最后有说明--> 
  <auth-method>FORM</auth-method> 
  <form-login-config> 
   <form-login-page>/login.html</form-login-page> 
   <form-error-page>/error.html</form-error-page> 
  </form-login-config> 
</login-config> 
<security-role> 
  <role-name>All Role</role-name> 
</security-role> 


    security-constriaint元素的用途是用来指示服务器使用何种验证方法了.此元素在web.xml中应该出现在login-config的紧前面。它包含是个可能的子元素,分别是:web-resource-collection、auth-constraint、user-data-constraint和display-name。下面各小节对它们进行介绍。
1. web-resource-collection
  此元素确定应该保护的资源,所有security-constraint元素都必须包含至少一个web-        resource-collection项.此元素由一个给出任意标识名称的web-resource-name元素、一个确定应该保护URL    的url-pattern元素、一个指出此保护所适用的HTTP命令(GET、POST等,缺省为所有方法)的http-method元素和一个提供资料的可选description元素组成。
    重要的是应该注意到,url-pattern仅适用于直接访问这些资源的客户机。特别是,它不适合于通过MVC体系结构利用RequestDispatcher来访问的页面,或者不适合于利用类似jsp:forward的手段来访问的页面。
2. auth-constraint
元素却指出哪些用户应该具有受保护资源的访问权。此元素应该包含一个或多个标识具有访问权限的用户类别role-name元素,以及包含(可选)一个描述角色的description元素。
3. user-data-constraint
这个可选的元素指出在访问相关资源时使用任何传输层保护。它必须包含一个transport-guarantee子元素(合法值为NONE、INTEGRAL或CONFIDENTIAL),并且可选地包含一个description元素。transport-guarantee为NONE值将对所用的通讯协议不加限制。INTEGRAL值表示数据必须以一种防止截取它的人阅读它的方式传送。虽然原理上(并且在未来的HTTP版本中),在INTEGRAL和CONFIDENTIAL之间可能会有差别,但在当前实践中,他们都只是简单地要求用SSL。
4 四种认证类型:

Xml代码 复制代码 收藏代码
  1. BASIC:HTTP规范,Base64    
  2. <web-app>    
  3.     ......    
  4.     <login-config>    
  5.         <auth-method>BASIC</auth-method>    
  6.     </login-config>    
  7.     ......    
  8. </web-app>    
  9.   
  10. DIGEST:HTTP规范,数据完整性强一些,但不是SSL    
  11. <web-app>    
  12.     ......    
  13.     <login-config>    
  14.         <auth-method>DIGEST</auth-method>    
  15.     </login-config>    
  16.     ......    
  17. </web-app>    
  18.   
  19. CLIENT-CERT:J2EE规范,数据完整性很强,公共钥匙(PKC)    
  20. <web-app>    
  21.     ......    
  22.     <login-config>    
  23.         <auth-method>CLIENT-CERT</auth-method>    
  24.     </login-config>    
  25.     ......    
  26. </web-app>   
BASIC:HTTP规范,Base64 
<web-app> 
    ...... 
    <login-config> 
        <auth-method>BASIC</auth-method> 
    </login-config> 
    ...... 
</web-app> 

DIGEST:HTTP规范,数据完整性强一些,但不是SSL 
<web-app> 
    ...... 
    <login-config> 
        <auth-method>DIGEST</auth-method> 
    </login-config> 
    ...... 
</web-app> 

CLIENT-CERT:J2EE规范,数据完整性很强,公共钥匙(PKC) 
<web-app> 
    ...... 
    <login-config> 
        <auth-method>CLIENT-CERT</auth-method> 
    </login-config> 
    ...... 
</web-app> 

FORM:J2EE规范,数据完整性非常弱,没有加密,允许有定制的登陆界面。
<web-app>
    ......
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.html</form-login-page>
            <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>
    ......
</web-app>
这里的 FORM 方式需要说明的是 登录页面的固定的元素:login.html

Html代码 复制代码 收藏代码
  1. <form name="loginform" method="post" action="j_security_check">    
  2.   
  3. <INPUT name="j_username" type="text">    
  4.   
  5. <INPUT name="j_password" TYPE="password">    
  6.   
  7. <input type="submit" value="登 录" >    
  8.   
  9. </form>   
<form name="loginform" method="post" action="j_security_check"> 

<INPUT name="j_username" type="text"> 

<INPUT name="j_password" TYPE="password"> 

<input type="submit" value="登 录" > 

</form> 

form 的action 必须是j_security_check, method="post", 用户名 name="j_username" , 密码name="j_password"  这些都是固定的元素
分享到:
评论

相关推荐

    启用了不安全的http方法漏洞

    在web.xml文件中配置下面一段内容 &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;http-method&gt;PUT&lt;/http-method&gt; &lt;http-method&gt;DELETE&lt;/http-method&gt; &lt;http-method&gt;HEAD&lt;/...

    详解Spring mvc的web.xml配置说明

    除了以上提到的配置,`web.xml`还可以包含错误页面定义、安全配置(如`&lt;security-constraint&gt;`)、本地化支持(`locale-encoding-mapping-list`)等。正确的配置有助于提升应用程序的性能、安全性和可维护性。理解并...

    web.xml详解(web-app_2_3.dtd)

    &lt;description&gt;A simple shopping cart application built using JSP and Servlets.&lt;/description&gt; ``` #### 4. `distributable`元素 `distributable`元素用于指示Web应用程序是否支持分布式部署。如果设置了此元素...

    web.xml详细说明

    `web.xml`是Java Web应用程序中的核心配置文件,用于描述和配置Web应用程序的各种属性、组件及其行为。本文档将详细解析`web.xml`中的各个元素及其功能,帮助开发者更好地理解和使用该文件。 #### 二、常用元素及其...

    Web.xml常用元素

    #### &lt;security-constraint&gt; - **作用**:指定哪些URL路径需要认证和授权,以及所使用的认证方式。 - **示例**: ```xml &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Secure Area&lt;/...

    web.xml详解(txt)

    &lt;description&gt;This is a simple web application for demonstration purposes.&lt;/description&gt; ``` ##### 2.4 `&lt;context-param&gt;` - **定义**:配置上下文参数,这些参数可以在整个Web应用范围内访问。 - **用途**...

    java web项目 web.xml配置详解

    - `&lt;security-constraint&gt;`:定义安全约束,如`&lt;web-resource-collection&gt;`定义受保护的URL集。 - `&lt;login-config&gt;`:`&lt;auth-method&gt;`指定认证方法,如FORM、BASIC等。 四、最佳实践 - 遵循标准:确保配置符合...

    web.xml 详解

    17. `&lt;security-constraint&gt;` - 指定受保护的URL。 18. `&lt;login-config&gt;` - 指定服务器如何对尝试访问受保护页面的用户进行身份验证。 19. `&lt;security-role&gt;` - 列出将在`servlet`元素内的`&lt;security-role-ref&gt;`元素...

    web.xml配置详解

    2. Web 应用描述:&lt;description&gt; 元素用于声明 Web 应用的描述信息,该信息将出现在服务器的管理控制台和 IDE 中。 3. Context 参数:&lt;context-param&gt; 元素用于声明应用范围内的初始化参数,该参数将被所有的 ...

    J2EE中关于web.xml文件的配置

    17. `&lt;security-constraint&gt;`:security-constraint 元素用于制定应该保护的 URL。 18. `&lt;login-config&gt;`:login-config 元素用于指定服务器应该怎样给试图访问受保护页面的用户授权。 19. `&lt;security-role&gt;`:...

    部署描述文件web.xml配置详解.doc

    在Java Web开发中,`web.xml`是一个非常重要的配置文件,它作为Web应用程序的部署描述符,负责管理与应用程序相关的各项配置信息。本文将深入解析`web.xml`的各项元素及其作用,帮助开发者更好地理解并利用这一配置...

    J2EE中关于web.xml文件的配置[文].pdf

    19. **&lt;security-role&gt;**: 声明应用的安全角色,便于在Servlet和Filter中引用。 20. **&lt;env-entry&gt;**: 用于声明Web应用的环境变量,使得应用可以在运行时获取这些值。 21. **&lt;ejb-ref&gt;** 和 **&lt;ejb-local-ref&gt;**: ...

    webService添加basic验证

    2. **添加安全约束**:在`web.xml`中增加`&lt;security-constraint&gt;`元素来定义需要验证的资源。在这个例子中,通过`&lt;url-pattern&gt;`指定所有以`/services/*`开头的URL都必须经过验证。 3. **指定认证方式**:在`&lt;login...

    用web.xml控制Web应用的行为

    - **限制对Web资源的访问**:使用`&lt;security-constraint&gt;`元素定义安全约束,限制URL或资源的访问权限。 - **分配角色名**:`&lt;role-name&gt;`元素用于定义应用的角色,这些角色可以关联到用户的权限。 10. **控制...

    Web.xml配置详解精华

    &lt;/security-constraint&gt; ``` **15. login-config** ```xml &lt;login-config&gt; &lt;auth-method&gt;BASIC&lt;/auth-method&gt; &lt;realm-name&gt;My Realm&lt;/realm-name&gt; &lt;/login-config&gt; ``` **16. security-role** ```xml ...

    web.xml文件详解

    - 示例:`&lt;security-constraint&gt;&lt;web-resource-collection&gt;&lt;web-resource-name&gt;Secure Area&lt;/web-resource-name&gt;&lt;url-pattern&gt;/secure/*&lt;/url-pattern&gt;&lt;/web-resource-collection&gt;&lt;/security-constraint&gt;`。...

    jsp web.xml文件的作用及基本配置.docx

    - 在 `web.xml` 中可以通过 `&lt;welcome-file-list&gt;` 元素来指定一系列欢迎页。 - 当用户访问应用根目录时,服务器会自动寻找这些欢迎页,并按照顺序返回第一个存在的页面。 - 示例代码: ```xml &lt;welcome-file-...

Global site tag (gtag.js) - Google Analytics