如何显示登录的用户,JSF 2.0应用程序具有基于表格的安全性
题
我使用自定义领域在Glassfish 3服务器上的JSF 2.0应用程序中配置了基于表单的log。获取有关登录用户的信息的最简单方法是什么,即用户名称。
这可能吗?还是当前的会话只是与安全角色相关联?如果是这样,是否有某种方法可以在不更改配置中的日志的情况下实现这一目标?
简而言之,我想要的是显示一个简单的消息:
登录为用户名
在我的网页上。
解决方案
通过JSF 2.0中的EL(表达式语言)登录的用户最简单的方法是:
#{request.remoteUser}
Tobbe的答案可以很好地从背景豆内获取远程用户。
其他提示
简单(也许不是最好的)答案是:
FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()
我为我弄清楚多久了,我感到震惊。
在您的LoginManagedBean类中,定义了当前使用者,并在此过程中:我没有在系统中使用外部登录,这对我有用。
public Login getCurrentUser() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext externalContext = fc.getExternalContext();
if (externalContext.getUserPrincipal() == null){
logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!current principal is null");
}
else{
Integer id = Integer.parseInt(externalContext.getUserPrincipal().getName());
logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!LOGGED USER "+ id);
try {
currentUser = getLoginService().getLoginById(id);
}
catch (Exception ex) {
}
}
return currentUser;
}
您可以使用的内部面板 #{request.userPrincipal.name}
.
不隶属于 StackOverflow