Search in sources :

Example 16 with AuthenticationManager

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

the class RememberMeAuthenticationFilterTests method authenticationSuccessHandlerIsInvokedOnSuccessfulAuthenticationIfSet.

@Test
public void authenticationSuccessHandlerIsInvokedOnSuccessfulAuthenticationIfSet() throws Exception {
    AuthenticationManager am = mock(AuthenticationManager.class);
    when(am.authenticate(remembered)).thenReturn(remembered);
    RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(am, new MockRememberMeServices(remembered));
    filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/target"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain fc = mock(FilterChain.class);
    request.setRequestURI("x");
    filter.doFilter(request, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo("/target");
    // Should return after success handler is invoked, so chain should not proceed
    verifyZeroInteractions(fc);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) SimpleUrlAuthenticationSuccessHandler(org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 17 with AuthenticationManager

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

the class ResourceOwnerPasswordTokenGranterTests method testBadCredentials.

@Test(expected = InvalidGrantException.class)
public void testBadCredentials() {
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(new AuthenticationManager() {

        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            throw new BadCredentialsException("test");
        }
    }, providerTokenServices, clientDetailsService, requestFactory);
    granter.grant("password", tokenRequest);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 18 with AuthenticationManager

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

the class WebSecurityConfigurerAdapter method getHttp.

/**
	 * Creates the {@link HttpSecurity} or returns the current instance
	 *
	 * ] * @return the {@link HttpSecurity}
	 * @throws Exception
	 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected final HttpSecurity getHttp() throws Exception {
    if (http != null) {
        return http;
    }
    DefaultAuthenticationEventPublisher eventPublisher = objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
    localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);
    AuthenticationManager authenticationManager = authenticationManager();
    authenticationBuilder.parentAuthenticationManager(authenticationManager);
    Map<Class<? extends Object>, Object> sharedObjects = createSharedObjects();
    http = new HttpSecurity(objectPostProcessor, authenticationBuilder, sharedObjects);
    if (!disableDefaults) {
        // @formatter:off
        http.csrf().and().addFilter(new WebAsyncManagerIntegrationFilter()).exceptionHandling().and().headers().and().sessionManagement().and().securityContext().and().requestCache().and().anonymous().and().servletApi().and().apply(new DefaultLoginPageConfigurer<HttpSecurity>()).and().logout();
        // @formatter:on
        ClassLoader classLoader = this.context.getClassLoader();
        List<AbstractHttpConfigurer> defaultHttpConfigurers = SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, classLoader);
        for (AbstractHttpConfigurer configurer : defaultHttpConfigurers) {
            http.apply(configurer);
        }
    }
    configure(http);
    return http;
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) AbstractHttpConfigurer(org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer) WebAsyncManagerIntegrationFilter(org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter)

Example 19 with AuthenticationManager

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

the class HttpBasicConfigurer method configure.

@Override
public void configure(B http) throws Exception {
    AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
    BasicAuthenticationFilter basicAuthenticationFilter = new BasicAuthenticationFilter(authenticationManager, this.authenticationEntryPoint);
    if (this.authenticationDetailsSource != null) {
        basicAuthenticationFilter.setAuthenticationDetailsSource(this.authenticationDetailsSource);
    }
    RememberMeServices rememberMeServices = http.getSharedObject(RememberMeServices.class);
    if (rememberMeServices != null) {
        basicAuthenticationFilter.setRememberMeServices(rememberMeServices);
    }
    basicAuthenticationFilter = postProcess(basicAuthenticationFilter);
    http.addFilter(basicAuthenticationFilter);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) RememberMeServices(org.springframework.security.web.authentication.RememberMeServices) BasicAuthenticationFilter(org.springframework.security.web.authentication.www.BasicAuthenticationFilter)

Example 20 with AuthenticationManager

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

the class CasAuthenticationFilterTests method testNullServiceTicketHandledGracefully.

@Test(expected = AuthenticationException.class)
public void testNullServiceTicketHandledGracefully() throws Exception {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    filter.setAuthenticationManager(new AuthenticationManager() {

        public Authentication authenticate(Authentication a) {
            throw new BadCredentialsException("Rejected");
        }
    });
    filter.attemptAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse());
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) 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