繁体   English   中英

如何使用 JDBCRealm 获取登录用户的用户名?

[英]How can I get the logged-in user's username using JDBCRealm?

我正在运行 Glassfish 3.0,并且正在实现 JDBCRealm 以进行登录身份验证。 用户名和角色保存在名为 usertable 的表中。 身份验证按预期工作。

但是现在的问题是当用户登录时,如何在应用程序中检索登录用户的用户名?

PS:我正在实现 JSF 页面和托管 Bean

在 JSF 中,您可以使用ExternalContext object 检索与请求关联的当前 Principal,因此,当前 session,可以从FacesContext中检索。 可以使用getUserPrincipal()方法从 ExternalContext 访问与请求关联的 Principal:

FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();

上述调用可以在 JSF 托管 bean 中完成。 您可以在 Principal object 上调用getName()方法,以字符串形式检索用户名称。

请注意,如果您在身份验证之前检索 Principal,或者身份验证方案不保护整个站点,则您可以获得引用 Anonymous 用户的Principal实例。

Request.getRemoteUser or Request.getUserPrincipal , independent of the realm you use for authentication, so if you use a File realm for testing and a JDBC realm for production it will work in both cases. 顺便说一句,如果你使用 JDBCRealm 也可以看看FlexibleJDBCRealm

您的身份验证方法应返回包含名称的 java.security.Principal。

http://download.oracle.com/javase/1.4.2/docs/api/java/security/Principal.html

暂无
暂无

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

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