001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.web.filter.authc; 020 021import org.apache.shiro.subject.Subject; 022import org.apache.shiro.web.filter.AccessControlFilter; 023import org.apache.shiro.web.util.WebUtils; 024 025import javax.servlet.ServletRequest; 026import javax.servlet.ServletResponse; 027 028/** 029 * Base class for all Filters that require the current user to be authenticated. This class encapsulates the 030 * logic of checking whether a user is already authenticated in the system while subclasses are required to perform 031 * specific logic for unauthenticated requests. 032 * 033 * @since 0.9 034 */ 035public abstract class AuthenticationFilter extends AccessControlFilter { 036 037 @SuppressWarnings("checkstyle:JavadocVariable") 038 public static final String DEFAULT_SUCCESS_URL = "/"; 039 040 private String successUrl = DEFAULT_SUCCESS_URL; 041 042 /** 043 * Returns the success url to use as the default location a user is sent after logging in. Typically a redirect 044 * after login will redirect to the originally request URL; this property is provided mainly as a fallback in case 045 * the original request URL is not available or not specified. 046 * <p/> 047 * The default value is {@link #DEFAULT_SUCCESS_URL}. 048 * 049 * @return the success url to use as the default location a user is sent after logging in. 050 */ 051 public String getSuccessUrl() { 052 return successUrl; 053 } 054 055 /** 056 * Sets the default/fallback success url to use as the default location a user is sent after logging in. Typically 057 * a redirect after login will redirect to the originally request URL; this property is provided mainly as a 058 * fallback in case the original request URL is not available or not specified. 059 * <p/> 060 * The default value is {@link #DEFAULT_SUCCESS_URL}. 061 * 062 * @param successUrl the success URL to redirect the user to after a successful login. 063 */ 064 public void setSuccessUrl(String successUrl) { 065 this.successUrl = successUrl; 066 } 067 068 069 /** 070 * Determines whether the current subject is authenticated. 071 * <p/> 072 * The default implementation {@link #getSubject(javax.servlet.ServletRequest, javax.servlet.ServletResponse) acquires} 073 * the currently executing Subject and then returns 074 * {@link org.apache.shiro.subject.Subject#isAuthenticated() subject.isAuthenticated()}; 075 * 076 * @return true if the subject is authenticated; false if the subject is unauthenticated 077 */ 078 protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { 079 Subject subject = getSubject(request, response); 080 return subject.isAuthenticated() && subject.getPrincipal() != null; 081 } 082 083 /** 084 * Redirects to user to the previously attempted URL after a successful login. This implementation simply calls 085 * <code>{@link org.apache.shiro.web.util.WebUtils WebUtils}. 086 * {@link WebUtils#redirectToSavedRequest(ServletRequest, ServletResponse, String) redirectToSavedRequest}</code> 087 * using the {@link #getSuccessUrl() successUrl} as the {@code fallbackUrl} argument to that call. 088 * 089 * @param request the incoming request 090 * @param response the outgoing response 091 * @throws Exception if there is a problem redirecting. 092 */ 093 protected void issueSuccessRedirect(ServletRequest request, ServletResponse response) throws Exception { 094 WebUtils.redirectToSavedRequest(request, response, getSuccessUrl()); 095 } 096 097}