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;
}
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;
}
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");
}
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!");
}
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);
}
Aggregations