users@glassfish.java.net

Can´t get Principals from Subject

From: <glassfish_at_javadesktop.org>
Date: Sat, 05 Jun 2010 01:39:10 PDT

Hi,

First of all my code,

public class CustomerBean {
    @EJB
    private AccountController accountController;
    private Long account;
    private Long customerId;
    @EJB
    private TxController txController;
    private MyCBH cbh;
    private MyLoginModule myLM;
    private Subject subject;
        private String user;
        private String password;
        private boolean succeeded;

    public CustomerBean() {
            /*
            customerId = Long.parseLong(
            FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());
        */
    }
    
    @SuppressWarnings("static-access")
        public void logon(ActionEvent ae){
                WorkingDirectory wd = new WorkingDirectory("login.config");
                String pathToResource = wd.get().getAbsolutePath();
                System.setProperty("java.security.auth.login.config", pathToResource);
        cbh = new MyCBH(getUser(), getPassword()); //("200", "javaee")
        myLM = new MyLoginModule(getUser(), getPassword().toCharArray());
        try {
                LoginContext lc = new LoginContext("dukesbankRealm", cbh);
            subject = lc.getSubject();
            myLM.initialize(subject, cbh, null, null);
                myLM.login();
                myLM.commit();
            //LoginContext lc = new LoginContext("dukesbankRealm", cbh);
            //lc.login();
            setSucceeded(true);
        } catch (LoginException e) {
                        // TODO Auto-generated catch block
                setSucceeded(false);
                }
                
                if(isSucceeded()) {
                        Set<Principal> set = subject.getPrincipals();
                        for(Principal p: set){
                                if (p.getName().equals(getUser())){
                                        customerId = Long.parseLong(p.getName());
                                        break;
                                }
                        }
                        
                } else {
                        customerId = null;
                }
        }
        
        public String getUser() {
                return user;
        }

        public void setUser(String user) {
                this.user = user;
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        public void setSucceeded(boolean succeeded) {
                this.succeeded = succeeded;
        }

        public boolean isSucceeded() {
                return succeeded;
        }

        public TxController getTxController() {
        return txController;
    }

    public Long getCustomerId() {
        return customerId;
    }
    
    public void setCustomerId(Long customerId) {
            this.customerId = customerId;
    }

    public void setActiveAccount(Long account) {
        this.account = account;
    }

    public Long getActiveAccount() {
        return this.account;
    }

    public AccountDetails getAccountDetails() {
        AccountDetails ad = null;

        try {
            ad = accountController.getDetails(this.account);
        } catch (InvalidParameterException e) {
            Debug.print(e.getMessage());

            // Not possible
        } catch (AccountNotFoundException e) {
            Debug.print(e.getMessage());

            // Not possible
        }

        if (ad != null) {
            Debug.print(
                "account ID: ",
                ad.getAccountId());
        }

        return ad;
    }

    public List<AccountDetails> getAccounts() {
        List<AccountDetails> accounts = null;

        try {
            accounts = accountController.getAccountsOfCustomer(customerId);
        } catch (InvalidParameterException e) {
            Debug.print(e.getMessage());

            // Not possible
        } catch (CustomerNotFoundException e) {
            Debug.print(e.getMessage());

            // Not possible
        }

        return accounts;
    }

    public Object logout() {
        HttpSession session = (HttpSession) Util.getExternalContext()
                                                .getSession(true);
        session.invalidate();

        return Navigation.main;
    }
}


        public boolean commit() throws LoginException {
                // TODO Auto-generated method stub
                if (username != null && success) {
            Principal user = new MyPrincipal(username);
            subject.getPrincipals().add(user);
            // user1 belongs to mygroup.
            if ("user1".equals(username)) {
                subject.getPrincipals().add(new MyPrincipal("Group: mygroup"));
            }
            return true;
        }
        return true;
        }

The problem is when I instance the Subject class from the LoginContext I get a wrong Subject and I guess it is because 'dukesbankRealm' that is a jdbcRealm is not well read.

This issue is happening in MyLoginModule.commit() and in CustomerBean.logon().

Will you put light on all of this?

Thanks,
Jose
[Message sent by forum member 'josealvarezdelara']

http://forums.java.net/jive/thread.jspa?messageID=472867