Example usage for java.security.acl Group toString

List of usage examples for java.security.acl Group toString

Introduction

In this page you can find the example usage for java.security.acl Group toString.

Prototype

public String toString();

Source Link

Document

Returns a string representation of this principal.

Usage

From source file:de.juwimm.cms.beans.foreign.security.ConQuestDaoAuthenticationProvider.java

/**
 * Attempts to login the user given the Authentication objects principal and credential
 *
 * @param auth The Authentication object to be authenticated.
 *
 * @return The authenticated Authentication object, with it's grantedAuthorities set.
 *
 * @throws AuthenticationException This implementation does not handle 'locked' or 'disabled' accounts. This method
 *         only throws a AuthenticationServiceException, with the message of the LoginException that will be
 *         thrown, should the loginContext.login() method fail.
 *//*from   w w w  . j  a v a 2  s . c o m*/
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    if (auth instanceof UsernamePasswordAuthenticationToken) {
        UsernamePasswordAuthenticationToken request = (UsernamePasswordAuthenticationToken) auth;

        try {
            //Create the LoginContext object, and pass our InternallCallbackHandler
            LoginContext loginContext = new LoginContext(loginContextName, new InternalCallbackHandler(auth));

            //Attempt to login the user, the LoginContext will call our InternalCallbackHandler at this point.
            loginContext.login();

            //create a set to hold the authorities, and add any that have already been applied.
            Set authorities = new HashSet();

            if (request.getAuthorities() != null) {
                authorities.addAll(Arrays.asList(request.getAuthorities()));
            }

            //get the subject principals and pass them to each of the AuthorityGranters
            Set principals = loginContext.getSubject().getPrincipals();

            authorities.add(new JaasGrantedAuthority("*", new AllPrincipal()));

            for (Iterator iterator = principals.iterator(); iterator.hasNext();) {
                Principal principal = (Principal) iterator.next();
                if (principal instanceof Group) {
                    Group g = (Group) principal;
                    if (g.members() != null) {
                        Enumeration members = g.members();
                        while (members.hasMoreElements()) {
                            Principal object = (Principal) members.nextElement();
                            authorities.add(new JaasGrantedAuthority(object.toString(), object));
                        }
                    } else {
                        authorities.add(new JaasGrantedAuthority(g.toString(), g));
                    }
                }
            }

            //Convert the authorities set back to an array and apply it to the token.
            JaasAuthenticationToken result = new JaasAuthenticationToken(request.getPrincipal(),
                    request.getCredentials(),
                    (GrantedAuthority[]) authorities.toArray(new GrantedAuthority[authorities.size()]),
                    loginContext);

            //Publish the success event
            publishSuccessEvent(result);

            //we're done, return the token.
            return result;
        } catch (LoginException loginException) {
            SpringSecurityException ase = loginExceptionResolver.resolveException(loginException);

            publishFailureEvent(request, ase);
            throw ase;
        }
    }

    return null;
}