java - 基于表单的身份验证 - 身份验证成功后重定向到错误页面 (Tomcat 7.0.4)

标签 java jakarta-ee authentication servlets forms-authentication

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

最佳答案

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

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");

关于java - 基于表单的身份验证 - 身份验证成功后重定向到错误页面 (Tomcat 7.0.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18984340/

相关文章:

java - 如何以一种形式在两个保管箱中传递两个列表

javascript - 在 JavaScript/jquery 中调用 bean 方法?

c# - ReactJs + Webapi 如何做外部认证?

java - jTable 行数 VS 模型行数

java - 无法读取未命名的模块

Java servlet - 任何限制访问用户名/密码的内置方法?

ios - 如何获取用户指纹生物识别数据?

php - 智威汤逊 : The token could not be parsed from the request

Java - 如何执行一个进程

web-services - 根据GlassFish领域对WS-Security UsernameToken进行身份验证可以得到“Authentication refused”