Shiro actual combat series (IX) Web

1、 Configuration

The easiest way to integrate Shiro into any web application is on the web Configure contextlistener and filter in XML to understand how to read Shiro's ini configuration file. Most of the INI configuration formats are defined in the ini sections section of the configuration page, but here we will introduce some additional specific parts of the web.

Shiro 1.2 and later in Shiro 1.2 and later, standard web applications add the following XML block to the web XML to initialize Shiro

This assumes that a Shiro ini configuration file is in either of the following two locations and uses the one found first:

1. /WEB-INF/shiro. ini

2. In the classpath root directory, Shiro INI file

Here's what the above configuration does: 

(1) Environmentloaderlistener initializes a Shiro webenvironment instance (including all operations required by Shiro, including securitymanager) so that it can be accessed in the ServletContext. If you need to obtain a webenvironment instance at any time, you can call webutils.getrequiredwebenvironment (ServletContext).  if

(2) Shirofilter will use this web environment to perform all necessary security operations on any filtered requests. 

(3) Finally, the definition of filter mapping ensures that all requests are filtered by shirofilter, which is recommended for most web applications to ensure that any request is secure.

2、 Shirofilter mapping

It is usually desirable to define shirofilter filter mapping before any other filter mapping declaration to ensure that Shiro also works well under those filters.

3、 Custom webenvironment class

By default, environmentloaderlistener will create an instance of iniwebenvironment to render Shiro's INI file based configuration. If you like, you can do it on the web Specify a custom ServletContext context param in XML:

@H_ 404_ 45@

This allows you to customize a configuration format for how to parse and represent webenvironment instances. You can subclass the existing iniwebenvironment for custom behavior, or fully support different configuration formats. For example, if someone wants to configure Shiro in XML instead of ini, they can create an XML based implementation, such as com foo. bar. shiro. XmlWebEnviroment

4、 Custom configuration locations

The ini web environment will read and load the INI configuration file. By default, this class will automatically look for Shiro in the following two places Ini configuration (in order). 1. / WEB-INF / shiro.ini 2. Classpath: shiro.ini it will use the first found one. However, if you want to put your configuration in another location, you can specify the location with content param in web.xml.

By default, in ServletContext Param value can be parsed under the rules defined by getresource method. For example, / WEB-INF / some / path / Shiro ini。

However, you can also specify a specific file system, such as classpath or URL Location, by using the appropriate resource prefix supported by Shiro, such as: ·

(1) file://home/foobar/myapp/shiro.ini 

(2)classpath:com/foo/bar/shiro. ini  (3)url: http://confighost.mycompany.com/myapp/shiro.ini

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>