繁体   English   中英

一个人在Java Servlet中使用j_security_check登录后,如何设置会话变量(用户ID的int值)?

[英]How do I set a session variable(an int for the User's ID) after a person logs in using j_security_check in a Java servlet?

我想将用户的ID存储在会话变量中,以在整个应用程序中使用。 我看不到如何使用默认的安全功能吗? 它对自己进行身份验证,我不知道(如果有)回调函数之类的东西吗? 我们可以用来做家务

任何帮助深表感谢

您可以为此使用Filter ,该Filter检查用户是否通过容器管理的安全性登录(即HttpServletRequest#getUserPrincipal()不返回null ),然后设置会话变量(如果不存在)。

基本上:

if (request.getUserPrincipal() != null && session.getAttribute("user") == null) {
    session.setAttribute("userId", userId);
}

但是,也许您根本不需要这样做,因为它证明您对HttpServletRequest#getUserPrincipal()并不了解。 也就是说,它可能已经完全提供了您需要的信息。 Principal#getName()可以提供登录用户名(可能与您所讨论的用户ID相同Principal#getName()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM