配置特定于上下文的Tomcat安全领域

And*_* Mc 7 java security tomcat

我试图在Tomcat 6.0中获取特定于上下文的安全领域,但是当我启动Tomcat时,我收到以下错误:

09-Dec-2010 16:12:40 org.apache.catalina.startup.ContextConfig validateSecurityRoles
INFO: WARNING: Security role name myrole used in an <auth-constraint> without being defined in a <security-role>
Run Code Online (Sandbox Code Playgroud)

我创建了以下context.xml文件:

<Context debug="0" reloadable="true">

  <Resource name="MyUserDatabase"
            type="org.apache.catalina.UserDatabase"
            description="User database that can be updated and saved"
            factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
            pathname="conf/my-users.xml" />

  <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
         resourceName="MyUserDatabase"/>

</Context>
Run Code Online (Sandbox Code Playgroud)

创建了一个文件:my-users.xml,它放在WEB-INF/conf下,其中包含以下内容:

<tomcat-users>
  <role rolename="myrole"/>
  <user username="test" password="changeit" roles="myrole" />
</tomcat-users>
Run Code Online (Sandbox Code Playgroud)

在我的web.xml文件中添加了以下行:

<web-app ...>
  ...
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Entire Application</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection> 
    <auth-constraint>
      <role-name>myrole</role-name>
    </auth-constraint>
  </security-constraint>

  <login-config>
    <auth-method>BASIC</auth-method>
  </login-config>
  ...
</web-app>
Run Code Online (Sandbox Code Playgroud)

但无论我把conf/my-users.xml放在哪里,似乎都会出错.我是否必须在路径名中指定显式PATH或者它是否相对于某个地方?理想情况下,我希望将其打包为WAR文件的一部分.

有任何想法吗?

stj*_*roe 4

我相信您的 web.xml 中需要以下内容

<security-role>
    <role-name>myrole</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)

以便定义角色。另外,我认为您需要在登录配置中引用领域

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>MyUserDatabase</realm-name>
 </login-config>
Run Code Online (Sandbox Code Playgroud)

或类似的。