我正在尝试设置 Tomcat 来密码保护整个 web 应用程序,但是当我收到密码提示时,它不接受我提供的用户名/密码(只是不断地提示用户/密码),如果我取消,我会收到 401 错误。
知道为什么它不起作用吗?我目前正在使用 MemoryRealm,但也尝试了 UserDatabaseRealm,在 server.xml 中用以下内容替换相应的 MemoryRealm 领域:
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
日志仅显示"GET / HTTP/1.1" 401 954
,并且仅在我取消身份验证提示后才显示。以下是我的配置文件(为简洁起见,删除了注释)
服务器.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.MemoryRealm" />
<Host name="localhost" appBase="/data/www/www.mysite.com/webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
</Engine>
</Service>
</Server>
tomcat-用户.xml:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="generic"/>
<user username="myself" password="mypassword" roles="generic"/>
</tomcat-users>
web.xml(在/data/www/www.mysite.com/webapps/WEB-INF下):
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4">
<display-name>/MySite-RPM_95_ca2f16f912c9dc4aab2a4fcb7c87cb9a1f1bbf04</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>MySite-RPM_95_ca2f16f912c9dc4aab2a4fcb7c87cb9a1f1bbf04</param-value>
</context-param>
<!-- listeners, filters, servlets, welcome-file-list, error-pages, and jsp-config/taglibs snipped for brevity-->
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>generic</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Realm</realm-name>
</login-config>
</web-app>