Search in sources :

Example 6 with AuthenticationManager

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

the class AbstractPreAuthenticatedProcessingFilterTests method requiresAuthenticationFalsePrincipalNotString.

// SEC-2078
@Test
public void requiresAuthenticationFalsePrincipalNotString() throws Exception {
    Object principal = new Object();
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(principal, "something", "ROLE_USER"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
    filter.setCheckForPrincipalChanges(true);
    filter.principal = principal;
    AuthenticationManager am = mock(AuthenticationManager.class);
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(am);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with AuthenticationManager

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

the class AbstractPreAuthenticatedProcessingFilterTests method requiresAuthenticationFalsePrincipalUser.

@Test
public void requiresAuthenticationFalsePrincipalUser() throws Exception {
    User currentPrincipal = new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
    UsernamePasswordAuthenticationToken currentAuthentication = new UsernamePasswordAuthenticationToken(currentPrincipal, currentPrincipal.getPassword(), currentPrincipal.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(currentAuthentication);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
    filter.setCheckForPrincipalChanges(true);
    filter.principal = new User(currentPrincipal.getUsername(), currentPrincipal.getPassword(), AuthorityUtils.NO_AUTHORITIES);
    AuthenticationManager am = mock(AuthenticationManager.class);
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(am);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with AuthenticationManager

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

the class AbstractPreAuthenticatedProcessingFilterTests method getFilter.

private static ConcretePreAuthenticatedProcessingFilter getFilter(boolean grantAccess) throws Exception {
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
    AuthenticationManager am = mock(AuthenticationManager.class);
    if (!grantAccess) {
        when(am.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException(""));
    } else {
        when(am.authenticate(any(Authentication.class))).thenAnswer(new Answer<Authentication>() {

            public Authentication answer(InvocationOnMock invocation) throws Throwable {
                return (Authentication) invocation.getArguments()[0];
            }
        });
    }
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    return filter;
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 9 with AuthenticationManager

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

the class AbstractPreAuthenticatedProcessingFilterTests method requiresAuthenticationOverridePrincipalChangedFalse.

@Test
public void requiresAuthenticationOverridePrincipalChangedFalse() throws Exception {
    Object principal = new Object();
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(principal, "something", "ROLE_USER"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter() {

        @Override
        protected boolean principalChanged(HttpServletRequest request, Authentication currentAuthentication) {
            return false;
        }
    };
    filter.setCheckForPrincipalChanges(true);
    filter.principal = principal;
    AuthenticationManager am = mock(AuthenticationManager.class);
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(am);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with AuthenticationManager

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

the class AbstractPreAuthenticatedProcessingFilterTests method testAfterPropertiesSetInvokesSuper.

// SEC-2045
@Test
public void testAfterPropertiesSetInvokesSuper() throws Exception {
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
    AuthenticationManager am = mock(AuthenticationManager.class);
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    assertThat(filter.initFilterBeanInvoked).isTrue();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) 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