Apache Shiro User Manual (5) Shiro configuration instructions, apacheshiro
The configuration of Apache Shiro is mainly divided into four parts:
- Definition and configuration of objects and Properties
- URL filter configuration
- Static User Configuration
- Static role configuration
Among them, because the user and role are generally dynamic data operated by the background, Shiro configuration generally only contains the first two configurations.
Most components of Apache Shiro are based on POJO, so we can use any configuration mechanism compatible with POJO, such as Java code, Sping XML, YAML, JSON, and INI files. The following uses Spring XML configuration as an example to describe some configuration parameters.
Shiro object Configuration:
It mainly defines and configures the implementation of various Shiro components. The main components have been briefly introduced in the previous article and are not described here.
Xml Code
- <Bean id = "securityManager" class = "org. apache. shiro. mgt. DefaultSecurityManager">
- <Property name = "cacheManager" ref = "cacheManager"/>
- <Property name = "sessionMode" value = "native"/>
- <! -- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
- <Property name = "realm" ref = "myRealm"/>
- <Property name = "sessionManager" ref = "sessionManager"/>
- </Bean>
Configuration of Shiro Filter
Shiro mainly manages security through URL filtering. The configuration here is to specify the specific authorization rule definition.
Xml Code
- <Bean id = "shiroFilter" class = "org. apache. shiro. spring. web. ShiroFilterFactoryBean">
- <Property name = "securityManager" ref = "securityManager"/>
- <Property name = "loginUrl" value = "/login. jsp"/>
- <Property name = "successUrl" value = "/home. jsp"/>
- <Property name = "unauthorizedUrl" value = "/unauthorized. jsp"/> -->
- <Property name = "filterChainDefinitions">
- <Value>
- # Some example chain definitions:
- /Admin/** = authc, roles [admin]
- /Docs/** = authc, perms [document: read]
- /** = Authc
- # More URL-to-FilterChain definitions here
- </Value>
- </Property>
- </Bean>
URL filter configuration instructions:
Shiro can implement URL-based authorization Authentication through the configuration file. FilterChain Definition Format:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
Each URL configuration indicates that application requests matching the URL are verified by the corresponding filter.
For example:
[Urls]
/Index.html = anon
/User/create = anon
/User/** = authc
/Admin/** = authc, roles [administrator]
/Rest/** = authc, rest
/Remoting/rpc/** = authc, perms ["remote: invoke"]
URL expression description
1. the URL directory is set based on HttpServletRequest. getContextPath ().
2. Wildcards can be used for URLs. ** represents any subdirectory.
3. When Shiro verifies the URL, the matching will no longer be performed if the URL matches successfully. Pay attention to the URL sequence in the configuration file, especially when using wildcards.
Filter Chain Definition
1. Multiple filters can be configured for a URL, which are separated by commas (,).
2. When multiple filters are set, they are regarded as pass only when all the filters pass the verification.
3. Some filters can specify parameters, such as perms and roles.
Shiro built-in FilterChain
Filter Name |
Class |
Anon |
Org. apache. shiro. web. filter. authc. AnonymousFilter |
Authc |
Org. apache. shiro. web. filter. authc. FormAuthenticationFilter |
AuthcBasic |
Org. apache. shiro. web. filter. authc. BasicHttpAuthenticationFilter |
Perms |
Org. apache. shiro. web. filter. authz. PermissionsAuthorizationFilter |
Port |
Org. apache. shiro. web. filter. authz. PortFilter |
Rest |
Org. apache. shiro. web. filter. authz. HttpMethodPermissionFilter |
Roles |
Org. apache. shiro. web. filter. authz. RolesAuthorizationFilter |
Ssl |
Org. apache. shiro. web. filter. authz. SslFilter |
User |
Org. apache. shiro. web. filter. authc. UserFilter |