在tomcat中保护Web应用程序

Har*_*sha 2 tomcat java-ee

我有一个在apache tomcat中运行的web应用程序,我使用"j_security_check"来保护这个应用程序.我的代码如下,

login.jsp的

<div id="loginForm">
    <form id="loginfrm" method="post" action="j_security_check">
        <table>
            <tr>
                <td>User Name</td>
                <td><input type="text" id="name" name="j_username" size="20" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" id="phone" name="j_password" size="20" /></td>
            </tr>
            <tr>
                <td></td>
                <td align="right"><input type="submit" value="Login" id="submitButton"></td>
            </tr>
        </table>
    </form>    
</div>
Run Code Online (Sandbox Code Playgroud)

web.xml中

<web-app ...>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>Example Form-Based Authentication Area</realm-name>
        <form-login-config>
            <form-login-page>/success.jsp</form-login-page>
            <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>
</web-app>
Run Code Online (Sandbox Code Playgroud)

错误页面,

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login ERROR!</h1>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

成功页面

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login Success</h1>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在这里,当我提供错误的用户名和错误的密码时,错误页面显示成功,但是当我给出正确的密码和用户名时,

HTTP Status 400 - Invalid direct reference to form login page

type Status report

message Invalid direct reference to form login page

description The request sent by the client was syntactically incorrect (Invalid direct reference to form login page).
Apache Tomcat/7.0.22
Run Code Online (Sandbox Code Playgroud)

请有人能告诉我在哪里错了吗?

Ric*_*mon 5

这条线:

<form-login-page>/success.jsp</form-login-page>
Run Code Online (Sandbox Code Playgroud)

web.xml实际上应该是:

<form-login-page>/login.jsp</form-login-page>
Run Code Online (Sandbox Code Playgroud)

这告诉tomcat无论何时到达受保护的页面,登录表单都会找到login.jsp.缺少的是实际需要进行身份验证的受保护页面的定义:

<security-constraint>
  <web-resource-collection>
    <web-resource-name>
      Entire Application
    </web-resource-name>
    <url-pattern>/success.jsp</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name> 'the name of the group with access' </role-name>
  </auth-constraint>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)

以及它的定义realm,这是您的用户名/密码对(tomcat领域)的存储库.

当您点击这些更改时,http://localhost:8080/succes.jsp您应该被重定向到登录页面,并且一组有效的凭据将被发送到/success.jsp一组错误的凭据/error.jsp.