Example usage for org.springframework.security.core GrantedAuthority toString

List of usage examples for org.springframework.security.core GrantedAuthority toString

Introduction

In this page you can find the example usage for org.springframework.security.core GrantedAuthority toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.cloudfoundry.identity.uaa.user.UaaUser.java

public UaaUser authorities(Collection<? extends GrantedAuthority> authorities) {
    ArrayList<GrantedAuthority> values = new ArrayList<GrantedAuthority>(authorities);
    for (int i = 0; i < values.size(); i++) {
        GrantedAuthority authority = values.get(i);
        values.set(i, UaaAuthority.authority(authority.toString()));
    }/*from   w  w  w. jav  a2s. c  o m*/
    if (!values.contains(UaaAuthority.UAA_USER)) {
        values.add(UaaAuthority.UAA_USER);
    }
    UaaUser user = new UaaUser(id, username, password, email, values, givenName, familyName, created, modified);
    return user;
}

From source file:org.cloudfoundry.identity.uaa.scim.bootstrap.ScimUserBootstrap.java

/**
 * Convert authorities to group names./*from   w  w  w  . j a  v  a 2 s. co  m*/
 */
private Collection<String> convertToGroups(List<? extends GrantedAuthority> authorities) {
    List<String> groups = new ArrayList<String>();
    for (GrantedAuthority authority : authorities) {
        groups.add(authority.toString());
    }
    return groups;
}

From source file:com.browsexml.ldap.LdapUserDetails.java

public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append(super.toString()).append(": ");
    sb.append("Username: ").append(this.username).append("; ");
    sb.append("Password: [PROTECTED]; ");
    sb.append("Enabled: ").append(this.enabled).append("; ");
    sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
    sb.append("credentialsNonExpired: ").append(this.credentialsNonExpired).append("; ");
    sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
    sb.append("Employee Id: ").append(this.employeeId).append("; ");
    sb.append("Mail: ").append(this.mail).append("; ");
    sb.append("Job: ").append(this.job).append("; ");

    if (this.getAuthorities() != null) {
        sb.append("Granted Authorities: ");
        boolean first = true;
        for (GrantedAuthority authority : this.getAuthorities()) {
            if (!first) {
                sb.append(", ");
            }//from  w w w. ja  v  a 2 s .c  o m
            first = false;
            sb.append(authority.toString());
        }
    } else {
        sb.append("Not granted any authorities");
    }

    return sb.toString();
}

From source file:nu.localhost.tapestry5.springsecurity.components.IfRole.java

private Collection<GrantedAuthority> authoritiesToRoles(Collection<GrantedAuthority> c) {
    Collection<GrantedAuthority> target = new ArrayList<GrantedAuthority>();

    for (final GrantedAuthority authority : c) {

        if (null == authority.getAuthority()) {
            throw new IllegalArgumentException(
                    "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process "
                            + authority.toString());
        }//  w w  w .  j  a  v a 2 s .c om

        target.add(authority);
    }

    return target;
}

From source file:ch.astina.hesperid.web.components.security.IfRole.java

@SuppressWarnings("unchecked")
private Set authoritiesToRoles(Collection c) {
    Set target = new HashSet();

    for (Iterator iterator = c.iterator(); iterator.hasNext();) {
        GrantedAuthority authority = (GrantedAuthority) iterator.next();

        if (null == authority.getAuthority()) {
            throw new IllegalArgumentException(
                    "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process "
                            + authority.toString());
        }/*w w  w .j  a v a2  s . c  o  m*/

        target.add(authority.getAuthority());
    }

    return target;
}

From source file:org.opennms.protocols.radius.springsecurity.RadiusAuthenticationProvider.java

/** {@inheritDoc} */
@Override/*from  w w w .ja v a 2  s .  co m*/
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken token)
        throws AuthenticationException {
    if (!StringUtils.hasLength(username)) {
        logger.info("Authentication attempted with empty username");
        throw new BadCredentialsException(
                messages.getMessage("RadiusAuthenticationProvider.emptyUsername", "Username cannot be empty"));
    }
    String password = (String) token.getCredentials();
    if (!StringUtils.hasLength(password)) {
        logger.info("Authentication attempted with empty password");
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
    }

    InetAddress serverIP = null;
    serverIP = InetAddressUtils.addr(server);
    if (serverIP == null) {
        logger.error("Could not resolve radius server address " + server);
        throw new AuthenticationServiceException(messages.getMessage(
                "RadiusAuthenticationProvider.unknownServer", "Could not resolve radius server address"));
    }
    AttributeFactory.loadAttributeDictionary("net.jradius.dictionary.AttributeDictionaryImpl");
    AttributeList attributeList = new AttributeList();
    attributeList.add(new Attr_UserName(username));
    attributeList.add(new Attr_UserPassword(password));
    RadiusPacket reply;
    try {
        RadiusClient radiusClient = new RadiusClient(serverIP, secret, port, port + 1, timeout);
        AccessRequest request = new AccessRequest(radiusClient, attributeList);

        logger.debug("Sending AccessRequest message to " + InetAddressUtils.str(serverIP) + ":" + port
                + " using " + (authTypeClass == null ? "PAP" : authTypeClass.getAuthName())
                + " protocol with timeout = " + timeout + ", retries = " + retries + ", attributes:\n"
                + attributeList.toString());
        reply = radiusClient.authenticate(request, authTypeClass, retries);
    } catch (RadiusException e) {
        logger.error("Error connecting to radius server " + server + " : " + e);
        throw new AuthenticationServiceException(messages.getMessage("RadiusAuthenticationProvider.radiusError",
                new Object[] { e }, "Error connecting to radius server: " + e));
    } catch (IOException e) {
        logger.error("Error connecting to radius server " + server + " : " + e);
        throw new AuthenticationServiceException(messages.getMessage("RadiusAuthenticationProvider.radiusError",
                new Object[] { e }, "Error connecting to radius server: " + e));
    }
    if (reply == null) {
        logger.error("Timed out connecting to radius server " + server);
        throw new AuthenticationServiceException(messages.getMessage(
                "RadiusAuthenticationProvider.radiusTimeout", "Timed out connecting to radius server"));
    }
    if (!(reply instanceof AccessAccept)) {
        logger.info("Received a reply other than AccessAccept from radius server " + server + " for user "
                + username + " :\n" + reply.toString());
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
    }
    logger.debug("Received AccessAccept message from " + InetAddressUtils.str(serverIP) + ":" + port
            + " for user " + username + " with attributes:\n" + reply.getAttributes().toString());

    String roles = null;
    if (!StringUtils.hasLength(rolesAttribute)) {
        logger.debug("rolesAttribute not set, using default roles (" + defaultRoles + ") for user " + username);
        roles = new String(defaultRoles);
    } else {
        Iterator<RadiusAttribute> attributes = reply.getAttributes().getAttributeList().iterator();
        while (attributes.hasNext()) {
            RadiusAttribute attribute = attributes.next();
            if (rolesAttribute.equals(attribute.getAttributeName())) {
                roles = new String(attribute.getValue().getBytes());
                break;
            }
        }
        if (roles == null) {
            logger.info("Radius attribute " + rolesAttribute + " not found, using default roles ("
                    + defaultRoles + ") for user " + username);
            roles = new String(defaultRoles);
        }
    }

    String[] rolesArray = roles.replaceAll("\\s*", "").split(",");
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(rolesArray.length);
    for (String role : rolesArray) {
        authorities.add(new SimpleGrantedAuthority(role));
    }
    if (logger.isDebugEnabled()) {
        StringBuffer readRoles = new StringBuffer();
        for (GrantedAuthority authority : authorities) {
            readRoles.append(authority.toString() + ", ");
        }
        if (readRoles.length() > 0) {
            readRoles.delete(readRoles.length() - 2, readRoles.length());
        }
        logger.debug("Parsed roles " + readRoles + " for user " + username);
    }

    return new User(username, password, true, true, true, true, authorities);
}