Search in sources :

Example 31 with SimpleGrantedAuthority

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

the class DefaultLdapAuthoritiesPopulator method getGroupMembershipRoles.

public Set<GrantedAuthority> getGroupMembershipRoles(String userDn, String username) {
    if (getGroupSearchBase() == null) {
        return new HashSet<GrantedAuthority>();
    }
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for roles for user '" + username + "', DN = " + "'" + userDn + "', with filter " + this.groupSearchFilter + " in search base '" + getGroupSearchBase() + "'");
    }
    Set<String> userRoles = getLdapTemplate().searchForSingleAttributeValues(getGroupSearchBase(), this.groupSearchFilter, new String[] { userDn, username }, this.groupRoleAttribute);
    if (logger.isDebugEnabled()) {
        logger.debug("Roles from search: " + userRoles);
    }
    for (String role : userRoles) {
        if (this.convertToUpperCase) {
            role = role.toUpperCase();
        }
        authorities.add(new SimpleGrantedAuthority(this.rolePrefix + role));
    }
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HashSet(java.util.HashSet)

Example 32 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority in project SI2016_TIM6 by SoftverInzenjeringETFSA.

the class TokenAuthenticationService method getAuthentication.

public static Authentication getAuthentication(HttpServletRequest request) {
    ServletContext servletContext = request.getServletContext();
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    studentRepository = webApplicationContext.getBean(StudentRepository.class);
    String token = request.getHeader(HEADER_STRING);
    if (token != null) {
        // parse the token.
        String user = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody().getSubject();
        Student student = studentRepository.findStudentByUsername(user);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (student != null) {
            authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT"));
        }
        return user != null ? new UsernamePasswordAuthenticationToken(user, null, authorities) : null;
    }
    return null;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) StudentRepository(ba.isss.repositories.StudentRepository) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Student(ba.isss.models.Student) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 33 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority in project OpenClinica by OpenClinica.

the class OpenClinicaLdapAuthoritiesPopulator method getGrantedAuthorities.

public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
    Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>(1);
    auths.add(new SimpleGrantedAuthority("ROLE_USER"));
    return auths;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 34 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority in project Activiti by Activiti.

the class BasicAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    boolean authenticated = identityService.checkPassword(name, password);
    if (authenticated) {
        List<Group> groups = identityService.createGroupQuery().groupMember(name).list();
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        for (Group group : groups) {
            grantedAuthorities.add(new SimpleGrantedAuthority(group.getId()));
        }
        identityService.setAuthenticatedUserId(name);
        return new UsernamePasswordAuthenticationToken(name, password, grantedAuthorities);
    } else {
        throw new BadCredentialsException("Authentication failed for this username and password");
    }
}
Also used : Group(org.activiti.engine.identity.Group) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 35 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority in project ORCID-Source by ORCID.

the class StatsApiServiceBaseImplTest method init.

@Before
public void init() {
    // create our mock data
    List<StatisticValuesEntity> statsTimelineValues = new ArrayList<StatisticValuesEntity>();
    List<StatisticValuesEntity> statsSummaryValues = new ArrayList<StatisticValuesEntity>();
    StatisticValuesEntity a = new StatisticValuesEntity();
    a.setId(1l);
    a.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
    a.setStatisticValue(100l);
    StatisticKeyEntity akey = new StatisticKeyEntity();
    akey.setGenerationDate(new Date(2000, 1, 1));
    akey.setId(200L);
    a.setKey(akey);
    StatisticValuesEntity b = new StatisticValuesEntity();
    b.setId(1l);
    b.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
    b.setStatisticValue(101l);
    StatisticKeyEntity bkey = new StatisticKeyEntity();
    bkey.setGenerationDate(new Date(1999, 1, 1));
    bkey.setId(201L);
    b.setKey(bkey);
    StatisticValuesEntity c = new StatisticValuesEntity();
    c.setId(1l);
    c.setStatisticName(StatisticsEnum.KEY_NUMBER_OF_WORKS.value());
    c.setStatisticValue(102l);
    c.setKey(akey);
    statsTimelineValues.add(a);
    statsTimelineValues.add(b);
    statsSummaryValues.add(a);
    statsSummaryValues.add(c);
    // mock the methods used
    when(statisticsDao.getLatestKey()).thenReturn(akey);
    when(statisticsDao.getStatistic(StatisticsEnum.KEY_LIVE_IDS.value())).thenReturn(statsTimelineValues);
    when(statisticsDao.getStatistic(200l)).thenReturn(statsSummaryValues);
    // mock the methods used
    StatisticKeyEntity key200 = new StatisticKeyEntity();
    key200.setId(200L);
    key200.setGenerationDate(new Date(2000, 1, 1));
    StatisticKeyEntity key201 = new StatisticKeyEntity();
    key201.setId(201L);
    key201.setGenerationDate(new Date(1999, 1, 1));
    when(statisticsDao.getKey(200L)).thenReturn(key200);
    when(statisticsDao.getKey(201L)).thenReturn(key201);
    TargetProxyHelper.injectIntoProxy(statsManagerReadOnly, "statisticsDaoReadOnly", statisticsDao);
    // setup security context
    ArrayList<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
    roles.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
    Authentication auth = new AnonymousAuthenticationToken("anonymous", "anonymous", roles);
    SecurityContextHolder.getContext().setAuthentication(auth);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) StatisticValuesEntity(org.orcid.statistics.jpa.entities.StatisticValuesEntity) StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Date(java.util.Date) Before(org.junit.Before)

Aggregations

SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)109 GrantedAuthority (org.springframework.security.core.GrantedAuthority)64 Test (org.junit.Test)49 ArrayList (java.util.ArrayList)30 Authentication (org.springframework.security.core.Authentication)27 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)22 HashSet (java.util.HashSet)14 User (org.springframework.security.core.userdetails.User)11 ObjectIdentity (org.springframework.security.acls.model.ObjectIdentity)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)7 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)7 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)7 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)7 Before (org.junit.Before)6 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)6 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)6 List (java.util.List)5 Map (java.util.Map)5 DBUnitTest (org.orcid.test.DBUnitTest)5