Search in sources :

Example 21 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project webanno by webanno.

the class LoginPage method getRedirectUrl.

private String getRedirectUrl() {
    String redirectUrl = null;
    HttpSession session = ((ServletWebRequest) RequestCycle.get().getRequest()).getContainerRequest().getSession(false);
    if (session != null) {
        SavedRequest savedRequest = (SavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if (savedRequest != null) {
            redirectUrl = savedRequest.getRedirectUrl();
        }
    }
    // There is some kind of bug that logs the user out again if the redirect page is
    // the context root and if that does not end in a slash. To avoid this, we add a slash
    // here. This is rather a hack, but I have no idea why this problem occurs. Figured this
    // out through trial-and-error rather then by in-depth debugging.
    String baseUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse(""));
    if (baseUrl.equals(redirectUrl)) {
        redirectUrl += "/";
    }
    // URL.
    if (redirectUrl != null && isNotBlank(form.urlfragment)) {
        redirectUrl += "#" + form.urlfragment;
    }
    return redirectUrl;
}
Also used : HttpSession(javax.servlet.http.HttpSession) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest)

Example 22 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project app-template by xtuer.

the class AuthenticationController method bindUser.

/**
 * 绑定用户
 */
@GetMapping("/page/bindUser")
@ResponseBody
public String bindUser(HttpServletRequest request, HttpServletResponse response) {
    // 1. 绑定用户,用户不存在则先创建
    // TODO
    // 2. 绑定用户成功后使用 savedRequest 重定向到登陆前的页面,这里只是为了展示怎么取到登陆前页面的 URL
    SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
    String redirectUrl = (savedRequest != null) ? savedRequest.getRedirectUrl() : "/";
    return redirectUrl;
}
Also used : HttpSessionRequestCache(org.springframework.security.web.savedrequest.HttpSessionRequestCache) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest)

Example 23 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.

the class BasicAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.

@Test
void shouldInvokeHandler() throws IOException {
    final BasicAuthenticationWithRedirectToLoginFilter filter = new BasicAuthenticationWithRedirectToLoginFilter(null, null);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final String message = "foo";
    SavedRequest savedRequest = mock(SavedRequest.class);
    SessionUtils.saveRequest(request, savedRequest);
    HttpSession originalSession = request.getSession(true);
    filter.onAuthenticationFailure(request, response, message);
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("foo");
    assertThat(request.getSession(false)).isNotSameAs(originalSession);
    assertThat(SessionUtils.savedRequest(request)).isSameAs(savedRequest);
    assertThat(SessionUtils.hasAuthenticationToken(request)).isFalse();
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.jupiter.api.Test)

Example 24 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.

the class UserEnabledCheckFilterWithRedirectToLoginPageTest method shouldRedirectToLoginPageWithAnErrorMessageInTheSession.

@Test
void shouldRedirectToLoginPageWithAnErrorMessageInTheSession() throws IOException {
    SavedRequest savedRequest = mock(SavedRequest.class);
    SessionUtils.saveRequest(request, savedRequest);
    HttpSession originalSession = request.getSession(true);
    filter.handleFailure(request, response, "something bad happened!");
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("something bad happened!");
    assertThat(request.getSession(false)).isNotSameAs(originalSession);
    assertThat(SessionUtils.savedRequest(request)).isSameAs(savedRequest);
    assertThat(SessionUtils.hasAuthenticationToken(request)).isFalse();
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("something bad happened!");
}
Also used : HttpSession(javax.servlet.http.HttpSession) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.jupiter.api.Test)

Example 25 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest 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();
    given(savedRequest.getRedirectUrl()).willReturn(redirectUrl);
    given(requestCache.getRequest(request, response)).willReturn(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.jupiter.api.Test)

Aggregations

SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)28 HttpSessionRequestCache (org.springframework.security.web.savedrequest.HttpSessionRequestCache)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 HttpSession (javax.servlet.http.HttpSession)4 Test (org.junit.jupiter.api.Test)4 Authentication (org.springframework.security.core.Authentication)4 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 DefaultSavedRequest (org.springframework.security.web.savedrequest.DefaultSavedRequest)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 MidpointAuthentication (com.evolveum.midpoint.authentication.api.config.MidpointAuthentication)2 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)2 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)2 RequestInfoForm (org.orcid.pojo.ajaxForm.RequestInfoForm)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 RequestCache (org.springframework.security.web.savedrequest.RequestCache)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ModuleAuthentication (com.evolveum.midpoint.authentication.api.config.ModuleAuthentication)1 ModuleAuthenticationImpl (com.evolveum.midpoint.authentication.impl.module.authentication.ModuleAuthenticationImpl)1