0

这是我的登录 Servlet 的 post 方法

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String login = request.getParameter("login").trim();
    String password = request.getParameter("password");

    User user = getUsersDao().login(login, DigestUtils.shaHex(password));

    if (user == null) {
        request.setAttribute("login", login);
        request.setAttribute("error", "Wrong username or password.");
        forward(request, response, LOGIN_JSP);
    } else {
        request.getSession().setAttribute(USER_SESSION, user);
        response.sendRedirect(LOGGED_IN_URL);
    }
}

whereLOGGED_IN_URL is "WEB-INF/jsp/index.jsp";
和 index.jsp 存在于这个地址上,这仅在登录后才起作用。用户的 if 条件是好的(我通过将其设置为 false 来检查它)。

为什么会发生?

4

1 回答 1

2

文件夹中的资源/WEB-INF不可公开访问(否则最终用户只需直接打开即可看到敏感信息,例如数据源用户名/密码web.xml)。

您需要将可公开访问的 JSP 文件放在该/WEB-INF文件夹之外。

LOGGED_IN_URL = "/index.jsp";

并重定向如下

response.sendRedirect(request.getContextPath() + LOGGED_IN_URL);

中的资源/WEB-INF仅可用于转发和包含。

于 2013-01-11T14:21:06.023 回答