/* * #%L * BroadleafCommerce Open Admin Platform * %% * Copyright (C) 2009 - 2013 Broadleaf Commerce * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ package org.broadleafcommerce.openadmin.security; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import org.broadleafcommerce.common.util.StringUtil; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BroadleafAdminAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { private String defaultFailureUrl; public BroadleafAdminAuthenticationFailureHandler() { super(); } public BroadleafAdminAuthenticationFailureHandler(String defaultFailureUrl) { super(defaultFailureUrl); this.defaultFailureUrl = defaultFailureUrl; } @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { String failureUrlParam = StringUtil.cleanseUrlString(request.getParameter("failureUrl")); String successUrlParam = StringUtil.cleanseUrlString(request.getParameter("successUrl")); String failureUrl = failureUrlParam==null?null:failureUrlParam.trim(); Boolean sessionTimeout = (Boolean) request.getAttribute("sessionTimeout"); if (StringUtils.isEmpty(failureUrl) && BooleanUtils.isNotTrue(sessionTimeout)) { failureUrl = defaultFailureUrl; } if (BooleanUtils.isTrue(sessionTimeout)) { failureUrl = "?sessionTimeout=true"; } if (StringUtils.isEmpty(successUrlParam)) { //Grab url the user, was redirected from successUrlParam = request.getHeader("referer"); } if (failureUrl != null) { if (!StringUtils.isEmpty(successUrlParam)) { //Preserve the original successUrl from the referer. If there is one, it must be the last url segment int successUrlPos = successUrlParam.indexOf("successUrl"); if (successUrlPos >= 0) { successUrlParam = successUrlParam.substring(successUrlPos); } else { successUrlParam = "successUrl=" + successUrlParam; } if (!failureUrl.contains("?")) { failureUrl += "?" + successUrlParam; } else { failureUrl += "&" + successUrlParam; } } else { } saveException(request, exception); getRedirectStrategy().sendRedirect(request, response, failureUrl); } else { super.onAuthenticationFailure(request, response, exception); } } }