Apache Shiro 使用手册(五)Shiro 配置说明
Apache Shiro的配置主要分为四部分:[*]对象和属性的定义与配置
[*]URL的过滤器配置
[*]静态用户配置
[*]静态角色配置
其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含前两项的配置。
Apache Shiro的大多数组件是基于POJO的,因此我们可以使用POJO兼容的任何配置机制进行配置,例如:Java代码、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式为例,并且对其中的一些配置参数进行一些简单说明。
Shiro对象的配置:
主要是对Shiro各个组件的实现进行定义配置,主要组件在前文已做过简单介绍,这里不再一一说明。
Xml代码 http://kdboy.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%3Cbean%20id%3D%22securityManager%22%20class%3D%22org.apache.shiro.mgt.DefaultSecurityManager%22%3E%0A%20%20%20%20%20%20%20%20%3Cproperty%20name%3D%22cacheManager%22%20ref%3D%22cacheManager%22%2F%3E%0A%20%20%20%20%20%20%20%20%3Cproperty%20name%3D%22sessionMode%22%20value%3D%22native%22%2F%3E%0A%20%20%20%20%20%20%20%20%3C!--%20Single%20realm%20app.%20%20If%20you%20have%20multiple%20realms%2C%20use%20the%20
[*]
[*]
[*]
[*]
[*]
[*]
[*]
Shiro过滤器的配置
Shiro主要是通过URL过滤来进行安全管理,这里的配置便是指定具体授权规则定义。
Xml代码 http://kdboy.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%3Cbean%20id%3D%22shiroFilter%22%20class%3D%22org.apache.shiro.spring.web.ShiroFilterFactoryBean%22%3E%0A%20%20%20%20%3Cproperty%20name%3D%22securityManager%22%20ref%3D%22securityManager%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22loginUrl%22%20value%3D%22%2Flogin.jsp%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22successUrl%22%20value%3D%22%2Fhome.jsp%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22unauthorizedUrl%22%20value%3D%22%2Funauthorized.jsp%22%2F%3E%20--%3E%0A%20%20%20%20%3Cproperty%20name%3D%22filterChainDefinitions%22%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20some%20example%20chain%20definitions%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2Fadmin%2F**%20%3D%20authc%2C%20roles%5Badmin%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2Fdocs%2F**%20%3D%20authc%2C%20perms%5Bdocument%3Aread%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F**%20%3D%20authc%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20more%20URL-to-FilterChain%20definitions%20here%0A%20%20%20%20%20%20%20%20%3C%2Fvalue%3E%0A%20%20%20%20%3C%2Fproperty%3E%0A%3C%2Fbean%3E
[*]
[*]
[*]
[*]
[*] -->
[*]
[*]
[*] # some example chain definitions:
[*] /admin/** = authc, roles
[*] /docs/** = authc, perms
[*] /** = authc
[*] # more URL-to-FilterChain definitions here
[*]
[*]
[*]
URL过滤器配置说明:
Shiro可以通过配置文件实现基于URL的授权验证。FilterChain定义格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。
例如:
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
1、URL目录是基于HttpServletRequest.getContextPath()此目录设置
2、URL可使用通配符,**代表任意子目录
3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。
Filter Chain定义说明
1、一个URL可以配置多个Filter,使用逗号分隔
2、当设置多个过滤器时,全部验证通过,才视为通过
3、部分过滤器可指定参数,如perms,roles
Shiro内置的FilterChain
Filter NameClassanonorg.apache.shiro.web.filter.authc.AnonymousFilterauthcorg.apache.shiro.web.filter.authc.FormAuthenticationFilterauthcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilterpermsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilterportorg.apache.shiro.web.filter.authz.PortFilterrestorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilterrolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFiltersslorg.apache.shiro.web.filter.authz.SslFilteruserorg.apache.shiro.web.filter.authc.UserFilter
页:
[1]