public class EnvironmentLoader extends Object
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.
context-param
s in web.xml
:
shiroEnvironmentClass
and shiroConfigLocations
that customize how the WebEnvironment
instance
will be initialized.
shiroEnvironmentClass
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
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):
ShiroFilter
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.EnvironmentLoaderListener
,
ShiroFilter
Modifier and Type | Field and Description |
---|---|
static String |
CONFIG_LOCATIONS_PARAM
Servlet Context config param for the resource path to use for configuring the
WebEnvironment instance:
shiroConfigLocations |
static String |
ENVIRONMENT_ATTRIBUTE_KEY |
static String |
ENVIRONMENT_CLASS_PARAM
Servlet Context config param for specifying the
WebEnvironment implementation class to use:
shiroEnvironmentClass |
Constructor and Description |
---|
EnvironmentLoader() |
Modifier and Type | Method and Description |
---|---|
protected WebEnvironment |
createEnvironment(javax.servlet.ServletContext sc)
Instantiates a
WebEnvironment based on the specified ServletContext. |
protected void |
customizeEnvironment(WebEnvironment environment)
Any additional customization of the Environment can be by overriding this method.
|
void |
destroyEnvironment(javax.servlet.ServletContext servletContext)
Destroys the
WebEnvironment for the given servlet context. |
protected WebEnvironment |
determineWebEnvironment(javax.servlet.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 a ConfigurationException will be thrown
A call to getDefaultWebEnvironmentClass() (default: IniWebEnvironment )
|
protected Class<?> |
determineWebEnvironmentClass(javax.servlet.ServletContext servletContext)
Deprecated.
This method is not longer used by Shiro, and will be removed in future versions,
use
determineWebEnvironment(ServletContext) or determineWebEnvironment(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(javax.servlet.ServletContext servletContext)
Initializes Shiro's
WebEnvironment instance for the specified ServletContext based on the
CONFIG_LOCATIONS_PARAM value. |
public static final String ENVIRONMENT_CLASS_PARAM
WebEnvironment
implementation class to use:
shiroEnvironmentClass
public static final String CONFIG_LOCATIONS_PARAM
WebEnvironment
instance:
shiroConfigLocations
public static final String ENVIRONMENT_ATTRIBUTE_KEY
public WebEnvironment initEnvironment(javax.servlet.ServletContext servletContext) throws IllegalStateException
WebEnvironment
instance for the specified ServletContext
based on the
CONFIG_LOCATIONS_PARAM
value.servletContext
- current servlet contextWebEnvironment
instance.IllegalStateException
- if an existing WebEnvironment has already been initialized and associated with
the specified ServletContext
.@Deprecated protected Class<?> determineWebEnvironmentClass(javax.servlet.ServletContext servletContext)
determineWebEnvironment(ServletContext)
or determineWebEnvironment(ServletContext)
IniWebEnvironment
or a custom class if specified.servletContext
- current servlet contextENVIRONMENT_CLASS_PARAM
,
IniWebEnvironment
,
determineWebEnvironment(ServletContext)
,
getDefaultWebEnvironmentClass()
protected Class<? extends WebEnvironment> getDefaultWebEnvironmentClass()
IniWebEnvironment
.protected WebEnvironment determineWebEnvironment(javax.servlet.ServletContext servletContext)
servletContext
ENVIRONMENT_ATTRIBUTE_KEY
propertyServiceLoader.load(WebEnvironment.class)
- (if more then one instance is found a ConfigurationException
will be throwngetDefaultWebEnvironmentClass()
(default: IniWebEnvironment
)servletContext
- current servlet contextservletContext
- the servletContext
to query the ENVIRONMENT_ATTRIBUTE_KEY
property fromENVIRONMENT_CLASS_PARAM
protected WebEnvironment createEnvironment(javax.servlet.ServletContext sc)
WebEnvironment
based on the specified ServletContext.
This implementation determines
a
WebEnvironment
implementation class to use. That class is instantiated, configured, and returned.
This allows custom WebEnvironment
implementations to be specified via a ServletContext init-param if
desired. If not specified, the default IniWebEnvironment
implementation will be used.sc
- current servlet contextMutableWebEnvironment
,
ResourceConfigurable
protected void customizeEnvironment(WebEnvironment environment)
environment
- public void destroyEnvironment(javax.servlet.ServletContext servletContext)
WebEnvironment
for the given servlet context.servletContext
- the ServletContext attributed to the WebSecurityManagerprotected void finalizeEnvironment(WebEnvironment environment)
environment
- Copyright © 2004–2020 The Apache Software Foundation. All rights reserved.