问题描述
在使用Maven插件tomcat7-maven-plugin
自动部署web应用到Tomcat时,遇到了401 Unauthorized错误。他已经按照以下三个步骤进行了操作:
1. 在Tomcat的安装目录下的conf\tomcat-users.xml
文件中添加了一个具有部署权限的新用户。
2. 在Maven的conf\settings.xml
文件中的servers
节点中添加了上述Tomcat用户的信息。
3. 在项目的pom.xml
文件中添加了使用tomcat7-maven-plugin
插件的配置。
在第一步中,用户在conf\tomcat-users.xml
文件中添加了以下内容:
<role rolename="manager-gui"/> | |
<role rolename="manager-script"/> | |
<role rolename="manager-jmx"/> | |
<user username="war-deployer" password="some-password" roles="manager-gui,manager-script,manager-jmx" /> |
然后,用户重启了Tomcat。
在第二步中,用户在conf\settings.xml
文件的servers
节点中添加了以下内容:
<server> | |
<id>maven-tomcat-war-deployment-server</id> | |
<username>war-deployer</username> | |
<password>some-password</password> | |
</server> |
最后,在第三步中,用户在项目的pom.xml
文件的plugins
节点中添加了以下内容,以使用tomcat7-maven-plugin
插件:
<plugin> | |
<groupId>org.apache.tomcat.maven</groupId> | |
<artifactId>tomcat7-maven-plugin</artifactId> | |
<version>2.0</version> | |
<configuration> | |
<url>http://localhost:8080/manager/text</url> | |
<path>/my-project-url-path</path> | |
</configuration> | |
</plugin> |
用户以为一切都会顺利进行,但是当执行命令mvn tomcat7:deploy
时,他的web应用没有被部署,Tomcat的webapps
目录中没有任何内容,并且Tomcat返回了以下响应:
[INFO] tomcatManager status code:401, ReasonPhrase:[INFO] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">[INFO] <html>[INFO] <head>[INFO] <title>401 Unauthorized</title>[INFO] <style type="text/css">[INFO] <!--[INFO] BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;font-size:12px;}[INFO] H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}[INFO] PRE, TT {border: 1px dotted #525D76}[INFO] A {color : black;}A.name {color : black;}[INFO] -->[INFO] </style>[INFO] </head>[INFO] <body>[INFO] <h1>401 Unauthorized</h1>[INFO] <p>[INFO] You are not authorized to view this page. If you have not changed[INFO] any configuration files, please examine the file[INFO] <tt>conf/tomcat-users.xml</tt> in your installation. That[INFO] file must contain the credentials to let you use this webapp.[INFO] </p>[INFO] <p>[INFO] For example, to add the <tt>manager-gui</tt> role to a user named[INFO] <tt>tomcat</tt> with a password of <tt>s3cret</tt>, add the following to the[INFO] config file listed above.[INFO] </p>[INFO] <pre>[INFO] <role rolename="manager-gui"/>[INFO] <user username="tomcat" password="s3cret" roles="manager-gui"/>[INFO] </pre>[INFO] <p>[INFO] Note that for Tomcat 7 onwards, the roles required to use the manager[INFO] application were changed from the single <tt>manager</tt> role to the[INFO] following four roles. You will need to assign the role(s) required for[INFO] the functionality you wish to access.[INFO] </p>[INFO] <ul>[INFO] <li><tt>manager-gui</tt> - allows access to the HTML GUI and the status[INFO] pages</li>[INFO] <li><tt>manager-script</tt> - allows access to the text interface and the[INFO] status pages</li>[INFO] <li><tt>manager-jmx</tt> - allows access to the JMX proxy and the status[INFO] pages</li>[INFO] <li><tt>manager-status</tt> - allows access to the status pages only</li>[INFO] </ul>[INFO] <p>[INFO] The HTML interface is protected against CSRF but the text and JMX interfaces[INFO] are not. To maintain the CSRF protection:[INFO] </p>[INFO] <ul>[INFO] <li>Users with the <tt>manager-gui</tt> role should not be granted either[INFO] the <tt>manager-script</tt> or <tt>manager-jmx</tt> roles.</li>[INFO] <li>If the text or jmx interfaces are accessed through a browser (e.g. for[INFO] testing since these interfaces are intended for tools not humans) then[INFO] the browser must be closed afterwards to terminate the session.</li>[INFO] </ul>[INFO] <p>[INFO] For more information - please see the[INFO] <a href="/docs/manager-howto.html" rel="noopener noreferrer">Manager App How-To</a>.[INFO] </p>[INFO] </body>[INFO] </html>
尽管Maven没有报告任何错误,并且最后显示了BUILD SUCCESS
,但用户已经为此问题苦苦挣扎了几个小时,并且在网上搜索了很多,但是找不到解决办法。请帮助用户解决这个问题。非常感谢!
解决方案
请注意以下操作注意版本差异及修改前做好备份。
方案
问题出现的原因是你忘记将插件的<configuration>
与settings.xml
中的<server>
定义关联起来,因此tomcat7-maven-plugin
尝试以未经身份验证的方式进行部署。
你需要在插件的配置部分添加<server>maven-tomcat-war-deployment-server</server>
,如下所示:
<configuration> | |
<server>maven-tomcat-war-deployment-server</server> | |
<url>http://localhost:8080/manager/text</url> | |
<path>/my-project-url-path</path> | |
</configuration> |
这样,插件就会使用settings.xml
中定义的服务器信息进行身份验证,从而解决401 Unauthorized错误。
请注意,确保<server>
的<id>
与pom.xml
中的插件配置中的<server>
一致。
希望这个解决方案能帮助到你。如果还有其他问题,请随时提问。