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

时间:2013-09-24 14:26:07

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

我正在尝试基于表单的身份验证,但我无法弄清楚为什么在登录页面上输入正确的用户/密码后,它会将我重定向到错误页面而不是{{ 1}}。

当我输入:

index.jsp

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

WEB.XML:

index.jsp

TOMCAT-USER.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>

3 个答案:

答案 0 :(得分:5)

成功验证和/ secure / *请求之间发生了一些错误。例如,可能是没有为特定HTTP方法配置过滤器,或者servlet抛出异常,在这种情况下可能与auth失败混淆。如果我是你,我会用一个临时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");

答案 1 :(得分:2)

您的auth-constraint元素(而不是security-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>

答案 2 :(得分:2)

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

相关问题