users@glassfish.java.net

Re: Use Glassfish realm from within Java code

From: <glassfish_at_javadesktop.org>
Date: Mon, 30 Mar 2009 05:54:10 PDT

To add some code, I want the code below to work. I have heard that this code is supposed to work with other servers, and also that it is known that it doesn't work for GlassFish. But what I would really like to know is how to tell the loginContext about the realm.

public static final String ROLES_GROUP_NAME = "Roles";
/** This name maps to the name of the application-policy in the Glassfish configuration */
public static final String POLICY_NAME = "lunaboek";

public boolean authenticate(String username, String password) {
            boolean authenticated = false;
            LoginCallbackHandler handler = new LoginCallbackHandler(username, password);
            
            try {
                    LoginContext ctx = new LoginContext(POLICY_NAME, handler);
                ctx.login();

                authenticated = true;
                        
                subject = ctx.getSubject();
                for (Principal p : subject.getPrincipals()) {
                        if ((p instanceof Group) && (ROLES_GROUP_NAME.equalsIgnoreCase(p.getName()))) {
                                Group g = (Group) p;
                                Enumeration<? extends Principal> members = g.members();
                                while (members.hasMoreElements()) {
                                        Principal member = members.nextElement();
                                        roles.add(member.getName());
                                }
                        }
                }
        } catch (LoginException e) {
                authenticated = false;
        }
        return authenticated;
}
[Message sent by forum member 'lvdpal' (lvdpal)]

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