登录后,Tomcat重定向到错误的URL

时间:2013-05-21 00:22:57

标签: java eclipse authentication tomcat redirect

我的Web应用程序存在此问题,我通过Tomcat执行基于表单的身份验证,并将其重定向到徽标图像文件,而不是重定向到包含该徽标图像的index.html文件...

我要去:

http://localhost:8080/GenTreeUploader/Servlet

然后它给我登录表单并且在成功登录后,我不在那个URL中,但是我被重定向到:

http://localhost:8080/GenTreeUploader/images/gdia_logo.png

我去的时候经过身份验证:

http://localhost:8080/GenTreeUploader/Servlet

然后我被重定向到正确的位置,而不是图像文件。

我正在附加我的web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- WELCOME FILE LIST -->
    <welcome-file-list>
        <welcome-file>/Servlet</welcome-file>
    </welcome-file-list>

    <!-- Security -->

    <security-constraint>

        <web-resource-collection>
            <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>

        <auth-constraint>
            <role-name>tomcat</role-name>
        </auth-constraint>

        <user-data-constraint>
            <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/error.html</form-error-page>
        </form-login-config>
    </login-config>

     <!-- Main Servlet -->
    <servlet>
        <servlet-name>GenTreeUploaderServlet</servlet-name>
        <servlet-class>org.ktu.gdia.presentation.web.GenTreeUploader</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>GenTreeUploaderServlet</servlet-name>
        <url-pattern>/Servlet</url-pattern>
    </servlet-mapping>


    <session-config>
        <session-timeout>
            60
        </session-timeout>
    </session-config>
</web-app>

好?任何想法为什么它不能按预期工作?提前谢谢。

1 个答案:

答案 0 :(得分:5)

重定向实际上正在发生,因为登录文件中请求的那些资源实际上受到保护,因为我选择了整个路径作为安全资源:

<web-resource-collection>
        <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>

所以我创建了一个名为“Admin”的新文件夹,并移动了需要在那里保护的文件,并修改了servlet的路径。这是我当前(固定)web.xml的样子:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- WELCOME FILE LIST -->
    <welcome-file-list>
        <welcome-file>Admin/Servlet</welcome-file>
    </welcome-file-list>

    <!-- Security -->

    <security-constraint>

        <web-resource-collection>
            <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
            <url-pattern>/Admin/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>

        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>

        <user-data-constraint>
            <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/error.html</form-error-page>
        </form-login-config>
    </login-config>

     <!-- Main Servlet -->
    <servlet>
        <servlet-name>GenTreeUploaderServlet</servlet-name>
        <servlet-class>org.ktu.gdia.presentation.web.GenTreeUploader</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>GenTreeUploaderServlet</servlet-name>
        <url-pattern>/Admin/Servlet</url-pattern>
    </servlet-mapping>


    <session-config>
        <session-timeout>
            60
        </session-timeout>
    </session-config>
</web-app>

所以现在Tomcat登录后不会将我重定向到图像或css文件,因为现在没有身份验证的任何人都可以访问它们。