tomcat对于web.xml的security-constraint使用的处理机制

本文详细介绍了Tomcat服务器在处理web.xml文件中的<security-constraint>元素时的机制。当<http-method>缺失时,所有HTTP方法都将被禁止访问资源。<auth-constraint>元素需与<login-config>配合,若缺失或为空,将影响权限控制。对于同一个URL模式,若有两个<security-constraint>,一个设置了角色限制,另一个未设置,Tomcat会按AuthenticatorBase类的逻辑处理,只要有不限角色的配置,该URL模式就不会受角色限制,允许任意角色和匿名用户访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.知识点

        web.xml中<security-constraint> 的子元素 <http-method> 是可选的,如果没有 <http-method> 元素,这表示将禁止所有 HTTP 方法访问相应的资源。

        子元素 <auth-constraint> 需要和 <login-config> 相配合使用,但可以被单独使用。如果没有 <auth-constraint> 子元素,这表明任何身份的用户都可以访问相应的资源。也就是说,如果 <security-constraint> 中没有 <auth-constraint> 子元素的话,配置实际上是不起中用的。如果加入了 <auth-constraint> 子元素,但是其内容为空,这表示所有身份的用户都被禁止访问相应的资源。 

 

二.问题

        对于同一个url-pattern,在web.xml出现2个<security-constraint>,一个是对该url-pattern进行了role的限制,一个没有限制role,会如何?

 

三.实践

        一个是对该url-pattern进行了role的限制,即配置auth-constraint,如:

<security-constraint>  
    <web-resource-collection>  
      <web-resource-name>test2</web-resource-name>  
      <url-pattern>/*</url-pattern>  
    </web-resource-collection>  
    <auth-constraint>  
       <role-name>tomcat1</role-name>  
    </auth-constraint>  
</security-constraint>   

        一个是对该url-pattern没有进行role的限制,如:

<security-constraint>  
    <web-resource-collection>  
      <web-resource-name>test3</web-resource-name>  
      <url-pattern>/*</url-pattern>  
    </web-resource-collection>  
</security-constraint>

        对于tomcat而言,在org.apache.catalina.authenticator.AuthenticatorBase认证类中。

/**
     * Enforce the security restrictions in the web application deployment
     * descriptor of our associated Context.
     *
     * @param request Request to be processed
     * @param response Response to be processed
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if thrown by a processing element
     */
    @Override
    public void invoke(Request request, Response response)
        throws IOException, ServletException {
  
  
  ....
  			// Since authenticate modifies the response on failure,
        // we have to check for allow-from-all first.
        //
        boolean authRequired;
        if (constraints == null) {
            authRequired = false;
        } else {//有安全限制
            authRequired = true;
            for(i=0; i < constraints.length && authRequired; i++) {
                if(!constraints[i].getAuthConstraint()) {//如果不需要认证限制
                    authRequired = false;//则不需要认证
                } else if(!constraints[i].getAllRoles()) {//如果不是*,即所有角色的话
                    String [] roles = constraints[i].findAuthRoles();
                    if(roles == null || roles.length == 0) { //只要此url-pattern有一个限制没有控制角色,则满足次url-pattern的url可以被任意角色和匿名用户访问
                        authRequired = false;//则不需要认证
                    }
                }
            }
        }      
         
   ......

 

四.结论

        所以按照上面同时配置同一个url-pattern,不同安全限制,只要有一个不限制角色,则此url-pattern不受角色限制,满足次url-pattern的url可以被任意角色和匿名用户访问。

 

文章来源:http://blog.youkuaiyun.com/yangjun2/article/details/8211492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值