Search in sources :

Example 6 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class JdbcDaoImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List<UserDetails> users = loadUsersByUsername(username);
    if (users.size() == 0) {
        this.logger.debug("Query returned no results for user '" + username + "'");
        throw new UsernameNotFoundException(this.messages.getMessage("JdbcDaoImpl.notFound", new Object[] { username }, "Username {0} not found"));
    }
    // contains no GrantedAuthority[]
    UserDetails user = users.get(0);
    Set<GrantedAuthority> dbAuthsSet = new HashSet<GrantedAuthority>();
    if (this.enableAuthorities) {
        dbAuthsSet.addAll(loadUserAuthorities(user.getUsername()));
    }
    if (this.enableGroups) {
        dbAuthsSet.addAll(loadGroupAuthorities(user.getUsername()));
    }
    List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>(dbAuthsSet);
    addCustomAuthorities(user.getUsername(), dbAuths);
    if (dbAuths.size() == 0) {
        this.logger.debug("User '" + username + "' has no authorities and will be treated as 'not found'");
        throw new UsernameNotFoundException(this.messages.getMessage("JdbcDaoImpl.noAuthority", new Object[] { username }, "User {0} has no GrantedAuthority"));
    }
    return createUserDetails(username, user, dbAuths);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 7 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class UsernamePasswordAuthenticationTokenDeserializer method deserialize.

/**
	 * This method construct {@link UsernamePasswordAuthenticationToken} object from serialized json.
	 * @param jp the JsonParser
	 * @param ctxt the DeserializationContext
	 * @return the user
	 * @throws IOException if a exception during IO occurs
	 * @throws JsonProcessingException if an error during JSON processing occurs
	 */
@Override
public UsernamePasswordAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    UsernamePasswordAuthenticationToken token = null;
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Boolean authenticated = readJsonNode(jsonNode, "authenticated").asBoolean();
    JsonNode principalNode = readJsonNode(jsonNode, "principal");
    Object principal = null;
    if (principalNode.isObject()) {
        principal = mapper.readValue(principalNode.toString(), new TypeReference<User>() {
        });
    } else {
        principal = principalNode.asText();
    }
    Object credentials = readJsonNode(jsonNode, "credentials").asText();
    List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").toString(), new TypeReference<List<GrantedAuthority>>() {
    });
    if (authenticated) {
        token = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
    } else {
        token = new UsernamePasswordAuthenticationToken(principal, credentials);
    }
    token.setDetails(readJsonNode(jsonNode, "details"));
    return token;
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class JdbcUserDetailsManager method createGroup.

public void createGroup(final String groupName, final List<GrantedAuthority> authorities) {
    Assert.hasText(groupName, "groupName should have text");
    Assert.notNull(authorities, "authorities cannot be null");
    logger.debug("Creating new group '" + groupName + "' with authorities " + AuthorityUtils.authorityListToSet(authorities));
    getJdbcTemplate().update(insertGroupSql, groupName);
    final int groupId = findGroupId(groupName);
    for (GrantedAuthority a : authorities) {
        final String authority = a.getAuthority();
        getJdbcTemplate().update(insertGroupAuthoritySql, new PreparedStatementSetter() {

            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setInt(1, groupId);
                ps.setString(2, authority);
            }
        });
    }
}
Also used : SQLException(java.sql.SQLException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement)

Example 9 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class RoleHierarchyImpl method getReachableGrantedAuthorities.

public Collection<GrantedAuthority> getReachableGrantedAuthorities(Collection<? extends GrantedAuthority> authorities) {
    if (authorities == null || authorities.isEmpty()) {
        return AuthorityUtils.NO_AUTHORITIES;
    }
    Set<GrantedAuthority> reachableRoles = new HashSet<GrantedAuthority>();
    for (GrantedAuthority authority : authorities) {
        addReachableRoles(reachableRoles, authority);
        Set<GrantedAuthority> additionalReachableRoles = getRolesReachableInOneOrMoreSteps(authority);
        if (additionalReachableRoles != null) {
            reachableRoles.addAll(additionalReachableRoles);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("getReachableGrantedAuthorities() - From the roles " + authorities + " one can reach " + reachableRoles + " in zero or more steps.");
    }
    List<GrantedAuthority> reachableRoleList = new ArrayList<GrantedAuthority>(reachableRoles.size());
    reachableRoleList.addAll(reachableRoles);
    return reachableRoleList;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 10 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class RoleHierarchyImpl method buildRolesReachableInOneStepMap.

/**
	 * Parse input and build the map for the roles reachable in one step: the higher role
	 * will become a key that references a set of the reachable lower roles.
	 */
private void buildRolesReachableInOneStepMap() {
    Pattern pattern = Pattern.compile("(\\s*([^\\s>]+)\\s*>\\s*([^\\s>]+))");
    Matcher roleHierarchyMatcher = pattern.matcher(this.roleHierarchyStringRepresentation);
    this.rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
    while (roleHierarchyMatcher.find()) {
        GrantedAuthority higherRole = new SimpleGrantedAuthority(roleHierarchyMatcher.group(2));
        GrantedAuthority lowerRole = new SimpleGrantedAuthority(roleHierarchyMatcher.group(3));
        Set<GrantedAuthority> rolesReachableInOneStepSet;
        if (!this.rolesReachableInOneStepMap.containsKey(higherRole)) {
            rolesReachableInOneStepSet = new HashSet<GrantedAuthority>();
            this.rolesReachableInOneStepMap.put(higherRole, rolesReachableInOneStepSet);
        } else {
            rolesReachableInOneStepSet = this.rolesReachableInOneStepMap.get(higherRole);
        }
        addReachableRoles(rolesReachableInOneStepSet, lowerRole);
        logger.debug("buildRolesReachableInOneStepMap() - From role " + higherRole + " one can reach role " + lowerRole + " in one step.");
    }
}
Also used : Pattern(java.util.regex.Pattern) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Set(java.util.Set) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Aggregations

GrantedAuthority (org.springframework.security.core.GrantedAuthority)158 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)79 Authentication (org.springframework.security.core.Authentication)51 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)33 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)33 HashSet (java.util.HashSet)22 UserDetails (org.springframework.security.core.userdetails.UserDetails)16 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)15 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)11 SecurityContext (org.springframework.security.core.context.SecurityContext)11 User (org.springframework.security.core.userdetails.User)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 MifosUser (org.mifos.security.MifosUser)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 DistinguishedName (org.springframework.ldap.core.DistinguishedName)7 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)7 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)7