Package org.apache.shiro.spring.web
Class ShiroFilterFactoryBean
- java.lang.Object
-
- org.apache.shiro.spring.web.ShiroFilterFactoryBean
-
- All Implemented Interfaces:
org.springframework.beans.factory.config.BeanPostProcessor,org.springframework.beans.factory.FactoryBean
public class ShiroFilterFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean, org.springframework.beans.factory.config.BeanPostProcessor
FactoryBeanto be used in Spring-based web applications for defining the master Shiro Filter.Usage
Declare a DelegatingFilterProxy inweb.xml, matching the filter name to the bean id:<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy<filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter>Then, in your spring XML file that defines your web ApplicationContext:<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- other properties as necessary ... --> </bean>Filter Auto-Discovery
While there is afiltersproperty that allows you to assign a filter beans to the 'pool' of filters available when definingfilter chains, it is optional. This implementation is also aBeanPostProcessorand will acquire anyFilterbeans defined independently in your Spring application context. Upon discovery, they will be automatically added to themapkeyed by the bean ID. That ID can then be used in the filter chain definitions, for example:<bean id="myCustomFilter" class="com.class.that.implements.javax.servlet.Filter"/> ... <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> ... <property name="filterChainDefinitions"> <value> /some/path/** = authc, myCustomFilter </value> </property> </bean>Global Property Values
Most Shiro servlet Filter implementations exist for defining custom Filterchain definitions. Most implementations subclass one of theAccessControlFilter,AuthenticationFilter,AuthorizationFilterclasses to simplify things, and each of these 3 classes has configurable properties that are application-specific. A dilemma arises where, if you want to for example set the application's 'loginUrl' for any Filter, you don't want to have to manually specify that value for each filter instance definied. To prevent configuration duplication, this implementation provides the following properties to allow you to set relevant values in only one place: Then at startup, any values specified via these 3 properties will be applied to all configured Filter instances so you don't have to specify them individually on each filter instance. To ensure your own custom filters benefit from this convenience, your filter implementation should subclass one of the 3 mentioned earlier.- Since:
- 1.0
- See Also:
DelegatingFilterProxy
-
-
Constructor Summary
Constructors Constructor Description ShiroFilterFactoryBean()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.apache.shiro.web.filter.mgt.FilterChainManagercreateFilterChainManager()protected org.apache.shiro.web.servlet.AbstractShiroFiltercreateInstance()This implementation: Ensures the requiredsecurityManagerproperty has been setCreatesaFilterChainManagerinstance that reflects the configuredfiltersandfilter chain definitionsWraps the FilterChainManager with a suitableFilterChainResolversince the Shiro Filter implementations do not know ofFilterChainManagers Sets both theSecurityManagerandFilterChainResolverinstances on a new Shiro Filter instance and returns that filter instance.Map<String,String>getFilterChainDefinitionMap()Returns the chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter.Map<String,javax.servlet.Filter>getFilters()Returns the filterName-to-Filter map of filters available for reference when defining filter chain definitions.StringgetLoginUrl()Returns the application's login URL to be assigned to all acquired Filters that subclassAccessControlFilterornullif no value should be assigned globally.ObjectgetObject()Lazily creates and returns aAbstractShiroFilterconcrete instance via thecreateInstance()method.ClassgetObjectType()ReturnsAbstractShiroFilter.classorg.apache.shiro.mgt.SecurityManagergetSecurityManager()Sets the applicationSecurityManagerinstance to be used by the constructed Shiro Filter.StringgetSuccessUrl()Returns the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilterornullif no value should be assigned globally.StringgetUnauthorizedUrl()Returns the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilterornullif no value should be assigned globally.booleanisSingleton()Returnstruealways.ObjectpostProcessAfterInitialization(Object bean, String beanName)Does nothing - only exists to satisfy the BeanPostProcessor interface and immediately returns thebeanargument.ObjectpostProcessBeforeInitialization(Object bean, String beanName)Inspects a bean, and if it implements theFilterinterface, automatically adds that filter instance to the internalfilters mapthat will be referenced later during filter chain construction.voidsetFilterChainDefinitionMap(Map<String,String> filterChainDefinitionMap)Sets the chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter.voidsetFilterChainDefinitions(String definitions)A convenience method that sets thefilterChainDefinitionMapproperty by accepting aProperties-compatible string (multi-line key/value pairs).voidsetFilters(Map<String,javax.servlet.Filter> filters)Sets the filterName-to-Filter map of filters available for reference when creatingfilter chain definitions.voidsetGlobalFilters(List<String> globalFilters)Sets the list of filters that will be executed against every request.voidsetLoginUrl(String loginUrl)Sets the application's login URL to be assigned to all acquired Filters that subclassAccessControlFilter.voidsetSecurityManager(org.apache.shiro.mgt.SecurityManager securityManager)Sets the applicationSecurityManagerinstance to be used by the constructed Shiro Filter.voidsetSuccessUrl(String successUrl)Sets the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilter.voidsetUnauthorizedUrl(String unauthorizedUrl)Sets the application's 'unauthorized' URL to be assigned to all acquired Filters that subclassAuthorizationFilter.
-
-
-
Method Detail
-
getSecurityManager
public org.apache.shiro.mgt.SecurityManager getSecurityManager()
Sets the applicationSecurityManagerinstance to be used by the constructed Shiro Filter. This is a required property - failure to set it will throw an initialization exception.- Returns:
- the application
SecurityManagerinstance to be used by the constructed Shiro Filter.
-
setSecurityManager
public void setSecurityManager(org.apache.shiro.mgt.SecurityManager securityManager)
Sets the applicationSecurityManagerinstance to be used by the constructed Shiro Filter. This is a required property - failure to set it will throw an initialization exception.- Parameters:
securityManager- the applicationSecurityManagerinstance to be used by the constructed Shiro Filter.
-
getLoginUrl
public String getLoginUrl()
Returns the application's login URL to be assigned to all acquired Filters that subclassAccessControlFilterornullif no value should be assigned globally. The default value isnull.- Returns:
- the application's login URL to be assigned to all acquired Filters that subclass
AccessControlFilterornullif no value should be assigned globally. - See Also:
setLoginUrl(java.lang.String)
-
setLoginUrl
public void setLoginUrl(String loginUrl)
Sets the application's login URL to be assigned to all acquired Filters that subclassAccessControlFilter. This is a convenience mechanism: for all configuredfilters, as well for any default ones (authc,user, etc), this value will be passed on to each Filter via theAccessControlFilter.setLoginUrl(String)method*. This eliminates the need to configure the 'loginUrl' property manually on each filter instance, and instead that can be configured once via this attribute. *If a filter already has already been explicitly configured with a value, it will not receive this value. Individual filter configuration overrides this global convenience property.- Parameters:
loginUrl- the application's login URL to apply to as a convenience to all discoveredAccessControlFilterinstances.- See Also:
AccessControlFilter.setLoginUrl(String)
-
getSuccessUrl
public String getSuccessUrl()
Returns the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilterornullif no value should be assigned globally. The default value isnull.- Returns:
- the application's after-login success URL to be assigned to all acquired Filters that subclass
AuthenticationFilterornullif no value should be assigned globally. - See Also:
setSuccessUrl(java.lang.String)
-
setSuccessUrl
public void setSuccessUrl(String successUrl)
Sets the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilter. This is a convenience mechanism: for all configuredfilters, as well for any default ones (authc,user, etc), this value will be passed on to each Filter via theAuthenticationFilter.setSuccessUrl(String)method*. This eliminates the need to configure the 'successUrl' property manually on each filter instance, and instead that can be configured once via this attribute. *If a filter already has already been explicitly configured with a value, it will not receive this value. Individual filter configuration overrides this global convenience property.- Parameters:
successUrl- the application's after-login success URL to apply to as a convenience to all discoveredAccessControlFilterinstances.- See Also:
AuthenticationFilter.setSuccessUrl(String)
-
getUnauthorizedUrl
public String getUnauthorizedUrl()
Returns the application's after-login success URL to be assigned to all acquired Filters that subclassAuthenticationFilterornullif no value should be assigned globally. The default value isnull.- Returns:
- the application's after-login success URL to be assigned to all acquired Filters that subclass
AuthenticationFilterornullif no value should be assigned globally. - See Also:
setSuccessUrl(java.lang.String)
-
setUnauthorizedUrl
public void setUnauthorizedUrl(String unauthorizedUrl)
Sets the application's 'unauthorized' URL to be assigned to all acquired Filters that subclassAuthorizationFilter. This is a convenience mechanism: for all configuredfilters, as well for any default ones (roles,perms, etc), this value will be passed on to each Filter via theAuthorizationFilter.setUnauthorizedUrl(String)method*. This eliminates the need to configure the 'unauthorizedUrl' property manually on each filter instance, and instead that can be configured once via this attribute. *If a filter already has already been explicitly configured with a value, it will not receive this value. Individual filter configuration overrides this global convenience property.- Parameters:
unauthorizedUrl- the application's 'unauthorized' URL to apply to as a convenience to all discoveredAuthorizationFilterinstances.- See Also:
AuthorizationFilter.setUnauthorizedUrl(String)
-
getFilters
public Map<String,javax.servlet.Filter> getFilters()
Returns the filterName-to-Filter map of filters available for reference when defining filter chain definitions. All filter chain definitions will reference filters by the names in this map (i.e. the keys).- Returns:
- the filterName-to-Filter map of filters available for reference when defining filter chain definitions.
-
setFilters
public void setFilters(Map<String,javax.servlet.Filter> filters)
Sets the filterName-to-Filter map of filters available for reference when creatingfilter chain definitions. Note: This property is optional: thisFactoryBeanimplementation will discover all beans in the web application context that implement theFilterinterface and automatically add them to this filter map under their bean name. For example, just defining this bean in a web Spring XML application context:<bean id="myFilter" class="com.class.that.implements.javax.servlet.Filter"> ... </bean>
Will automatically place that bean into this Filters map under the key 'myFilter'.- Parameters:
filters- the optional filterName-to-Filter map of filters available for reference when creating(java.util.Map) filter chain definitions.
-
getFilterChainDefinitionMap
public Map<String,String> getFilterChainDefinitionMap()
Returns the chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter. Each map entry should conform to the format defined by theFilterChainManager.createChain(String, String)JavaDoc, where the map key is the chain name (e.g. URL path expression) and the map value is the comma-delimited string chain definition.- Returns:
- he chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter.
-
setFilterChainDefinitionMap
public void setFilterChainDefinitionMap(Map<String,String> filterChainDefinitionMap)
Sets the chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter. Each map entry should conform to the format defined by theFilterChainManager.createChain(String, String)JavaDoc, where the map key is the chain name (e.g. URL path expression) and the map value is the comma-delimited string chain definition.- Parameters:
filterChainDefinitionMap- the chainName-to-chainDefinition map of chain definitions to use for creating filter chains intercepted by the Shiro Filter.
-
setFilterChainDefinitions
public void setFilterChainDefinitions(String definitions)
A convenience method that sets thefilterChainDefinitionMapproperty by accepting aProperties-compatible string (multi-line key/value pairs). Each key/value pair must conform to the format defined by theFilterChainManager.createChain(String,String)JavaDoc - each property key is an ant URL path expression and the value is the comma-delimited chain definition.- Parameters:
definitions- aProperties-compatible string (multi-line key/value pairs) where each key/value pair represents a single urlPathExpression-commaDelimitedChainDefinition.
-
setGlobalFilters
public void setGlobalFilters(List<String> globalFilters)
Sets the list of filters that will be executed against every request. Defaults to theInvalidRequestFilterwhich will block known invalid request attacks.- Parameters:
globalFilters- the list of filters to execute before specific path filters.
-
getObject
public Object getObject() throws Exception
Lazily creates and returns aAbstractShiroFilterconcrete instance via thecreateInstance()method.- Specified by:
getObjectin interfaceorg.springframework.beans.factory.FactoryBean- Returns:
- the application's Shiro Filter instance used to filter incoming web requests.
- Throws:
Exception- if there is a problem creating theFilterinstance.
-
getObjectType
public Class getObjectType()
ReturnsAbstractShiroFilter.class- Specified by:
getObjectTypein interfaceorg.springframework.beans.factory.FactoryBean- Returns:
AbstractShiroFilter.class
-
isSingleton
public boolean isSingleton()
Returnstruealways. There is almost always only ever 1 ShiroFilterper web application.- Specified by:
isSingletonin interfaceorg.springframework.beans.factory.FactoryBean- Returns:
truealways. There is almost always only ever 1 ShiroFilterper web application.
-
createFilterChainManager
protected org.apache.shiro.web.filter.mgt.FilterChainManager createFilterChainManager()
-
createInstance
protected org.apache.shiro.web.servlet.AbstractShiroFilter createInstance() throws ExceptionThis implementation:- Ensures the required
securityManagerproperty has been set CreatesaFilterChainManagerinstance that reflects the configuredfiltersandfilter chain definitions- Wraps the FilterChainManager with a suitable
FilterChainResolversince the Shiro Filter implementations do not know ofFilterChainManagers - Sets both the
SecurityManagerandFilterChainResolverinstances on a new Shiro Filter instance and returns that filter instance.
- Returns:
- a new Shiro Filter reflecting any configured filters and filter chain definitions.
- Throws:
Exception- if there is a problem creating the AbstractShiroFilter instance.
- Ensures the required
-
postProcessBeforeInitialization
public Object postProcessBeforeInitialization(Object bean, String beanName) throws org.springframework.beans.BeansException
Inspects a bean, and if it implements theFilterinterface, automatically adds that filter instance to the internalfilters mapthat will be referenced later during filter chain construction.- Specified by:
postProcessBeforeInitializationin interfaceorg.springframework.beans.factory.config.BeanPostProcessor- Throws:
org.springframework.beans.BeansException
-
postProcessAfterInitialization
public Object postProcessAfterInitialization(Object bean, String beanName) throws org.springframework.beans.BeansException
Does nothing - only exists to satisfy the BeanPostProcessor interface and immediately returns thebeanargument.- Specified by:
postProcessAfterInitializationin interfaceorg.springframework.beans.factory.config.BeanPostProcessor- Throws:
org.springframework.beans.BeansException
-
-