0

I'm hoping someone can give me a hint at what's going on here with this exception. I'm putting together an environment where i can test out j_security_check with JSF and what I'm noticing is when i redeploy my application without restarting glassfish i see this error. I thought it would be perfectly safe to just redeploy the war but my setup is hosed until glassfish is restarted and war redeployed. I'm worried that this will becomes an issue later down the road. Any advise?

I'm using Glassfish 3.1.2, NetBeans 7.1.2...

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean userController
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
    at com.sun.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:63)
    at com.sun.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:68)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.OutputLinkRenderer.getValue(OutputLinkRenderer.java:182)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.OutputLinkRenderer.renderAsActive(OutputLinkRenderer.java:191)
    at com.sun.faces.renderkit.html_basic.OutputLinkRenderer.encodeBegin(OutputLinkRenderer.java:107)
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting invoke lifecycle  method private void avenger.UserController.startup()
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:231)
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:223)
    ... 56 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting invoke lifecycle  method private void avenger.UserController.startup()
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokeLifecycleMethod(GlassFishInjectionProvider.java:393)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:306)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:229)
    ... 57 more
Caused by: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
    at java.security.AccessController.doPrivileged(Native Method)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokeLifecycleMethod(GlassFishInjectionProvider.java:376)
    ... 59 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.glassfish.faces.integration.GlassFishInjectionProvider$2.run(GlassFishInjectionProvider.java:382)
    ... 61 more
Caused by: java.lang.ClassCastException: avenger.MyUser cannot be cast to avenger.MyUser
    at avenger.UserController.updateUserList(UserController.java:103)
    at avenger.UserController.startup(UserController.java:39)

Here are portions of my web.xml

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<security-constraint>
    <display-name>Admin</display-name>
    <web-resource-collection>
        <web-resource-name>Admin Views</web-resource-name>
        <description/>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
</security-constraint>
<security-role>
    <description/>
    <role-name>ADMIN</role-name>
</security-role>
<security-role>
    <description/>
    <role-name>USER</role-name>
</security-role>

my management bean

@ManagedBean(eager = true)
@SessionScoped
public class UserController implements Serializable {

    /**
     * Creates a new instance of UserBean
     */
    public UserController() {}

    @PostConstruct
    private void startup() {
        System.out.println("inilitiazing UserController");
        updateUserList();
    }

    @PreDestroy
    private void shutdown() {
        System.out.println("destroying UserController");
        Utils.stopEntityManager();
    }

    private static final long serialVersionUID = 1L;


    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + (this.userList != null ? this.userList.hashCode() : 0);
        hash = 67 * hash + (this.cUser != null ? this.cUser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserController other = (UserController) obj;
        if (this.userList != other.userList && (this.userList == null || !this.userList.equals(other.userList))) {
            return false;
        }
        if (this.cUser != other.cUser && (this.cUser == null || !this.cUser.equals(other.cUser))) {
            return false;
        }
        return true;
    }


    //the current user
    MyUser cUser = new MyUser();
    public MyUser getMyUser() { return cUser; }
    public void setMyUser(MyUser cUser) {this.cUser = cUser;}

    //the new user
    MyUser nUser = new MyUser();
    public MyUser getNewUser() { return nUser; }
    public void setNewUser(MyUser nUser) {this.nUser = nUser;}

    // list of current users
    List userList = new LinkedList();
    public void setUserList(List uList) {userList = uList;}
    public List getUserList() {return userList;}


    public String updateUserList() {

        System.out.println("updateUserList()" );
        EntityManager em = Utils.getEntityManager();

        //Query q = em.createNamedQuery("MyUser.findAll");
        Query q = em.createQuery("Select u FROM MyUser u", MyUser.class);
        userList = q.getResultList();

        for (MyUser cu : userList) {
            String groupList = "";
            for (Grouptable gt : cu.getGrouptableList()) {
                groupList += ":"+gt.getGrouptablePK().getGroupid();
            }
            cu.setGrouplist(groupList);
        }
        return "users.xhtml";
    }

    public boolean usernameExist() {
        return false;
    }

    public String resetNewUser() {
        nUser = new MyUser();
        return "createuser.xhtml";
    }

    public String createNewUser () {

        System.out.println("attempting to create account with\n"
                + "\tusername: " + nUser.getUsername() +"\n"
                + "\tpassword: "+nUser.getPassword() +"\n"
                + "\tfirst name: "+nUser.getFirstName()+"\n"
                + "\tlast name: "+nUser.getLastName()+"\n");

        //TODO: check if newUser already exist if so send message to user


        // set the hashed password
        MessageDigest md = null;
        byte [] digest;
        try { md = MessageDigest.getInstance("MD5"); } 
        catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("nUser"+nUser);
        //System.out.println("cUser.password"+cUser.getPassword());
        digest = md.digest(nUser.getPassword().getBytes());
        nUser.setPassword(Utils.toHex(digest));

        System.out.println("creating user: " + nUser.getUsername() +
                           " password: "+nUser.getPassword());
        EntityManager em = Utils.getEntityManager();
        em.getTransaction().begin();
        em.persist(nUser);
        em.getTransaction().commit();
        updateUserList();

        return "users.xhtml";
    }

    public String deleteUser () {
        System.out.println("deleting user: "+cUser.getUsername());
        EntityManager em = Utils.getEntityManager();
        cUser = em.find(MyUser.class, cUser.getUsername());

        em.getTransaction().begin();
        em.remove(cUser);
        em.getTransaction().commit();
        //em.close();
        return "users.xhtml";
    }

    public String deleteSelectedUsers () {
        for (MyUser cu : userList) {
            if (cu.getSelected()) {
                System.out.println("Planning to remove: "+cu);
            }
        }
        EntityManager em = Utils.getEntityManager();
        for (MyUser cu : userList) {
            if (cu.getUsername().equalsIgnoreCase("ADMIN") && cu.getSelected()) {
                System.out.println("admin account can not be deleted");
                continue;
            }           
            if (cu.getSelected()) {
                MyUser targetUser = em.find(MyUser.class, cu.getUsername());
                System.out.println("removing user: "+cu.toString());
                em.getTransaction().begin();
                em.remove(targetUser);
                em.getTransaction().commit();
            }
        }
        updateUserList();
        return "users.xhtml";
    }

    public String login() {
        System.out.println("user attempting to login...");
        System.out.println("username: "+cUser.getUsername());
        System.out.println("password: "+cUser.getPassword());

        // set the hashed password
        MessageDigest md = null;
        byte [] digest;
        try { md = MessageDigest.getInstance("MD5"); } 
        catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        }
        digest = md.digest(cUser.getPassword().getBytes());
        cUser.setPassword(Utils.toHex(digest));

        MyUser userFound = Utils.getEntityManager().find(MyUser.class, cUser.getUsername());
        if (userFound == null) {
            System.err.println("user does not exist");
            return null;
        }

        if (!userFound.getPassword().equalsIgnoreCase(cUser.getPassword())) {
            System.err.println("Incorrect Password");
            return null;
        }

        return "users.xhtml";

//        FacesContext context = FacesContext.getCurrentInstance();
//        HttpServletRequest request = (HttpServletRequest) context.getExternalContext();
//        try {
//            request.login(this.cUser.getFirstName(), this.cUser.getPassword());
//            this.cUser = Utils.getEntityManager().find(MyUser.class,this.cUser.getUsername());
//        } catch (ServletException e) {
//            context.addMessage(null, new FacesMessage("Unknown login"));
//        }

    }

    public MyUser getUser() {
        if (cUser==null) {
            Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
            if (principal != null) {
                //cUser = userDAO.find(principal.getName()); // Find User by j_username
                cUser = Utils.getEntityManager().find(MyUser.class, principal.getName());
            }
        }
        return cUser;
    }
}

my init.sql for JavaDB

create table usertable (
    username varchar(128) NOT NULL CONSTRAINT USER_PK PRIMARY KEY ,
    password varchar(128) NOT NULL,
    firstname varchar(128) NOT NULL,
    lastname varchar(128) NOT NULL
);

create table grouptable(
    username varchar(128) NOT NULL,
    groupid  varchar(128) NOT NULL,
    CONSTRAINT GROUP_PK PRIMARY KEY(username, groupid),
    CONSTRAINT USER_FK FOREIGN KEY(username) REFERENCES usertable(username)
        ON DELETE CASCADE ON UPDATE RESTRICT
);

insert into usertable(username,password,firstname,lastname) 
    values ('admin', '21232f297a57a5a743894a0e4a801fc3','','');
insert into grouptable(username,groupid) values ('admin', 'USER');
insert into grouptable(username,groupid) values ('admin', 'ADMIN');

i created my connection pool and jdbcRealm using instructions from: http://blog.gamatam.com/2009/11/jdbc-realm-setup-with-glassfish-v3.html

simgineer
  • 1,754
  • 2
  • 22
  • 49
  • I have a hard time in understanding how exactly JAAS is related to this problem. Does this problem disappear of you don't use JAAS? Some code which would replicate the problem for us would be helpful. – BalusC Jun 19 '12 at 13:03
  • Hi BalusC. I've added some more info. I started seeing this issue only after attempting to add the authentication so very likely a pre requisite of setting up j_security_check authentication against a db. You might recognize some of the code from one of your previous solutions, as i'm basically trying to implement your solution proposed in: http://stackoverflow.com/questions/2206911/best-way-for-user-authentication-on-javaee-6-using-jsf-2-0?lq=1 – simgineer Jun 19 '12 at 18:18

0 Answers0