ExternalContext предоставляет информацию о пользователе и роли.
public class RolesAccess implements Serializable {
public String getUserPrincipalName() {
FacesContext context = FacesContext.getCurrentInstance();
Principal principal = context.getExternalContext().getUserPrincipal();
if(principal == null) {
return null;
}
return principal.getName();
}
public String getUser() {
FacesContext context = FacesContext.getCurrentInstance();
return context.getExternalContext().getRemoteUser();
}
public boolean isManager() {
FacesContext context = FacesContext.getCurrentInstance();
return context.getExternalContext().isUserInRole("manager");
}
}
Если вы используете JSF в сервлетах, эта информация сопоставляется со значениями, предоставляемыми HttpServletRequest .
Вы можете использовать управляемые bean-компоненты для предоставления значений представлению через Язык выражений .
<f:view>
<h:outputLabel value="#{rolesBean.userPrincipalName}" />
<h:outputLabel value="#{rolesBean.user}" />
<h:outputLabel value="#{rolesBean.manager}" />
</f:view>