【问题标题】:Form Based Authentication - Redirect to error page after successful authentication (Tomcat 7.0.4)基于表单的身份验证 - 身份验证成功后重定向到错误页面 (Tomcat 7.0.4)
【发布时间】:2013-09-24 14:26:06
【问题描述】:

我正在尝试基于表单的身份验证,但我不明白为什么在登录页面上输入正确的用户/密码后,它会将我重定向到错误页面而不是 index.jsp

当我输入时:

http://localhost:8080/<context>/secure/index.jsp

我得到了登录页面。但是,当我输入用户/密码(经理/经理)时,它会将我带到error.html 而不是index.jsp

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>FormBasedAuthentication</display-name>
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.html</form-login-page>
        <form-error-page>/error.html</form-error-page>
    </form-login-config>
</login-config>
<security-role>
    <role-name>role1</role-name>
</security-role>    
<security-constraint>
    <web-resource-collection>
        <web-resource-name>SecurePages</web-resource-name>
        <description>Security constraint for JSP resources</description>
        <url-pattern>/secure/*</url-pattern>
        <http-method>POST</http-method>
        <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>role1</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

TOMCAT-USER.XML:

<tomcat-users>
   <role rolename="manager-gui"/>
   <role rolename="manager-script"/>
   <role rolename="manager-status"/>
   <role rolename="manager-jmx"/>
   <role rolename="role1"/>
   <user username="manager" password="manager" roles="role1"/>
</tomcat-users>

【问题讨论】:

  • 你的应用服务器是什么?

标签: java jakarta-ee authentication servlets forms-authentication


【解决方案1】:

在成功的身份验证和对 /secure/* 的请求之间发生了一些错误。例如,可能没有为特定的 HTTP 方法配置过滤器,或者 servlet 抛出异常,在这种情况下可能与身份验证失败相混淆。如果我是你,我会将 error.html 替换为临时 servlet(或 jsp:error.jsp),以仔细检查请求以获取有关失败原因的详细信息。 应该检查这些请求属性:

Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
String exceptionType = (String) request.getAttribute("javax.servlet.error.exception_type");
String errorMsg = (String) request.getAttribute("javax.servlet.error.message");

【讨论】:

    【解决方案2】:

    您的security-constraint 元素中必须有一个auth-constraint(而不是user-data-constraint)。使用auth-constraint,您可以在应用程序中指定用户或角色(在 JAAS 词汇表中命名为 Principal,在 web.xml 词汇表中命名为 Role),可以访问指定的web-resource-collection。此用户或角色必须在 security-role tag 内的 web.xml 中定义。

    最终的 web.xml 内容应该是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID"
             version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>FormBasedAuthentication</display-name>
        <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>
        <security-constraint>
            <web-resource-collection>
                <web-resource-name>SecurePages</web-resource-name>
                <description>Security constraint for resources in the secure directory</description>
                <url-pattern>/secure/*</url-pattern>
                <http-method>POST</http-method>
                <http-method>GET</http-method>
            </web-resource-collection>
            <auth-constraint>
                <role-name>admin</role-name>
            </auth-constraint>
        </security-constraint>
        <security-role>
            <role-name>admin</role-name>
        </security-role>
    </web-app>
    

    【讨论】:

    • 请看我的修改问题。我已经做到了,它现在正在工作。但是在成功认证后,tomcat 将请求重定向到 error.html 而不是 index.jsp。有什么想法吗?
    【解决方案3】:

    Tomcat 配置中的服务器位置应该是“使用 Tomcat 安装”而不是“使用工作区元数据”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多