我正在使用基于表单的身份验证(JBOSS/JAAS),但我的表单正在将数据发送到我的 Servlet,因此我可以在尝试登录之前执行一些检查。
现在我需要转发到 j_security_check 但我尝试的方法不起作用(404 错误)...
如何重定向/转发到 j_security_check(请注意应用程序通过 https / sssl 运行)?
我可以通过重定向使其工作,并且参数进入 URL,但这不安全(因为用户/通行证保留在浏览器历史记录中,等等)...
有任何想法吗?
我正在使用基于表单的身份验证(JBOSS/JAAS),但我的表单正在将数据发送到我的 Servlet,因此我可以在尝试登录之前执行一些检查。
现在我需要转发到 j_security_check 但我尝试的方法不起作用(404 错误)...
如何重定向/转发到 j_security_check(请注意应用程序通过 https / sssl 运行)?
我可以通过重定向使其工作,并且参数进入 URL,但这不安全(因为用户/通行证保留在浏览器历史记录中,等等)...
有任何想法吗?
j_security_check
Tomcat 仅在之前为该会话启动登录过程时才接受请求。所以首先你需要尝试访问一个安全约束资源。然后 Tomcat 会将您重定向到登录表单。只有这样你才能访问j_security_check
。
我看到这是一个旧帖子,但我想分享一个答案。
如果原始请求是 POST,您可以在进行一些处理后简单地转发到 j_security_check。
...
request.getParameterMap().put( "j_username", new String[]{ userId } );
request.getParameterMap().put( "j_password", new String[]{ password } );
try
{
request.getRequestDispatcher("/j_security_check").forward( request , response );
return null;
}
catch (ServletException e)
{
logger.error( e );
}
catch (IOException e)
{
logger.error( e );
}
我知道这是一个太老的问题,但我遇到了同样的问题,我发现解决它的独特方法是使用这样的东西:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String user = req.getParameter("j_username");
String pass = req.getParameter("j_password");
resp.sendRedirect("/Project-web/j_security_check?j_username=" + user + "&j_password=" + pass);
}
由于我刚刚遇到同样的问题,而其他答案并没有证明对我有用,我想分享我自己的解决方案:
1) 创建一个新的 JSP,比如 loginForward.jsp,仅用于转发目的:
<%@ page language="java" %>
<%
final String username = request.getParameter("j_username");
final String password = request.getParameter("j_password");
%>
<body onload="document.getElementById('creds').submit()">
<form name="creds" id='creds' method="POST" action="j_security_check">
<input type="hidden" name="j_username" value="<%= username %>">
<input type="hidden" name="j_password" value="<%= password %>">
</form>
</body>
2) 在您的 servlet 中,转发到该 JSP:
final String redirectURL = String.format("loginForward.jsp j_username=%s&j_password=%s", username, password);
RequestDispatcher requestDispatcher = request.getRequestDispatcher(redirectURL);
requestDispatcher.forward(request, response);
return;