I used JAAS and I don't know how to get the information about the user id, username .. should I set the values using a ManagedBean, or the JAAS it self provides it.
JAAS get User information
1.4k views Asked by Houssin Inani At
2
There are 2 answers
1
On
The JAAS provide username and does not provide all information, here a suggestion to recover all infromation with ManagedBean and JSP
With JSF you can get all informations about the user like this :
import org.service.UserFacade;
import org.entity.userEntity;
import java.security.Principal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@SessionScoped
public class MySessionController {
@EJB
private UserFacade userFacade;
String login;
public MySessionController() {
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
// get login from principal
login = principal.getName();
//get all informations of user from EJB : UserFacade
userEntity user = userFacade.findByLogin(login);
}
}
With JSP you can get all informations about the user like this :
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="org.service.UserFacade"%>
<%@page import="org.entity.userEntity"%>
<%@page import="java.security.Principal"%>
<%
String login;
Principal loginUser = request.getUserPrincipal();
// get login from principal
if (loginUser != null) {
login = loginUser.getName()
}
//get all informations of user from EJB : UserFacade
Context c = new InitialContext();
UserFacade userFacade = (UserFacade) c.lookup("java:global/MyProject/MyEjbMoule/UserFacade!org.service.UserFacade");
userEntity user = userFacade.findByLogin(login);
%>
Well to get user information you have to use principals refer
https://nick-lab.gs.washington.edu/java/jdk1.4.1/guide/security/jaas/tutorials/GeneralAcnOnly.html#SampleLMPrincipal
you will also get a sample code there after setting a principal to your subject in your loginmodule you can get the name with the commandloginContext.getSubject().getPrincipals().iterator().next().getName()
associate principal to each information and add it to subject while logging in then you can get the user data with the subject.. if anymore doubts comment