有效用户的Tomcat安全约束

时间:2009-07-06 23:01:45

标签: java tomcat web-applications security-constraint

我正在尝试保护tomcat中的资源,以便只有“有效用户”(在域中具有有效登录名和密码的用户)才能访问它。它们不一定属于该领域的一个群体。我尝试了许多<security-constraint>指令的组合但没有成功。有任何想法吗?

3 个答案:

答案 0 :(得分:12)

除了auth-constraint之外,您还要添加到security-constraint:

   <auth-constraint>
       <role-name>*</role-name>
   </auth-constraint>

您需要在网络应用中指定安全角色:

    <security-role>
        <role-name>*</role-name>
    </security-role>

答案 1 :(得分:1)

tomcat中有几个领域实现 - 内存,数据库,JAAS等。最简单的配置(尽管不是最安全的)内存,包含一个XML文件,通常在conf / tomcat-users.xml下:

<tomcat-users>
  <user name="tomcat" password="tomcat" roles="tomcat" />
  <user name="role1"  password="tomcat" roles="role1"  />
  <user name="both"   password="tomcat" roles="tomcat,role1" />
</tomcat-users>

领域配置在上下文,主机或引擎配置下,如下所示:

<Realm className="org.apache.catalina.realm.MemoryRealm"
       pathname="conf/tomcat-users.xml" />

然后,在web.xml中输入以下定义:

    <security-constraint>
            <web-resource-collection>
                    <web-resource-name>MRC Customer Care</web-resource-name>
                    <url-pattern>/protected/*</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                    <role-name>role1</role-name>
            </auth-constraint>
    </security-constraint>

    <!-- Define the Login Configuration for this Application -->
    <login-config>
            <auth-method>DIGEST</auth-method>
            <realm-name>YOUR REALM NAME</realm-name>
    </login-config>

    <security-role>
            <description>
              The role that is required to access the application. 
              Should be on from the realm (the tomcat-users.xml file).
            </description>
            <role-name>role1</role-name>                  
    </security-role>

web.xml部分是从我们的一个Web应用程序中获取(稍作更改)。

答案 2 :(得分:0)

如果我们使用Tomcat 8.x,因为提供的server.xml将出现在嵌套的Realm元素中,请在“最外层”Realm元素中添加“allRolesMode =”authOnly“'并更改上述web.xml以进行测试。 e.g。

  <Realm allRolesMode="authOnly" className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
         resources under the key "UserDatabase".  Any edits
         that are performed against this UserDatabase are immediately
         available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase" />
  </Realm>

请阅读org.apache.catalina.realm.RealmBase.java了解详情。

此外,logging.properties中的以下设置非常有用。

org.apache.catalina.realm.level=ALL
org.apache.catalina.realm.useParentHandlers=true
org.apache.catalina.authenticator.level=ALL
org.apache.catalina.authenticator.useParentHandlers=true