use of org.springframework.security.web.savedrequest.SavedRequest in project midpoint by Evolveum.
the class MidPointAuthenticationSuccessHandler method onAuthenticationSuccess.
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
String urlSuffix = AuthConstants.DEFAULT_PATH_AFTER_LOGIN;
String authenticatedChannel = null;
if (authentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
ModuleAuthenticationImpl moduleAuthentication = (ModuleAuthenticationImpl) mpAuthentication.getProcessingModuleAuthentication();
moduleAuthentication.setState(AuthenticationModuleState.SUCCESSFULLY);
if (mpAuthentication.getAuthenticationChannel() != null) {
authenticatedChannel = mpAuthentication.getAuthenticationChannel().getChannelId();
if (mpAuthentication.isAuthenticated()) {
urlSuffix = mpAuthentication.getAuthenticationChannel().getPathAfterSuccessfulAuthentication();
mpAuthentication.getAuthenticationChannel().postSuccessAuthenticationProcessing();
if (mpAuthentication.getAuthenticationChannel().isPostAuthenticationEnabled()) {
getRedirectStrategy().sendRedirect(request, response, urlSuffix);
return;
}
} else {
urlSuffix = mpAuthentication.getAuthenticationChannel().getPathDuringProccessing();
}
}
}
SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest != null && savedRequest.getRedirectUrl().contains(ModuleWebSecurityConfigurationImpl.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH + "/")) {
String target = savedRequest.getRedirectUrl().substring(0, savedRequest.getRedirectUrl().indexOf(ModuleWebSecurityConfigurationImpl.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH + "/")) + urlSuffix;
getRedirectStrategy().sendRedirect(request, response, target);
return;
}
if (savedRequest != null && authenticatedChannel != null) {
int startIndex = savedRequest.getRedirectUrl().indexOf(request.getContextPath()) + request.getContextPath().length();
int endIndex = savedRequest.getRedirectUrl().length() - 1;
String channelSavedRequest = null;
if ((startIndex < endIndex)) {
String localePath = savedRequest.getRedirectUrl().substring(startIndex, endIndex);
channelSavedRequest = AuthSequenceUtil.searchChannelByPath(localePath);
}
if (channelSavedRequest == null) {
channelSavedRequest = SecurityPolicyUtil.DEFAULT_CHANNEL;
}
if (!(channelSavedRequest.equals(authenticatedChannel))) {
getRedirectStrategy().sendRedirect(request, response, urlSuffix);
return;
}
} else {
setDefaultTargetUrl(urlSuffix);
}
super.onAuthenticationSuccess(request, response, authentication);
}
use of org.springframework.security.web.savedrequest.SavedRequest in project midpoint by Evolveum.
the class MidpointAuthenticationFailureHandler method onAuthenticationFailure.
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String urlSuffix = AuthConstants.DEFAULT_PATH_AFTER_LOGIN;
if (authentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
if (mpAuthentication.isAuthenticated()) {
getRedirectStrategy().sendRedirect(request, response, urlSuffix);
return;
}
ModuleAuthentication moduleAuthentication = mpAuthentication.getProcessingModuleAuthentication();
if (mpAuthentication.getAuthenticationChannel() != null) {
if (mpAuthentication.isLast(moduleAuthentication) && mpAuthentication.getAuthenticationChannel().isDefault()) {
urlSuffix = getPathAfterUnsuccessfulAuthentication(mpAuthentication.getAuthenticationChannel());
} else {
urlSuffix = mpAuthentication.getAuthenticationChannel().getPathDuringProccessing();
}
}
moduleAuthentication.setState(AuthenticationModuleState.FAILURE);
}
saveException(request, exception);
SavedRequest savedRequest = getRequestCache().getRequest(request, response);
if (savedRequest == null || StringUtils.isBlank(savedRequest.getRedirectUrl()) || ((DefaultSavedRequest) savedRequest).getServletPath().startsWith(ModuleWebSecurityConfiguration.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH)) {
getRedirectStrategy().sendRedirect(request, response, urlSuffix);
return;
}
getRedirectStrategy().sendRedirect(request, response, savedRequest.getRedirectUrl());
}
use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.
the class ReAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.
@Test
void shouldInvokeHandler() throws IOException {
final ReAuthenticationWithRedirectToLoginFilter filter = new ReAuthenticationWithRedirectToLoginFilter(null, null, null, null, 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 AuthenticationController method performLogin.
@RequestMapping(value = "/auth/security_check", method = RequestMethod.POST)
public RedirectView performLogin(@RequestParam("j_username") String username, @RequestParam("j_password") String password, HttpServletRequest request) {
if (securityIsDisabledOrAlreadyLoggedIn(request)) {
return new RedirectView("/pipelines", true);
}
LOGGER.debug("Requesting authentication for form auth.");
try {
SavedRequest savedRequest = SessionUtils.savedRequest(request);
final AuthenticationToken<UsernamePassword> authenticationToken = passwordBasedPluginAuthenticationProvider.authenticate(new UsernamePassword(username, password), null);
if (authenticationToken == null) {
return badAuthentication(request, BAD_CREDENTIALS_MSG);
} else {
SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
}
String redirectUrl = savedRequest == null ? "/go/pipelines" : savedRequest.getRedirectUrl();
return new RedirectView(redirectUrl, false);
} catch (AuthenticationException e) {
LOGGER.error("Failed to authenticate user: {} ", username, e);
return badAuthentication(request, e.getMessage());
} catch (Exception e) {
return unknownAuthenticationError(request);
}
}
use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.
the class AuthenticationController method authenticateWithWebBasedPlugin.
@RequestMapping(value = "/plugin/{pluginId}/authenticate")
public RedirectView authenticateWithWebBasedPlugin(@PathVariable("pluginId") String pluginId, HttpServletRequest request) {
if (securityIsDisabledOrAlreadyLoggedIn(request)) {
return new RedirectView("/pipelines", true);
}
LOGGER.debug("Requesting authentication for form auth.");
SavedRequest savedRequest = SessionUtils.savedRequest(request);
try {
final AccessToken accessToken = webBasedPluginAuthenticationProvider.fetchAccessToken(pluginId, getRequestHeaders(request), getParameterMap(request));
AuthenticationToken<AccessToken> authenticationToken = webBasedPluginAuthenticationProvider.authenticate(accessToken, pluginId);
if (authenticationToken == null) {
return unknownAuthenticationError(request);
}
SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
} catch (AuthenticationException e) {
LOGGER.error("Failed to authenticate user.", e);
return badAuthentication(request, e.getMessage());
} catch (Exception e) {
return unknownAuthenticationError(request);
}
SessionUtils.removeAuthenticationError(request);
String redirectUrl = savedRequest == null ? "/go/pipelines" : savedRequest.getRedirectUrl();
return new RedirectView(redirectUrl, false);
}
Aggregations