2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.web;
12 import java.io.IOException;
15 import javax.servlet.ServletException;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
19 import org.springframework.security.core.Authentication;
20 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
21 import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
22 import org.springframework.security.web.savedrequest.RequestCache;
23 import org.springframework.security.web.savedrequest.SavedRequest;
24 import org.springframework.util.StringUtils;
26 public class ControllerAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
27 private RequestCache requestCache = new HttpSessionRequestCache();
30 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
31 Authentication authentication) throws ServletException, IOException {
32 SavedRequest savedRequest = requestCache.getRequest(request, response);
34 if (savedRequest == null) {
35 super.onAuthenticationSuccess(request, response, authentication);
39 String targetUrlParameter = getTargetUrlParameter();
40 if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
41 requestCache.removeRequest(request, response);
42 super.onAuthenticationSuccess(request, response, authentication);
47 clearAuthenticationAttributes(request);
49 // Use the DefaultSavedRequest URL
51 String targetUrl = savedRequest.getRedirectUrl();
52 //workaround to avoid being redirected to ajax calls
53 Map<String, String[]> m = savedRequest.getParameterMap();
56 String[] value = m.get("x-page-url");
57 if(value != null && value.length > 0)
58 targetUrl = request.getContextPath() + "#" + value[0];
60 logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
64 getRedirectStrategy().sendRedirect(request, response, targetUrl);
67 public void setRequestCache(RequestCache requestCache) {
68 this.requestCache = requestCache;