Search in sources :

Example 91 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project opennms by OpenNMS.

the class RadiusAuthenticationProviderTest method doTestRetrieveUserMultipleTimes.

public void doTestRetrieveUserMultipleTimes(RadiusAuthenticator authenticator) {
    RadiusAuthenticationProvider provider = new RadiusAuthenticationProvider(m_radiusServer, m_sharedSecret);
    RadiusAuthenticator authTypeClass = authenticator;
    provider.setAuthTypeClass(authTypeClass);
    provider.setRolesAttribute("Unknown-VSAttribute(5813:1)");
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(m_principal, m_credentials);
    provider.retrieveUser(m_username, token);
    provider.retrieveUser(m_username, token);
    provider.retrieveUser(m_username, token);
    provider.retrieveUser(m_username, token);
    provider.retrieveUser(m_username, token);
    provider.retrieveUser(m_username, token);
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) RadiusAuthenticator(net.jradius.client.auth.RadiusAuthenticator)

Example 92 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project opennms by OpenNMS.

the class SimpleBackEndTest method testBackendWithBasicAuth.

@Test
@JUnitHttpServer(port = 9162, basicAuth = true, webapps = @Webapp(context = "/", path = "src/test/resources/simple-test-webapp"))
public void testBackendWithBasicAuth() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("testuser", "testpassword"));
    assertNotNull(m_authBackEnd);
    assertEquals("first get should be 0", 0, m_authBackEnd.getCount());
    assertEquals("second should be 1", 1, m_authBackEnd.getCount());
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 93 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project opennms by OpenNMS.

the class AuthenticationIT method testAuthenticateTempUser.

@Test
public void testAuthenticateTempUser() throws Exception {
    OnmsUser user = new OnmsUser("tempuser");
    user.setFullName("Temporary User");
    user.setPassword("18126E7BD3F84B3F3E4DF094DEF5B7DE");
    user.setDutySchedule(Arrays.asList("MoTuWeThFrSaSu800-2300"));
    m_userManager.save(user);
    org.springframework.security.core.Authentication authentication = new UsernamePasswordAuthenticationToken("tempuser", "mike");
    org.springframework.security.core.Authentication authenticated = m_provider.authenticate(authentication);
    assertNotNull("authenticated Authentication object not null", authenticated);
    Collection<? extends GrantedAuthority> authorities = authenticated.getAuthorities();
    assertNotNull("GrantedAuthorities should not be null", authorities);
    assertEquals("GrantedAuthorities size", 1, authorities.size());
    assertContainsAuthority(Authentication.ROLE_USER, authorities);
}
Also used : OnmsUser(org.opennms.netmgt.model.OnmsUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 94 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken 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 95 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project Asqatasun by Asqatasun.

the class LoginController method doGuestAutoLogin.

private void doGuestAutoLogin(HttpServletRequest request, String guestUser) {
    try {
        // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(guestUser, guestPassword);
        token.setDetails(new WebAuthenticationDetails(request));
        Authentication guest = authenticationManager.authenticate(token);
        Logger.getLogger(this.getClass()).debug("Logging in with [{}]" + guest.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(guest);
    } catch (Exception e) {
        SecurityContextHolder.getContext().setAuthentication(null);
        Logger.getLogger(this.getClass()).debug("Failure in autoLogin", e);
    }
}
Also used : WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)309 Test (org.junit.Test)156 Authentication (org.springframework.security.core.Authentication)114 GrantedAuthority (org.springframework.security.core.GrantedAuthority)37 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)34 UserDetails (org.springframework.security.core.userdetails.UserDetails)33 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 SecurityContext (org.springframework.security.core.context.SecurityContext)21 AuthenticationException (org.springframework.security.core.AuthenticationException)20 User (org.springframework.security.core.userdetails.User)17 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)15 ArrayList (java.util.ArrayList)14 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)13 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)12 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)11 Before (org.junit.Before)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8