Package org.apache.shiro.web.env
Class EnvironmentLoader
- java.lang.Object
-
- org.apache.shiro.web.env.EnvironmentLoader
-
- Direct Known Subclasses:
EnvironmentLoaderListener
public class EnvironmentLoader extends Object
AnEnvironmentLoader
is responsible for loading a web application's ShiroWebEnvironment
(which includes the web app'sWebSecurityManager
) into theServletContext
at application startup. In Shiro 1.1 and earlier, the Shiro ServletFilter was responsible for creating theWebSecurityManager
and any additional objects (security filters, etc). However, any component not filtered by the Shiro Filter (such as other context listeners) was not able to easily acquire the these objects to perform security operations. Due to this, in Shiro 1.2 and later, thisEnvironmentLoader
(or more likely, theEnvironmentLoaderListener
subclass) is the preferred mechanism to initialize a Shiro environment. The Shiro Filter, while still required for request filtering, will not perform this initialization at startup if theEnvironmentLoader
(or listener) runs first.Usage
This implementation will look for two servlet contextcontext-param
s inweb.xml
:shiroEnvironmentClass
andshiroConfigLocations
that customize how theWebEnvironment
instance will be initialized.shiroEnvironmentClass
TheshiroEnvironmentClass
context-param
, if it exists, allows you to specify the fully-qualified implementation class name of theWebEnvironment
to instantiate. For example:<context-param> <param-name>shiroEnvironmentClass</param-name> <param-value>com.foo.bar.shiro.MyWebEnvironment</param-value> </context-param>
If not specified, the default value is theIniWebEnvironment
class, which assumes Shiro's default INI configuration formatshiroConfigLocations
TheshiroConfigLocations
context-param
, if it exists, allows you to specify the config location(s) (resource path(s)) that will be relayed to the instantiatedWebEnvironment
. For example:<context-param> <param-name>shiroConfigLocations</param-name> <param-value>/WEB-INF/someLocation/shiro.ini</param-value> </context-param>
TheWebEnvironment
implementation must implement theResourceConfigurable
interface if it is to acquire theshiroConfigLocations
value. If thiscontext-param
is not specified, theWebEnvironment
instance determines default resource lookup behavior. For example, theIniWebEnvironment
will check the following two locations for INI config by default (in order):- /WEB-INF/shiro.ini
- classpath:shiro.ini
Web Security Enforcement
Using this loader will only initialize Shiro's environment in a web application - it will not filter web requests or perform web-specific security operations. To do this, you must ensure that you have also configured theShiroFilter
inweb.xml
. Finally, it should be noted that this implementation was based on ideas in Spring 3'sorg.springframework.web.context.ContextLoader
implementation - no need to reinvent the wheel for this common behavior.- Since:
- 1.2
- See Also:
EnvironmentLoaderListener
,ShiroFilter
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONFIG_LOCATIONS_PARAM
Servlet Context config param for the resource path to use for configuring theWebEnvironment
instance:shiroConfigLocations
static String
ENVIRONMENT_ATTRIBUTE_KEY
static String
ENVIRONMENT_CLASS_PARAM
Servlet Context config param for specifying theWebEnvironment
implementation class to use:shiroEnvironmentClass
-
Constructor Summary
Constructors Constructor Description EnvironmentLoader()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected WebEnvironment
createEnvironment(ServletContext sc)
Instantiates aWebEnvironment
based on the specified ServletContext.protected void
customizeEnvironment(WebEnvironment environment)
Any additional customization of the Environment can be by overriding this method.void
destroyEnvironment(ServletContext servletContext)
Destroys theWebEnvironment
for the given servlet context.protected WebEnvironment
determineWebEnvironment(ServletContext servletContext)
Return the WebEnvironment implementation class to use, based on the order of: A custom WebEnvironment class - specified in theservletContext
ENVIRONMENT_ATTRIBUTE_KEY
propertyServiceLoader.load(WebEnvironment.class)
- (if more then one instance is found aConfigurationException
will be thrown A call togetDefaultWebEnvironmentClass()
(default:IniWebEnvironment
)protected Class<?>
determineWebEnvironmentClass(ServletContext servletContext)
Deprecated.This method is not longer used by Shiro, and will be removed in future versions, usedetermineWebEnvironment(ServletContext)
ordetermineWebEnvironment(ServletContext)
protected void
finalizeEnvironment(WebEnvironment environment)
Any additional cleanup of the Environment can be done by overriding this method.protected Class<? extends WebEnvironment>
getDefaultWebEnvironmentClass()
Returns the default WebEnvironment class, which is unless overridden:IniWebEnvironment
.WebEnvironment
initEnvironment(ServletContext servletContext)
Initializes Shiro'sWebEnvironment
instance for the specifiedServletContext
based on theCONFIG_LOCATIONS_PARAM
value.
-
-
-
Field Detail
-
ENVIRONMENT_CLASS_PARAM
public static final String ENVIRONMENT_CLASS_PARAM
Servlet Context config param for specifying theWebEnvironment
implementation class to use:shiroEnvironmentClass
- See Also:
- Constant Field Values
-
CONFIG_LOCATIONS_PARAM
public static final String CONFIG_LOCATIONS_PARAM
Servlet Context config param for the resource path to use for configuring theWebEnvironment
instance:shiroConfigLocations
- See Also:
- Constant Field Values
-
ENVIRONMENT_ATTRIBUTE_KEY
public static final String ENVIRONMENT_ATTRIBUTE_KEY
-
-
Constructor Detail
-
EnvironmentLoader
public EnvironmentLoader()
-
-
Method Detail
-
initEnvironment
public WebEnvironment initEnvironment(ServletContext servletContext) throws IllegalStateException
Initializes Shiro'sWebEnvironment
instance for the specifiedServletContext
based on theCONFIG_LOCATIONS_PARAM
value.- Parameters:
servletContext
- current servlet context- Returns:
- the new Shiro
WebEnvironment
instance. - Throws:
IllegalStateException
- if an existing WebEnvironment has already been initialized and associated with the specifiedServletContext
.
-
determineWebEnvironmentClass
@Deprecated protected Class<?> determineWebEnvironmentClass(ServletContext servletContext)
Deprecated.This method is not longer used by Shiro, and will be removed in future versions, usedetermineWebEnvironment(ServletContext)
ordetermineWebEnvironment(ServletContext)
Return the WebEnvironment implementation class to use, either the defaultIniWebEnvironment
or a custom class if specified.- Parameters:
servletContext
- current servlet context- Returns:
- the WebEnvironment implementation class to use
- See Also:
ENVIRONMENT_CLASS_PARAM
,IniWebEnvironment
,determineWebEnvironment(ServletContext)
,getDefaultWebEnvironmentClass()
-
getDefaultWebEnvironmentClass
protected Class<? extends WebEnvironment> getDefaultWebEnvironmentClass()
Returns the default WebEnvironment class, which is unless overridden:IniWebEnvironment
.- Returns:
- the default WebEnvironment class.
-
determineWebEnvironment
protected WebEnvironment determineWebEnvironment(ServletContext servletContext)
Return the WebEnvironment implementation class to use, based on the order of:- A custom WebEnvironment class - specified in the
servletContext
ENVIRONMENT_ATTRIBUTE_KEY
property ServiceLoader.load(WebEnvironment.class)
- (if more then one instance is found aConfigurationException
will be thrown- A call to
getDefaultWebEnvironmentClass()
(default:IniWebEnvironment
)
- Parameters:
servletContext
- current servlet contextservletContext
- theservletContext
to query theENVIRONMENT_ATTRIBUTE_KEY
property from- Returns:
- the WebEnvironment implementation class to use
- See Also:
ENVIRONMENT_CLASS_PARAM
- A custom WebEnvironment class - specified in the
-
createEnvironment
protected WebEnvironment createEnvironment(ServletContext sc)
Instantiates aWebEnvironment
based on the specified ServletContext. This implementationdetermines
aWebEnvironment
implementation class to use. That class is instantiated, configured, and returned. This allows customWebEnvironment
implementations to be specified via a ServletContext init-param if desired. If not specified, the defaultIniWebEnvironment
implementation will be used.- Parameters:
sc
- current servlet context- Returns:
- the constructed Shiro WebEnvironment instance
- See Also:
MutableWebEnvironment
,ResourceConfigurable
-
customizeEnvironment
protected void customizeEnvironment(WebEnvironment environment)
Any additional customization of the Environment can be by overriding this method. For example setup shared resources, etc. By default this method does nothing.- Parameters:
environment
-
-
destroyEnvironment
public void destroyEnvironment(ServletContext servletContext)
Destroys theWebEnvironment
for the given servlet context.- Parameters:
servletContext
- the ServletContext attributed to the WebSecurityManager
-
finalizeEnvironment
protected void finalizeEnvironment(WebEnvironment environment)
Any additional cleanup of the Environment can be done by overriding this method. For example clean up shared resources, etc. By default this method does nothing.- Parameters:
environment
-- Since:
- 1.3
-
-