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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4400645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:12:50  来源:igfitidea点击:

Configuring a Context specific Tomcat Security Realm

javasecuritytomcat

提问by Andy Mc

I am trying to get a context specific security Realm in Tomcat 6.0, but when I start Tomcat I get the following error:

我正在尝试在 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>

I have created the following context.xml file:

我创建了以下 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>

Created a file: my-users.xml which I have placed under WEB-INF/conf which contains the following:

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

<tomcat-users>
  <role rolename="myrole"/>
  <user username="test" password="changeit" roles="myrole" />
</tomcat-users>

Added the following lines to my web.xml file:

将以下行添加到我的 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>

But seem to get the error wherever I put conf/my-users.xml. Do I have to specify an explicit PATH in the pathname or is it relative to somewhere? Ideally I would like to have it packaged up as part of my WAR file.

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

Any ideas?

有任何想法吗?

回答by stjohnroe

I believe you need the following in your web.xml

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

<security-role>
    <role-name>myrole</role-name>
</security-role>

in order to define the role. Also, I think that you will need to reference the realm in the login-config

以定义角色。另外,我认为您需要在 login-config 中引用领域

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>MyUserDatabase</realm-name>
 </login-config>

or similar.

或类似。

回答by Bernard

Have a look at the tomcat-users.xmlfile in your Tomcat configuration directory that defines security roles as an example of what you need.

查看tomcat-users.xmlTomcat 配置目录中的文件,该文件将安全角色定义为您需要的示例。

Also, refer to the following articlefor guidance.

此外,请参阅以下文章以获取指导。