Class EnvironmentLoader
java.lang.Object
org.apache.shiro.web.env.EnvironmentLoader
- Direct Known Subclasses:
EnvironmentLoaderListener
An
EnvironmentLoader is responsible for loading a web application's Shiro WebEnvironment
(which includes the web app's WebSecurityManager) into the
ServletContext at application startup.
In Shiro 1.1 and earlier, the Shiro ServletFilter was responsible for creating the WebSecurityManager 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, this EnvironmentLoader (or more likely, the
EnvironmentLoaderListener 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 the EnvironmentLoader (or listener) runs first.
Usage
This implementation will look for two servlet contextcontext-params in web.xml:
shiroEnvironmentClass and shiroConfigLocations that customize how the WebEnvironment instance
will be initialized.
shiroEnvironmentClass
TheshiroEnvironmentClass context-param, if it exists, allows you to specify the
fully-qualified implementation class name of the WebEnvironment 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 the IniWebEnvironment class, which assumes Shiro's default
INI configuration format
shiroConfigLocations
TheshiroConfigLocations context-param, if it exists, allows you to specify the config location(s)
(resource path(s)) that will be relayed to the instantiated WebEnvironment. For example:
<context-param>
<param-name>shiroConfigLocations</param-name>
<param-value>/WEB-INF/someLocation/shiro.ini</param-value>
</context-param>
The WebEnvironment implementation must implement the ResourceConfigurable interface if it is to
acquire the shiroConfigLocations value.
If this context-param is not specified, the WebEnvironment instance determines default resource
lookup behavior. For example, the IniWebEnvironment 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 in web.xml.
Finally, it should be noted that this implementation was based on ideas in Spring 3's
org.springframework.web.context.ContextLoader implementation - no need to reinvent the wheel for this common
behavior.- Since:
- 1.2
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringServlet Context config param for the resource path to use for configuring theWebEnvironmentinstance:shiroConfigLocationsstatic final Stringenvironment attribute key.static final StringServlet Context config param for specifying theWebEnvironmentimplementation class to use:shiroEnvironmentClass -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected WebEnvironmentcreateEnvironment(javax.servlet.ServletContext sc) Instantiates aWebEnvironmentbased on the specified ServletContext.protected voidcustomizeEnvironment(WebEnvironment environment) Any additional customization of the Environment can be by overriding this method.voiddestroyEnvironment(javax.servlet.ServletContext servletContext) Destroys theWebEnvironmentfor the given servlet context.protected WebEnvironmentdetermineWebEnvironment(javax.servlet.ServletContext servletContext) Return the WebEnvironment implementation class to use, based on the order of: A custom WebEnvironment class - specified in theservletContextENVIRONMENT_ATTRIBUTE_KEYpropertyServiceLoader.load(WebEnvironment.class)- (if more then one instance is found aConfigurationExceptionwill be thrown A call togetDefaultWebEnvironmentClass(ServletContext)(default:IniWebEnvironment)protected Class<?> determineWebEnvironmentClass(javax.servlet.ServletContext servletContext) Deprecated.protected Iterator<WebEnvironment> protected voidfinalizeEnvironment(WebEnvironment environment) Any additional cleanup of the Environment can be done by overriding this method.protected Class<? extends WebEnvironment> getDefaultWebEnvironmentClass(javax.servlet.ServletContext ctx) Returns the default WebEnvironment class, which is unless overridden:IniWebEnvironment.initEnvironment(javax.servlet.ServletContext servletContext) Initializes Shiro'sWebEnvironmentinstance for the specifiedServletContextbased on theCONFIG_LOCATIONS_PARAMvalue.
-
Field Details
-
ENVIRONMENT_CLASS_PARAM
Servlet Context config param for specifying theWebEnvironmentimplementation class to use:shiroEnvironmentClass- See Also:
-
CONFIG_LOCATIONS_PARAM
Servlet Context config param for the resource path to use for configuring theWebEnvironmentinstance:shiroConfigLocations- See Also:
-
ENVIRONMENT_ATTRIBUTE_KEY
environment attribute key.
-
-
Constructor Details
-
EnvironmentLoader
public EnvironmentLoader()
-
-
Method Details
-
initEnvironment
public WebEnvironment initEnvironment(javax.servlet.ServletContext servletContext) throws IllegalStateException Initializes Shiro'sWebEnvironmentinstance for the specifiedServletContextbased on theCONFIG_LOCATIONS_PARAMvalue.- Parameters:
servletContext- current servlet context- Returns:
- the new Shiro
WebEnvironmentinstance. - Throws:
IllegalStateException- if an existing WebEnvironment has already been initialized and associated with the specifiedServletContext.
-
determineWebEnvironmentClass
@Deprecated protected Class<?> determineWebEnvironmentClass(javax.servlet.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 defaultIniWebEnvironmentor a custom class if specified.- Parameters:
servletContext- current servlet context- Returns:
- the WebEnvironment implementation class to use
- See Also:
-
doLoadWebEnvironmentsFromServiceLoader
-
getDefaultWebEnvironmentClass
protected Class<? extends WebEnvironment> getDefaultWebEnvironmentClass(javax.servlet.ServletContext ctx) Returns the default WebEnvironment class, which is unless overridden:IniWebEnvironment.- Parameters:
ctx- servlet context- Returns:
- the default WebEnvironment class.
-
determineWebEnvironment
Return the WebEnvironment implementation class to use, based on the order of:- A custom WebEnvironment class
- specified in the
servletContextENVIRONMENT_ATTRIBUTE_KEYproperty ServiceLoader.load(WebEnvironment.class)- (if more then one instance is found aConfigurationExceptionwill be thrown- A call to
getDefaultWebEnvironmentClass(ServletContext)(default:IniWebEnvironment)
- Parameters:
servletContext- current servlet contextservletContext- theservletContextto query theENVIRONMENT_ATTRIBUTE_KEYproperty from- Returns:
- the WebEnvironment implementation class to use
- See Also:
- A custom WebEnvironment class
- specified in the
-
createEnvironment
Instantiates aWebEnvironmentbased on the specified ServletContext. This implementationdeterminesaWebEnvironmentimplementation class to use. That class is instantiated, configured, and returned. This allows customWebEnvironmentimplementations to be specified via a ServletContext init-param if desired. If not specified, the defaultIniWebEnvironmentimplementation will be used.- Parameters:
sc- current servlet context- Returns:
- the constructed Shiro WebEnvironment instance
- See Also:
-
customizeEnvironment
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
Destroys theWebEnvironmentfor the given servlet context.- Parameters:
servletContext- the ServletContext attributed to the WebSecurityManager
-
finalizeEnvironment
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
-
determineWebEnvironment(ServletContext)ordetermineWebEnvironment(ServletContext)