How to get a caller principal in my backing bean?
This is my backing bean (deployed to GlassFish):
@ManagedBean
@DeclareRoles({ "USER" })
@RolesAllowed("USER")
public class MyBean {
public MyBean() {}
public final String getId() {
Principal p = .. // how and开发者_开发知识库 where?
return p.getName();
}
}
How and where I can get java.security.Principal
object to understand who is logged in now?
You can obtain it by ExternalContext#getUserPrincipal()
.
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
精彩评论