use of org.springframework.security.authentication.BadCredentialsException in project spring-security by spring-projects.
the class AbstractPreAuthenticatedProcessingFilterTests method getFilter.
private static ConcretePreAuthenticatedProcessingFilter getFilter(boolean grantAccess) {
ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
AuthenticationManager am = mock(AuthenticationManager.class);
if (!grantAccess) {
given(am.authenticate(any(Authentication.class))).willThrow(new BadCredentialsException(""));
} else {
given(am.authenticate(any(Authentication.class))).willAnswer((Answer<Authentication>) (invocation) -> (Authentication) invocation.getArguments()[0]);
}
filter.setAuthenticationManager(am);
filter.afterPropertiesSet();
return filter;
}
use of org.springframework.security.authentication.BadCredentialsException in project spring-security by spring-projects.
the class AbstractPreAuthenticatedProcessingFilterTests method exceptionIsThrownOnFailedAuthenticationIfContinueFilterChainOnUnsuccessfulAuthenticationSetToFalse.
/* SEC-881 */
@Test
public void exceptionIsThrownOnFailedAuthenticationIfContinueFilterChainOnUnsuccessfulAuthenticationSetToFalse() throws Exception {
AuthenticationManager am = mock(AuthenticationManager.class);
given(am.authenticate(any(Authentication.class))).willThrow(new BadCredentialsException(""));
this.filter.setContinueFilterChainOnUnsuccessfulAuthentication(false);
this.filter.setAuthenticationManager(am);
this.filter.afterPropertiesSet();
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), mock(FilterChain.class)));
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.springframework.security.authentication.BadCredentialsException in project spring-security by spring-projects.
the class ExceptionMappingAuthenticationFailureHandlerTests method defaultTargetUrlIsUsedIfNoMappingExists.
@Test
public void defaultTargetUrlIsUsedIfNoMappingExists() throws Exception {
ExceptionMappingAuthenticationFailureHandler fh = new ExceptionMappingAuthenticationFailureHandler();
fh.setDefaultFailureUrl("/failed");
MockHttpServletResponse response = new MockHttpServletResponse();
fh.onAuthenticationFailure(new MockHttpServletRequest(), response, new BadCredentialsException(""));
assertThat(response.getRedirectedUrl()).isEqualTo("/failed");
}
use of org.springframework.security.authentication.BadCredentialsException in project spring-security by spring-projects.
the class AuthenticationFilterTests method filterWhenDefaultsAndAuthenticationFailThenUnauthorized.
@Test
public void filterWhenDefaultsAndAuthenticationFailThenUnauthorized() throws Exception {
Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE");
given(this.authenticationConverter.convert(any())).willReturn(authentication);
given(this.authenticationManager.authenticate(any())).willThrow(new BadCredentialsException("failed"));
AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManager, this.authenticationConverter);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain chain = mock(FilterChain.class);
filter.doFilter(request, response, chain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.springframework.security.authentication.BadCredentialsException in project spring-security by spring-projects.
the class AuthenticationFilterTests method filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationFailThenUnauthorized.
@Test
public void filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationFailThenUnauthorized() throws Exception {
givenResolveWillReturnAuthenticationManager();
Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE");
given(this.authenticationConverter.convert(any())).willReturn(authentication);
given(this.authenticationManager.authenticate(any())).willThrow(new BadCredentialsException("failed"));
AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManagerResolver, this.authenticationConverter);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain chain = mock(FilterChain.class);
filter.doFilter(request, response, chain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Aggregations