Search in sources :

Example 1 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project head by mifos.

the class PersonnelDaoHibernate method findAuthenticatedUserByUsername.

@SuppressWarnings("unchecked")
@Override
public MifosUser findAuthenticatedUserByUsername(String username) {
    PersonnelBO user = findPersonnelByUsername(username);
    if (user == null) {
        return null;
    }
    Set<Short> activityIds = new HashSet<Short>();
    Set<Short> roleIds = new HashSet<Short>();
    for (PersonnelRoleEntity personnelRole : user.getPersonnelRoles()) {
        RoleBO role = personnelRole.getRole();
        roleIds.add(role.getId());
        activityIds.addAll(role.getActivityIds());
    }
    byte[] password = user.getEncryptedPassword();
    boolean enabled = user.isActive();
    boolean accountNonExpired = !user.isLocked();
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = !user.isLocked();
    List<GrantedAuthority> authorities = getGrantedActivityAuthorities(activityIds);
    return new MifosUser(user.getPersonnelId(), user.getOffice().getOfficeId(), user.getLevelEnum().getValue(), new ArrayList<Short>(roleIds), username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, user.getPreferredLocale());
}
Also used : PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) PersonnelRoleEntity(org.mifos.customers.personnel.business.PersonnelRoleEntity) GrantedAuthority(org.springframework.security.core.GrantedAuthority) MifosUser(org.mifos.security.MifosUser) HashSet(java.util.HashSet) RoleBO(org.mifos.security.rolesandpermission.business.RoleBO)

Example 2 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project head by mifos.

the class RepayLoanActionStrutsTest method setMifosUserFromContext.

private void setMifosUserFromContext() {
    SecurityContext securityContext = new SecurityContextImpl();
    MifosUser principal = new MifosUser(userContext.getId(), userContext.getBranchId(), userContext.getLevelId(), new ArrayList<Short>(userContext.getRoles()), userContext.getName(), "".getBytes(), true, true, true, true, new ArrayList<GrantedAuthority>(), userContext.getLocaleId());
    Authentication authentication = new TestingAuthenticationToken(principal, principal);
    securityContext.setAuthentication(authentication);
    SecurityContextHolder.setContext(securityContext);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) MifosUser(org.mifos.security.MifosUser) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken)

Example 3 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project head by mifos.

the class CustomerNotesActionStrutsTest method setMifosUserFromContext.

private void setMifosUserFromContext() {
    SecurityContext securityContext = new SecurityContextImpl();
    MifosUser principal = new MifosUser(userContext.getId(), userContext.getBranchId(), userContext.getLevelId(), new ArrayList<Short>(userContext.getRoles()), userContext.getName(), "".getBytes(), true, true, true, true, new ArrayList<GrantedAuthority>(), userContext.getLocaleId());
    Authentication authentication = new TestingAuthenticationToken(principal, principal);
    securityContext.setAuthentication(authentication);
    SecurityContextHolder.setContext(securityContext);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) MifosUser(org.mifos.security.MifosUser) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken)

Example 4 with GrantedAuthority

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

the class UserInfoTokenServices method extractAuthentication.

private OAuth2Authentication extractAuthentication(Map<String, Object> map) {
    Object principal = getPrincipal(map);
    List<GrantedAuthority> authorities = this.authoritiesExtractor.extractAuthorities(map);
    OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
    token.setDetails(map);
    return new OAuth2Authentication(request, token);
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) GrantedAuthority(org.springframework.security.core.GrantedAuthority) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 5 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project camel by apache.

the class SpringSecurityAuthorizationPolicyTest method createAuthenticationToken.

private Authentication createAuthenticationToken(String username, String password, String... roles) {
    Authentication authToken;
    if (roles != null && roles.length > 0) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(roles.length);
        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        authToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
    } else {
        authToken = new UsernamePasswordAuthenticationToken(username, password);
    }
    return authToken;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

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