【问题标题】:Tomcat security constraint for valid user有效用户的 Tomcat 安全约束
【发布时间】:2010-11-08 13:02:48
【问题描述】:

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

【问题讨论】:

    标签: java tomcat web-applications security-constraint


    【解决方案1】:

    除了要添加到安全约束的身份验证约束:

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

    您需要在 web-app 中指定安全角色:

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

    【讨论】:

    • 我要试试这个并报告。
    • 添加安全角色标签是我们的解决方案。谢谢。
    • 有什么方法可以在 web.xml 之外实际添加约束?例如。在 context.xml 中为除了 Realm/Valve 之外的所有 Web 应用添加约束?
    • 上面的答案是$CATALINA_BASE/conf/web.xml。见stackoverflow.com/questions/18242619/…
    【解决方案2】:

    在 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 部分取自我们的一个网络应用程序(略有改动)。

    【讨论】:

    • 在我的特定环境中,我使用 JNDIRealm 连接到 ldap。问题是我不能在 ldap 中包含组,并且必须仅基于用户名和密码进行身份验证,而无需任何角色。我试过使用&lt;role-name&gt;&lt;/role-name&gt;&lt;role-name&gt;*&lt;/role-name&gt; 没有成功。
    【解决方案3】:

    如果我们使用的是 Tomcat 8.x,由于提供的 server.xml 将包含在嵌套的 Realm 元素中,请在“outmost” Realm 元素中添加 'allRolesMode="authOnly"' 并更改上述 web.xml 以进行测试. 例如

      <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-23
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2014-09-10
      • 2014-11-20
      • 1970-01-01
      相关资源
      最近更新 更多