Spring MVC在登录后在会话中存储用户对象,并从jsp页面检索它

时间:2016-05-31 10:42:21

标签: java spring jsp spring-mvc spring-security

我正在尝试在spring MVC会话中存储登录的用户对象,并从jsp页面检索它。我在扩展CustomAuthenticationSuccessHandlerSimpleUrlAuthenticationSuccessHandler方法中创建了handle,我在会话中存储了简单变量。

@Override
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
                throws IOException {
   String targetUrl ="/dashboard";

   HttpSession session = request.getSession(true);
   session.setAttribute("userEmail", "damith.uwu@gmail.com");

   if (response.isCommitted()) {
      System.out.println("Can't redirect");
      return;
   }
       redirectStrategy.sendRedirect(request, response, targetUrl);
}

在我的jsp页面内

<c:if test="${not empty userEmail}">
    ${userEmail}
</c:if>

但它总是空着。

我的问题是,

  • 这是存储登录用户变量的正确方法还是最佳方式?
  • 有没有其他方法可以完成此操作?

谢谢。

0 个答案:

没有答案
相关问题