web.xml中<security-constraint>安全认证标签说明

1.security-constraint元素

部署描述符中的<security-constraint>元素允许不通过编程就可以限制对某个资源的访问。

 

<security-constraint>
    <display-name>default</display-name>
    <web-resource-collection>
        <web-resource-name>all files</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

 (1) web-resource-collection元素

    web-resource-collection元素标识需要限制访问的资源子集。在web-resource-collection元素中,可以定义URL模式和HTTP方法。如果不存在HTTP方法,就将安全约束应用于所有的方法。

        web-resource-name是与受保护资源相关联的名称。http-method元素可被赋予一个HTTP方法,比如GET和POS。

(2) auth-constraint元素

        auth-constraint元素用于指定可以访问该资源集合的用户角色。如果没有指定auth-constraint元素,就将安全约束应用于所有角色。

        role-name元素包含安全角色的名称。

(3) user-data-constraint元素

        user-data-constraint元素用来显示怎样保护在客户端和Web容器之间传递的数据。

        transport-guarantee元素必须具有如下的某个值:

        ● NONE,这意味着应用不需要传输保证。

        ● INTEGRAL,意味着服务器和客户端之间的数据必须以某种方式发送,而且在传送中不能改变。

        ● CONFIDENTIAL,这意味着传输的数据必须是加密的数据。

       在大多数情况下,安全套接字层(SSL)用于INTEGRAL或CONFIDENTIAL。

2.login-config元素

login-config元素用来指定所使用的验证方法、领域名和表单验证机制所需的特性。

<login-config>

    <auth-method>FORM</auth-method>

    <realm-name>appTest</realm-name>

    <form-login-config>

        <form-login-page>/login.jsp</form-login-page>

        <form-error-page>/login_failed.jsp</form-error-page>

    </form-login-config>

</login-config>

login-config子元素的描述如下:

● auth-method指定验证方法。它的值为下面的一个:BASIC、DIGEST、FORM或 CLIENT-CERT

● realm-name指定HTTP Basic验证中使用的领域名。

●form-login-config指定基于表单的登录中应该使用的登录页面和出错页面。如果没有使用基于表单的验证,则忽略这些元素。这个元素的定义如下,其中form-login-page用于指定显示登录页面的资源路径, form-error-page则用于指定用户登录失败时显示出错页面的资源路径。这两个页面路径都必须以a/开始,并与应用目录相对应。

3.security-role元素

security-role元素指定用于安全约束中的安全角色的声明。

<security-role>

    <description>admin</description>

    <role-name>admin</role-name>

</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-constraint>
		<web-resource-collection>
			<web-resource-name>testxiangmu</web-resource-name>
			<url-pattern>/ssl/*</url-pattern>
		</web-resource-collection>
		<user-data-constraint>
			<transport-guarantee>CONFIDENTIAL</transport-guarantee>
		</user-data-constraint>
	</security-constraint>

网上配置SSL的例子 

http://www.oschina.net/question/12_23148

 

 

猜你喜欢

转载自liuna718-163-com.iteye.com/blog/2217057