【问题标题】:Question about session management in servlet jsp关于servlet jsp中会话管理的问题
【发布时间】:2014-05-22 08:53:28
【问题描述】:

我目前正在使用 servlet 和 JSP 在 J2EE 中开发一个中级 Web 应用程序。它就像一个内容管理系统。根据我的需要,我的网站的工作方式非常相似,但是对于在 J2EE 中使用 MVC最佳实践和不良实践有一些问题 .

用户登录应用代码为:

<c:choose>
    <c:when test="${not empty sessionScope.admin}">
    <a href="/context/controller?action=add-content"> + Add a Content</a>
    </c:when>
    <c:otherwise>
    <a href="/context/controller?action=log-in"> Admin Login</a>
    </c:otherwise>
</c:choose>

控制器 servlet 中的 Java 代码为:

if (userDAO.isUser(request.getParameter("uname"), request.getParameter("upass"))) {             
    request.getSession().setAttribute("admin", request.getParameter("uname"));  
    request.getRequestDispatcher("/admin.jsp").forward(request, response);
} else {
    request.getSession().setAttribute("admin", "");
    request.getRequestDispatcher("/content.jsp").forward(request, response);
}

注销:

<a href="/context/controller?action=log-out">Logout</a>

控制器 servlet 中的 Java 代码为:

if (action != null && action.equals("log-out")) {
        HttpSession session = request.getSession(false);
        if(session != null){
            session.invalidate();
        }
        request.getRequestDispatcher("index.jsp").forward(
                request, response);
    }

我想知道上面的登录、注销和会话管理逻辑是否正确?

而且我也无法阻止用户在退出后返回安全页面,我该如何设置?

【问题讨论】:

  • 避免返回不是一种选择。所有浏览器都允许这样做,或者用户可以重新输入 URL 或通过搜索历史记录。您必须考虑用户将始终返回或使用历史记录访​​问直接页面。因此,请在您的控制器中为此做好准备,并将用户发送到正确的位置。用户万岁!

标签: jakarta-ee session jsp


【解决方案1】:

我可以立即看到的一个明显错误是,每次不被识别为普通用户的登录尝试都允许以管理员身份登录。
我严重怀疑这就是您想要的,您希望那些登录被拒绝并显示错误...
而且您可能希望设置您的系统,以便管理员也是普通用户。

所以你会得到类似的东西:
if (login == success) then if (login == admin) then login_as_admin else login_as_user else deny_login_with_error

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多