Why is Ant returning a 403 on deploy?

2019-04-24 02:14发布

I'm attempting to deploy to a war file in Tomcat 7. It's giving me the following error.

deploy:
   [echo] Deploying on Tomcat.

BUILD FAILED
   C:\Users\coder\workspace\projectName\build.xml:84: java.io.IOException: Server returned    
   HTTP response code: 403 for URL: http://localhost:8090/manager/deploy?path=%2FprojectName

Here's my build file

<project name="ProjectName" default="main"
                basedir=".">

                <!-- Tell ant to use my environment variables -->
                <property environment="env"/>

                <property file="./build.properties"/>
                <property name="username" value="someUsername"/>
                <property name="password" value="somePassword"/>

                <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"/>

                <property name="tomcat.home"
                value="${env.CATALINA_HOME}"/>
                <property name="hibernate.home"
                value="${env.CATALINA_HOME}"/>
                <property name="servlet.jar"
                value="${tomcat.home}/common/lib/servlet-api.jar"/>
                <property name="jsp.jar"
                value="${tomcat.home}/common/lib/jsp-api.jar"/>
                <property name="hibernate.jar" value="C:/hibernate-distribution-3.6.4.Final/hibernate3.jar"/>

                <property name="deploy.dir"
                value="${tomcat.home}/webapps"/>
                <property name="build.compiler" value="modern"/>
                <property name="build.dir" value="build" />
                <property name="src.dir" value="src"/>
                <property name="war.file" value="projectName"/>
                <property name="war.file.name" value="${war.file}.war"/>

                <path id="project.class.path">
                <fileset dir="./WEB-INF/lib/">
                <include name="**/*.jar"/>
                </fileset>
                <pathelement path="${src.dir}"/>
                <pathelement path="${servlet.jar}"/>
                <pathelement path="${jsp.jar}"/>
                <pathelement path="${hibernate.jar}"/>
                </path>

                <target name="clean">
                <delete dir="${build.dir}" includeEmptyDirs="true" />
                </target>

                <target name="prep">
                <mkdir dir="${build.dir}"/>
                </target>

                <target name="compile">
                <javac srcdir="${src.dir}"
                destdir="${build.dir}"
                debug="on"
                deprecation="on">
                <include name="**/*.java"/>
                <classpath refid="project.class.path"/>
                </javac>
                </target>

                <target name="cleanWebApp">
                <delete file="${deploy.dir}/${war.file.name}" />
                <delete dir="${deploy.dir}/${war.file}"
                includeEmptyDirs="true" />
                </target>

                <target name="war">
                <war warfile="${war.file.name}"
                webxml="./WEB-INF/web.xml">
                <fileset dir="./" includes="**/*.*" excludes="*.war,
                **/*.nbattrs, web.xml, **/WEB-INF/**/*.*,
                **/project-files/**/*.*"/>
                <webinf dir="./WEB-INF" includes="**/*"
                excludes="web.xml, **/*.jar, **/*.class"/>
                <lib dir="./WEB-INF/lib"/>
                <classes dir="${build.dir}"/>
                <classes dir="${src.dir}">
                <include name="**/*.properties"/>
                </classes>
                </war>
                </target>

                <target name="deploy">
                    <echo message="Deploying on Tomcat." />
                    <deploy url="http://localhost:8090/manager" username="someUsername"
                     password="somePassword" path="/projectName" war="./${war.file.name}" />
                </target>

                <target name="main" depends="clean, prep, cleanWebApp,
                compile, war, deploy"/>

                </project>

5条回答
我只想做你的唯一
2楼-- · 2019-04-24 02:49

I had the same problem with Tomcat 6, but any solutions above don't help me. So I fixed it by adding additional role ('manager') to user in tomcat-users.

<user username="tomcat" password="tomcat" roles="tomcat,manager-gui,manager"/>
查看更多
Bombasti
3楼-- · 2019-04-24 02:51

The account mentioned in manager.username and manager.password, has to be in the role of "manager-script" (or "admin-script" also if that does't work).

It seems that "manager" and "admin" roles are changed to "manager-gui", "admin-gui", "manager-script" (for text connection), "admin-script" (for text connection) in Tomcat 7.

I found 4 roles relevant to manager from below error page:

403 Access Denied

You are not authorized to view this page.

If you have already configured the Manager application to allow access and you have used your browsers back button, used a saved book-mark or similar then you may have triggered the cross-site request forgery (CSRF) protection that has been enabled for the HTML interface of the Manager application. You will need to reset this protection by returning to the main Manager page. Once you return to this page, you will be able to continue using the Manager appliction's HTML interface normally. If you continue to see this access denied message, check that you have the necessary permissions to access this application.

If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.

For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.

Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.

manager-gui - allows access to the HTML GUI and the status pages

manager-script - allows access to the text interface and the status pages

manager-jmx - allows access to the JMX proxy and the status pages

manager-status - allows access to the status pages only

The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:

Users with the manager-gui role should not be granted either the manager-script or manager-jmx roles. If the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session. For more information - please see the Manager App HOW-TO.

查看更多
Animai°情兽
4楼-- · 2019-04-24 02:52

You're getting an access denied error.

This is possible because either your username or password is incorrect or you haven't added roles correctly.

Here is a blurb from the Tomcat documentation --

Apache Tomcat 6.0 Realm Configuration HOW-TO wrote:If you wish to use the Manager Application to deploy and undeploy applications in a running Tomcat installation, you MUST add the "manager" role to at least one username in your selected Realm implementation. This is because the manager web application itself uses a security constraint that requires role "manager" to access ANY request URI within that application.

Hope that helps.

查看更多
贼婆χ
5楼-- · 2019-04-24 02:58

The base URL you should be using should be: http://localhost:8090/manager/text

查看更多
We Are One
6楼-- · 2019-04-24 03:00

I've recently encountered this error and none of these approaches helped me.

The solution was to explicitly write roles into tomcat-users.xml:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user name="admin" password="admin" roles="admin-gui,admin-script,manager-gui,manager-script"/>

Roles admin-gui and admin-script are not required to run the script, I need them to use this user to admin tomcat in a web gui.

查看更多
登录 后发表回答