2010-09-03 78 views

回答

10

使用authentication tag

这个标签允许访问存储在 安全上下文当前 认证对象。它直接在 JSP中呈现对象的 属性。因此,举例来说,如果认证的主要 属性是Spring Security的 的UserDetails的 对象实例,然后使用 <秒:认证 属性=“principal.username”/>将 呈现当前用户的名称。

当然,它是没有必要使用 JSP标签这种东西和 有些人喜欢保持尽可能少 逻辑尽可能在视图中。您可以 访问在 你的MVC控制器的认证对象(通过调用 SecurityContextHolder.getContext()。getAuthentication()) 和直接的数据添加到您的 模型由视图渲染。

3

我假设你正在使用弹簧安全。后成功登录把UserDetails对象中,像这样的会议(这通常是控制器,其中,如果登录为全成,你会向前)

Object principal = SecurityContextHolder.getContext() 
    .getAuthentication().getPrincipal(); 
HttpSession session = request.getSession(true); //create a new session 

// put the UserDetails object here. 
session.setAttribute("userDetails", principal); 

在你的JSP可以访问UserDetails对象,像这样:

<span class="message">Welcome ${userDetails.username}</span>