Search in sources :

Example 1 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-boot by spring-projects.

the class SampleSecureApplicationTests method init.

@Before
public void init() {
    AuthenticationManager authenticationManager = this.context.getBean(AuthenticationManager.class);
    this.authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Before(org.junit.Before)

Example 2 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testDefaultUsernamePassword.

@Test
public void testDefaultUsernamePassword() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityAutoConfiguration.class);
    this.context.refresh();
    SecurityProperties security = this.context.getBean(SecurityProperties.class);
    AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(security.getUser().getName(), security.getUser().getPassword());
    assertThat(manager.authenticate(token)).isNotNull();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 3 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser.

@Test
public void testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(AuthenticationManagerCustomizer.class, SecurityAutoConfiguration.class);
    this.context.refresh();
    SecurityProperties security = this.context.getBean(SecurityProperties.class);
    AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(security.getUser().getName(), security.getUser().getPassword());
    try {
        manager.authenticate(token);
        fail("Expected Exception");
    } catch (AuthenticationException success) {
    // Expected
    }
    token = new UsernamePasswordAuthenticationToken("foo", "bar");
    assertThat(manager.authenticate(token)).isNotNull();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 4 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security-oauth by spring-projects.

the class ResourceServerBeanDefinitionParserTests method testAuthenticationManager.

@Test
public void testAuthenticationManager() {
    GenericXmlApplicationContext context = new GenericXmlApplicationContext(getClass(), "resource-server-authmanager-context.xml");
    // System.err.println(Arrays.asList(context.getBeanDefinitionNames()));
    assertTrue(context.containsBeanDefinition("oauth2ProviderFilter"));
    OAuth2AuthenticationProcessingFilter filter = context.getBean(OAuth2AuthenticationProcessingFilter.class);
    assertEquals(context.getBean(AuthenticationManager.class), ReflectionTestUtils.getField(filter, "authenticationManager"));
    assertNotNull(ReflectionTestUtils.getField(filter, "tokenExtractor"));
    context.close();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) OAuth2AuthenticationProcessingFilter(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.Test)

Example 5 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security by spring-projects.

the class AuthenticationConfigurationGh3935Tests method delegateUsesExisitingAuthentication.

@Test
public void delegateUsesExisitingAuthentication() {
    String username = "user";
    String password = "password";
    User user = new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER"));
    when(this.uds.loadUserByUsername(username)).thenReturn(user);
    AuthenticationManager authenticationManager = this.adapter.authenticationManager;
    assertThat(authenticationManager).isNotNull();
    Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    verify(this.uds).loadUserByUsername(username);
    assertThat(auth.getPrincipal()).isEqualTo(user);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)44 Test (org.junit.Test)29 Authentication (org.springframework.security.core.Authentication)24 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 MockFilterChain (org.springframework.mock.web.MockFilterChain)11 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)11 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)10 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 FilterChain (javax.servlet.FilterChain)7 AuthenticationException (org.springframework.security.core.AuthenticationException)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 Before (org.junit.Before)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 User (org.springframework.security.core.userdetails.User)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 OAuth2AuthenticationProcessingFilter (org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter)2