Search in sources :

Example 36 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class PreAuthenticatedAuthenticationProviderTests method authenticateInvalidToken.

@Test
public final void authenticateInvalidToken() throws Exception {
    UserDetails ud = new User("dummyUser", "dummyPwd", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new UsernamePasswordAuthenticationToken("dummyUser", "dummyPwd");
    Authentication result = provider.authenticate(request);
    assertThat(result).isNull();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 37 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class RequestAttributeAuthenticationFilterTests method userIsReauthenticatedIfPrincipalChangesAndCheckForPrincipalChangesIsSet.

@Test
public void userIsReauthenticatedIfPrincipalChangesAndCheckForPrincipalChangesIsSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
    filter.setAuthenticationManager(createAuthenticationManager());
    filter.setCheckForPrincipalChanges(true);
    request.setAttribute("REMOTE_USER", "cat");
    filter.doFilter(request, response, new MockFilterChain());
    request = new MockHttpServletRequest();
    request.setAttribute("REMOTE_USER", "dog");
    filter.doFilter(request, response, new MockFilterChain());
    Authentication dog = SecurityContextHolder.getContext().getAuthentication();
    assertThat(dog).isNotNull();
    assertThat(dog.getName()).isEqualTo("dog");
    // Make sure authentication doesn't occur every time (i.e. if the variable
    // *doesn't*
    // change)
    filter.setAuthenticationManager(mock(AuthenticationManager.class));
    filter.doFilter(request, response, new MockFilterChain());
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(dog);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) RequestAttributeAuthenticationFilter(org.springframework.security.web.authentication.preauth.RequestAttributeAuthenticationFilter) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 38 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class RememberMeAuthenticationFilterTests method testOperationWhenAuthenticationExistsInContextHolder.

@Test
public void testOperationWhenAuthenticationExistsInContextHolder() throws Exception {
    // Put an Authentication object into the SecurityContextHolder
    Authentication originalAuth = new TestingAuthenticationToken("user", "password", "ROLE_A");
    SecurityContextHolder.getContext().setAuthentication(originalAuth);
    // Setup our filter correctly
    RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(mock(AuthenticationManager.class), new MockRememberMeServices(remembered));
    filter.afterPropertiesSet();
    // Test
    MockHttpServletRequest request = new MockHttpServletRequest();
    FilterChain fc = mock(FilterChain.class);
    request.setRequestURI("x");
    filter.doFilter(request, new MockHttpServletResponse(), fc);
    // Ensure filter didn't change our original object
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
    verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 39 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class TokenBasedRememberMeServicesTests method autoLoginReturnsNullIfNoCookiePresented.

@Test
public void autoLoginReturnsNullIfNoCookiePresented() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();
    Authentication result = services.autoLogin(new MockHttpServletRequest(), response);
    assertThat(result).isNull();
    // No cookie set
    assertThat(response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY)).isNull();
}
Also used : Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 40 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class AbstractRememberMeServicesTests method autoLoginShouldFailIfUserNotFound.

@Test
public void autoLoginShouldFailIfUserNotFound() {
    uds.setThrowException(true);
    MockRememberMeServices services = new MockRememberMeServices(uds);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(createLoginCookie("cookie:1:2"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    Authentication result = services.autoLogin(request, response);
    assertThat(result).isNull();
    assertCookieCancelled(response);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Aggregations

Authentication (org.springframework.security.core.Authentication)498 Test (org.junit.Test)192 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)114 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)98 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)75 SecurityContext (org.springframework.security.core.context.SecurityContext)63 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)57 GrantedAuthority (org.springframework.security.core.GrantedAuthority)50 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)47 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)42 MifosUser (org.mifos.security.MifosUser)38 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)32 AuthenticationException (org.springframework.security.core.AuthenticationException)31 UserDetails (org.springframework.security.core.userdetails.UserDetails)31 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)29 HashMap (java.util.HashMap)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)27 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)25