【发布时间】:2010-11-08 13:02:48
【问题描述】:
我正在尝试保护 tomcat 中的资源,以便只有“有效用户”(在领域中具有有效登录名和密码的用户)才能访问它。他们不一定属于领域中的一个组。我尝试了许多 <security-constraint> 指令的组合但没有成功。有任何想法吗?
【问题讨论】:
标签: java tomcat web-applications security-constraint
我正在尝试保护 tomcat 中的资源,以便只有“有效用户”(在领域中具有有效登录名和密码的用户)才能访问它。他们不一定属于领域中的一个组。我尝试了许多 <security-constraint> 指令的组合但没有成功。有任何想法吗?
【问题讨论】:
标签: java tomcat web-applications security-constraint
除了要添加到安全约束的身份验证约束:
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
您需要在 web-app 中指定安全角色:
<security-role>
<role-name>*</role-name>
</security-role>
【讨论】:
在 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 部分取自我们的一个网络应用程序(略有改动)。
【讨论】:
<role-name></role-name> 和<role-name>*</role-name> 没有成功。
如果我们使用的是 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
【讨论】: