Search in sources :

Example 6 with RedirectStrategy

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

the class RetryWithHttpEntryPointTests method testGettersSetters.

@Test
public void testGettersSetters() {
    RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    PortMapper portMapper = mock(PortMapper.class);
    PortResolver portResolver = mock(PortResolver.class);
    RedirectStrategy redirector = mock(RedirectStrategy.class);
    ep.setPortMapper(portMapper);
    ep.setPortResolver(portResolver);
    ep.setRedirectStrategy(redirector);
    assertThat(ep.getPortMapper()).isSameAs(portMapper);
    assertThat(ep.getPortResolver()).isSameAs(portResolver);
    assertThat(ep.getRedirectStrategy()).isSameAs(redirector);
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortResolver(org.springframework.security.web.PortResolver) PortMapper(org.springframework.security.web.PortMapper) RedirectStrategy(org.springframework.security.web.RedirectStrategy) Test(org.junit.Test)

Example 7 with RedirectStrategy

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

the class SavedRequestAwareAuthenticationSuccessHandlerTests method onAuthenticationSuccessHasSavedRequest.

@Test
public void onAuthenticationSuccessHasSavedRequest() throws Exception {
    String redirectUrl = "http://localhost/appcontext/page";
    RedirectStrategy redirectStrategy = mock(RedirectStrategy.class);
    RequestCache requestCache = mock(RequestCache.class);
    SavedRequest savedRequest = mock(SavedRequest.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    when(savedRequest.getRedirectUrl()).thenReturn(redirectUrl);
    when(requestCache.getRequest(request, response)).thenReturn(savedRequest);
    SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
    handler.setRequestCache(requestCache);
    handler.setRedirectStrategy(redirectStrategy);
    handler.onAuthenticationSuccess(request, response, mock(Authentication.class));
    verify(redirectStrategy).sendRedirect(request, response, redirectUrl);
}
Also used : RequestCache(org.springframework.security.web.savedrequest.RequestCache) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.Test)

Example 8 with RedirectStrategy

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

the class ConcurrentSessionFilterTests method doFilterWhenNoSessionInformationThenChainIsContinued.

@Test
public void doFilterWhenNoSessionInformationThenChainIsContinued() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession());
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    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 : 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) Test(org.junit.Test)

Example 9 with RedirectStrategy

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

the class UserAuthorizationSuccessfulAuthenticationHandlerTests method testAuthenticationSuccess.

/**
	 * test determineTargetUrl
	 */
@Test
public void testAuthenticationSuccess() throws Exception {
    UserAuthorizationSuccessfulAuthenticationHandler handler = new UserAuthorizationSuccessfulAuthenticationHandler();
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    RedirectStrategy redirectStrategy = mock(RedirectStrategy.class);
    handler.setRedirectStrategy(redirectStrategy);
    when(request.getAttribute(UserAuthorizationProcessingFilter.CALLBACK_ATTRIBUTE)).thenReturn("http://my.host.com/my/context");
    when(request.getAttribute(UserAuthorizationProcessingFilter.VERIFIER_ATTRIBUTE)).thenReturn("myver");
    when(request.getParameter("requestToken")).thenReturn("mytok");
    handler.onAuthenticationSuccess(request, response, null);
    verify(redirectStrategy).sendRedirect(request, response, "http://my.host.com/my/context?oauth_token=mytok&oauth_verifier=myver");
    handler = new UserAuthorizationSuccessfulAuthenticationHandler();
    handler.setRedirectStrategy(redirectStrategy);
    when(request.getAttribute(UserAuthorizationProcessingFilter.CALLBACK_ATTRIBUTE)).thenReturn("http://my.hosting.com/my/context?with=some&query=parameter");
    when(request.getAttribute(UserAuthorizationProcessingFilter.VERIFIER_ATTRIBUTE)).thenReturn("myvera");
    when(request.getParameter("requestToken")).thenReturn("mytoka");
    handler.onAuthenticationSuccess(request, response, null);
    verify(redirectStrategy).sendRedirect(request, response, "http://my.hosting.com/my/context?with=some&query=parameter&oauth_token=mytoka&oauth_verifier=myvera");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserAuthorizationSuccessfulAuthenticationHandler(org.springframework.security.oauth.provider.filter.UserAuthorizationSuccessfulAuthenticationHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) RedirectStrategy(org.springframework.security.web.RedirectStrategy) Test(org.junit.Test)

Example 10 with RedirectStrategy

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

the class OAuthConsumerContextFilterTests method testDoFilter.

/**
	 * tests the filter.
	 */
@Test
public void testDoFilter() throws Exception {
    final OAuthRememberMeServices rememberMeServices = new NoOpOAuthRememberMeServices();
    final BaseProtectedResourceDetails resource = new BaseProtectedResourceDetails();
    resource.setId("dep1");
    OAuthConsumerContextFilter filter = new OAuthConsumerContextFilter() {

        @Override
        protected String getCallbackURL(HttpServletRequest request) {
            return "urn:callback";
        }

        @Override
        protected String getUserAuthorizationRedirectURL(ProtectedResourceDetails details, OAuthConsumerToken requestToken, String callbackURL) {
            return callbackURL + "&" + requestToken.getResourceId();
        }
    };
    filter.setRedirectStrategy(new RedirectStrategy() {

        public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
            response.sendRedirect(url);
        }
    });
    filter.setTokenServices(tokenServices);
    filter.setConsumerSupport(support);
    filter.setRememberMeServices(rememberMeServices);
    doThrow(new AccessTokenRequiredException(resource)).when(filterChain).doFilter(request, response);
    when(tokenServices.getToken("dep1")).thenReturn(null);
    when(request.getParameter("oauth_verifier")).thenReturn(null);
    when(response.encodeRedirectURL("urn:callback")).thenReturn("urn:callback?query");
    OAuthConsumerToken token = new OAuthConsumerToken();
    token.setAccessToken(false);
    token.setResourceId(resource.getId());
    when(support.getUnauthorizedRequestToken("dep1", "urn:callback?query")).thenReturn(token);
    filter.doFilter(request, response, filterChain);
    verify(filterChain).doFilter(request, response);
    verify(tokenServices).storeToken("dep1", token);
    verify(response).sendRedirect("urn:callback?query&dep1");
    verify(request, times(2)).setAttribute(anyString(), anyObject());
    reset(request, response, filterChain);
    doThrow(new AccessTokenRequiredException(resource)).when(filterChain).doFilter(request, response);
    when(tokenServices.getToken("dep1")).thenReturn(token);
    when(request.getParameter(OAuthProviderParameter.oauth_verifier.toString())).thenReturn("verifier");
    OAuthConsumerToken accessToken = new OAuthConsumerToken();
    when(support.getAccessToken(token, "verifier")).thenReturn(accessToken);
    when(response.isCommitted()).thenReturn(false);
    filter.doFilter(request, response, filterChain);
    verify(filterChain, times(2)).doFilter(request, response);
    verify(tokenServices).removeToken("dep1");
    verify(tokenServices).storeToken("dep1", accessToken);
    verify(request, times(2)).setAttribute(anyString(), anyObject());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuthRememberMeServices(org.springframework.security.oauth.consumer.rememberme.OAuthRememberMeServices) NoOpOAuthRememberMeServices(org.springframework.security.oauth.consumer.rememberme.NoOpOAuthRememberMeServices) BaseProtectedResourceDetails(org.springframework.security.oauth.consumer.BaseProtectedResourceDetails) AccessTokenRequiredException(org.springframework.security.oauth.consumer.AccessTokenRequiredException) HttpServletResponse(javax.servlet.http.HttpServletResponse) NoOpOAuthRememberMeServices(org.springframework.security.oauth.consumer.rememberme.NoOpOAuthRememberMeServices) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) RedirectStrategy(org.springframework.security.web.RedirectStrategy) ProtectedResourceDetails(org.springframework.security.oauth.consumer.ProtectedResourceDetails) BaseProtectedResourceDetails(org.springframework.security.oauth.consumer.BaseProtectedResourceDetails) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) 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