我很困惑。我已成功设置Spring Security(基于角色的访问等),但我不明白的是如何在Spring创建的会话中添加内容?或者我应该使用另一个会话来存储DTO属性?如果我使用自己的会话,Spring Security会保护它吗?它对我来说根本不透明:
<p id="layoutdims">
<strong>
Logged in as <security:authentication property="principal.username" />
</strong>
<strong>
Logged in as <% HttpSession hs = request.getSession(); %>
<%=hs.getAttribute("principal.username") %>
</strong>
</p>
如果Spring Security使用主会话,为什么hs.getAttribute
给我null?我觉得这很愚蠢,特别是当我做了最复杂的事情时。
答案 0 :(得分:0)
尝试获取会话对象,如下所示
ServletRequestAttributes attr = (ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes();
HttpSession session= attr.getRequest().getSession(true); // true == allow create