Search in sources :

Example 46 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project opennms by OpenNMS.

the class SpringSecurityContextServiceTest method setUp.

@Before
public void setUp() throws Exception {
    SecurityContext context = new SecurityContextImpl();
    User principal = new User(USERNAME, PASS, true, true, true, true, Arrays.asList(new GrantedAuthority[] { ROLE_ADMIN, ROLE_PROVISION }));
    org.springframework.security.core.Authentication auth = new PreAuthenticatedAuthenticationToken(principal, new Object());
    context.setAuthentication(auth);
    SecurityContextHolder.setContext(context);
    this.m_securityContextService = new SpringSecurityContextService();
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Before(org.junit.Before)

Example 47 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority 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 48 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method setUpMockAuthenticationContext.

private void setUpMockAuthenticationContext() {
    // initialise the context with the user identified by the email 
    // "test1@test.com" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList<GrantedAuthority>();
    TgolUserDetails tud = new TgolUserDetails("test1@test.com", "", true, false, true, true, gac, mockAdminUser);
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.getName()).andReturn("admin@test.com").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) TgolUserDetails(org.asqatasun.webapp.security.userdetails.TgolUserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 49 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project Asqatasun by Asqatasun.

the class PageListControllerTest method setUpMockAuthenticationContext.

/**
     * 
     */
private void setUpMockAuthenticationContext() {
    // initialise the context with the user identified by the email 
    // "test1@test.com" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList();
    TgolUserDetails tud = new TgolUserDetails("test1@test.com", "", true, false, true, true, gac, mockUser);
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.getName()).andReturn("test1@test.com").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("test1@test.com").anyTimes();
    replay(mockAuthenticationDetails);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) TgolUserDetails(org.asqatasun.webapp.security.userdetails.TgolUserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) AuthenticationDetails(org.springframework.security.authentication.AuthenticationDetails)

Example 50 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project Asqatasun by Asqatasun.

the class ContractControllerTest method setUpMockAuthenticationContext.

private void setUpMockAuthenticationContext() {
    // initialise the context with the user identified by the email 
    // "test1@test.com" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList();
    TgolUserDetails tud = new TgolUserDetails("test1@test.com", "", true, false, true, true, gac, mockUser);
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.getName()).andReturn("test1@test.com").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("test1@test.com").anyTimes();
    replay(mockAuthenticationDetails);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) TgolUserDetails(org.asqatasun.webapp.security.userdetails.TgolUserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) AuthenticationDetails(org.springframework.security.authentication.AuthenticationDetails)

Aggregations

GrantedAuthority (org.springframework.security.core.GrantedAuthority)188 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)90 Authentication (org.springframework.security.core.Authentication)55 ArrayList (java.util.ArrayList)43 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)37 HashSet (java.util.HashSet)27 UserDetails (org.springframework.security.core.userdetails.UserDetails)16 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)15 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)11 Before (org.junit.Before)10 SecurityContext (org.springframework.security.core.context.SecurityContext)10 User (org.springframework.security.core.userdetails.User)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 DefaultGrantedAuthority (eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority)9 List (java.util.List)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)9 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8