JAAS获取用户信息

时间:2015-06-28 19:28:08

标签: java-ee jaas

我使用了JAAS而且我不知道如何获取有关用户ID,用户名的信息..我应该使用ManagedBean设置值,还是自己提供的JAAS。

1 个答案:

答案 0 :(得分:-3)

JAAS提供用户名并且不提供所有信息,这里建议使用ManagedBean和JSP恢复所有信息

使用JSF,您可以获得有关用户的所有信息:

    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);
        }
    }

使用JSP,您可以获得有关用户的所有信息:

<%@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);
    %>