001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.apache.shiro.ee.filters; 015 016import java.util.function.Supplier; 017 018import org.apache.shiro.ee.filters.AuthenticationFilterDelegate.MethodsFromFilter; 019 020import static org.apache.shiro.ee.filters.FormResubmitSupport.redirectToSaved; 021 022import org.apache.shiro.ee.filters.Forms.FallbackPredicate; 023 024import javax.servlet.ServletRequest; 025import javax.servlet.ServletResponse; 026import javax.servlet.http.HttpServletRequest; 027 028import lombok.experimental.Delegate; 029import org.apache.shiro.authc.AuthenticationException; 030import org.apache.shiro.authc.AuthenticationToken; 031import org.apache.shiro.subject.Subject; 032import org.apache.shiro.web.util.WebUtils; 033 034import static org.omnifaces.facesviews.FacesViews.FACES_VIEWS_ORIGINAL_SERVLET_PATH; 035 036/** 037 * Implements JSF Ajax redirection via OmniFaces 038 * Implements form resubmit and auto remember-me functionality 039 */ 040public class FormAuthenticationFilter extends org.apache.shiro.web.filter.authc.FormAuthenticationFilter { 041 static final String LOGIN_PREDICATE_ATTR_NAME = "org.apache.shiro.ee.login-predicate"; 042 static final String LOGIN_WAITTIME_ATTR_NAME = "org.apache.shiro.ee.login-wait-time"; 043 static final String LOGIN_URL_ATTR_NAME = "org.apache.shiro.ee.login-url"; 044 static final FallbackPredicate NO_PREDICATE = (path, request) -> false; 045 private final @Delegate AuthenticationFilterDelegate delegate; 046 047 private final class Methods implements MethodsFromFilter { 048 @Override 049 public Subject getSubject(ServletRequest request, ServletResponse response) { 050 return FormAuthenticationFilter.super.getSubject(request, response); 051 } 052 053 @Override 054 public boolean isLoginRequest(ServletRequest request, ServletResponse response) { 055 return FormAuthenticationFilter.super.isLoginRequest(request, response); 056 } 057 058 @Override 059 public boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, 060 ServletRequest request, ServletResponse response) { 061 return FormAuthenticationFilter.super.onLoginFailure(token, e, request, response); 062 } 063 064 @Override 065 public String getLoginUrl() { 066 return FormAuthenticationFilter.super.getLoginUrl(); 067 } 068 069 @Override 070 public boolean preHandle(ServletRequest request, ServletResponse response) throws Exception { 071 return FormAuthenticationFilter.super.preHandle(request, response); 072 } 073 } 074 075 public FormAuthenticationFilter() { 076 delegate = new AuthenticationFilterDelegate(new Methods()); 077 } 078 079 @Override 080 protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, 081 ServletResponse response) throws Exception { 082 if (request instanceof HttpServletRequest) { 083 FallbackPredicate loginFallbackType = (FallbackPredicate) request.getAttribute(LOGIN_PREDICATE_ATTR_NAME); 084 redirectToSaved(WebUtils.toHttp(request), WebUtils.toHttp(response), loginFallbackType, "/"); 085 } 086 return false; 087 } 088 089 @Override 090 protected String getPathWithinApplication(ServletRequest request) { 091 return getPathWithinApplication(request, () -> super.getPathWithinApplication(request)); 092 } 093 094 static String getPathWithinApplication(ServletRequest request, Supplier<String> superMethod) { 095 String origPath = (String) request.getAttribute(FACES_VIEWS_ORIGINAL_SERVLET_PATH); 096 if (origPath != null) { 097 return origPath; 098 } else { 099 return superMethod.get(); 100 } 101 } 102}