Search in sources :

Example 1 with RedirectStrategy

use of org.springframework.security.web.RedirectStrategy in project spring-security by spring-projects.

the class ConcurrentSessionFilterTests method doFilterWhenOverrideThenCustomRedirectStrategyUsed.

@Test
public void doFilterWhenOverrideThenCustomRedirectStrategyUsed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    final String expiredUrl = "/expired";
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl + "will-be-overrridden") {

        /* (non-Javadoc)
					 * @see org.springframework.security.web.session.ConcurrentSessionFilter#determineExpiredUrl(javax.servlet.http.HttpServletRequest, org.springframework.security.core.session.SessionInformation)
					 */
        @Override
        protected String determineExpiredUrl(HttpServletRequest request, SessionInformation info) {
            return expiredUrl;
        }
    };
    filter.setRedirectStrategy(redirect);
    filter.doFilter(request, response, new MockFilterChain());
    verify(redirect).sendRedirect(request, response, expiredUrl);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) Matchers.anyString(org.mockito.Matchers.anyString) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 2 with RedirectStrategy

use of org.springframework.security.web.RedirectStrategy in project spring-security by spring-projects.

the class ConcurrentSessionFilterTests method doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed.

@Test
public void doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    String expiredUrl = "/expired";
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl);
    filter.setRedirectStrategy(redirect);
    filter.doFilter(request, response, new MockFilterChain());
    verify(redirect).sendRedirect(request, response, expiredUrl);
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) Matchers.anyString(org.mockito.Matchers.anyString) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 3 with RedirectStrategy

use of org.springframework.security.web.RedirectStrategy in project spring-security by spring-projects.

the class ConcurrentSessionFilterTests method doFilterWhenNoSessionThenChainIsContinued.

@Test
public void doFilterWhenNoSessionThenChainIsContinued() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    String expiredUrl = "/expired";
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl);
    filter.setRedirectStrategy(redirect);
    MockFilterChain chain = new MockFilterChain();
    filter.doFilter(request, response, chain);
    assertThat(chain.getRequest()).isNotNull();
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) Matchers.anyString(org.mockito.Matchers.anyString) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 4 with RedirectStrategy

use of org.springframework.security.web.RedirectStrategy in project spring-security-oauth by spring-projects.

the class OAuth2ClientContextFilterTests method testRedirectUri.

public void testRedirectUri(String redirect, Map<String, String> params, String result) throws Exception {
    OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
    RedirectStrategy redirectStrategy = Mockito.mock(RedirectStrategy.class);
    filter.setRedirectStrategy(redirectStrategy);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    UserRedirectRequiredException exception = new UserRedirectRequiredException(redirect, params);
    filter.redirectUser(exception, request, response);
    Mockito.verify(redirectStrategy).sendRedirect(request, response, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)

Example 5 with RedirectStrategy

use of org.springframework.security.web.RedirectStrategy in project spring-security by spring-projects.

the class SimpleUrlAuthenticationFailureHandlerTests method error401IsReturnedIfNoUrlIsSet.

@Test
public void error401IsReturnedIfNoUrlIsSet() throws Exception {
    SimpleUrlAuthenticationFailureHandler afh = new SimpleUrlAuthenticationFailureHandler();
    RedirectStrategy rs = mock(RedirectStrategy.class);
    afh.setRedirectStrategy(rs);
    assertThat(afh.getRedirectStrategy()).isSameAs(rs);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    afh.onAuthenticationFailure(request, response, mock(AuthenticationException.class));
    assertThat(response.getStatus()).isEqualTo(401);
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

RedirectStrategy (org.springframework.security.web.RedirectStrategy)10 Test (org.junit.Test)9 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 Matchers.anyString (org.mockito.Matchers.anyString)5 MockFilterChain (org.springframework.mock.web.MockFilterChain)4 SessionRegistry (org.springframework.security.core.session.SessionRegistry)4 ConcurrentSessionFilter (org.springframework.security.web.session.ConcurrentSessionFilter)4 Date (java.util.Date)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 SessionInformation (org.springframework.security.core.session.SessionInformation)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 IOException (java.io.IOException)1 MockPortResolver (org.springframework.security.MockPortResolver)1 Authentication (org.springframework.security.core.Authentication)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 AccessTokenRequiredException (org.springframework.security.oauth.consumer.AccessTokenRequiredException)1 BaseProtectedResourceDetails (org.springframework.security.oauth.consumer.BaseProtectedResourceDetails)1 OAuthConsumerToken (org.springframework.security.oauth.consumer.OAuthConsumerToken)1