Spring Security:在成功身份验证时在会话中添加其他属性(属性)

vac*_*uum 8 authentication session spring spring-security

简单的问题:在成功身份验证中向HttpSession添加属性(属性)的最佳方法是什么?例如,userID.

现在我在UsernamePasswordAuthenticationFilter中使用我自己的SimpleUrlAuthenticationSuccessHandler实现并按如下方式执行:

public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication auth)
            throws IOException, ServletException {
        PersonBean person = (PersonBean) auth.getPrincipal();
        request.getSession().setAttribute("currentUserId", person .getId().toString());
        super.onAuthenticationSuccess(request, response, auth);
Run Code Online (Sandbox Code Playgroud)

但我不认为这是一种很好的方法,因为有另一种方法可以进行身份​​验证(例如,RememberMe).

那么我需要在这里使用什么?

vac*_*uum 5

答案是在春季论坛上给出的.链接.

通常,需要实现一个ApplicationListener,它监听成功事件并在会话中添加其他属性.

但在我的情况下,它不需要在会话中存储属性.我可以像这里检索userID:

var userId = ${pageContext.request.userPrincipal.principal.id}
Run Code Online (Sandbox Code Playgroud)