gpt4 book ai didi

jakarta-ee - 保护 tomcat 中的 Web 应用程序

转载 作者:行者123 更新时间:2023-11-28 21:55:20 25 4
gpt4 key购买 nike

我有一个在 apache tomcat 中运行的网络应用程序,我使用“j_security_check”来保护这个应用程序。我的代码如下,

登录.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>

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>

错误页面,

<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>

成功页面

<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>

在这里,当我输入错误的用户名和密码时,错误页面会成功显示,但是当我输入正确的密码和用户名时,它会显示,

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

谁能告诉我哪里错了?

最佳答案

行:

<form-login-page>/success.jsp</form-login-page>

web.xml 中实际上应该是:

<form-login-page>/login.jsp</form-login-page>

这告诉 tomcat 无论何时您到达 protected 页面,登录表单都位于 login.jsp 中。缺少的是实际需要身份验证才能看到的 protected 页面的定义:

<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>

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

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

关于jakarta-ee - 保护 tomcat 中的 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com