欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Apache Shiro 使用手册(五) Shiro 配置说明

程序员文章站 2023-10-28 16:15:28
apache shiro的配置主要分为四部分: 对象和属性的定义与配置url的过滤器配置静态用户配置静态角色配置其中,由于用户、角色一般由后台进行操作的动态数据,因此sh...

apache shiro的配置主要分为四部分:

对象和属性的定义与配置
url的过滤器配置
静态用户配置
静态角色配置
其中,由于用户、角色一般由后台进行操作的动态数据,因此shiro配置一般仅包含前两项的配置。

apache shiro的大多数组件是基于pojo的,因此我们可以使用pojo兼容的任何配置机制进行配置,例如:java代码、sping xml、yaml、json、ini文件等等。下面,以spring xml的配置方式为例,并且对其中的一些配置参数进行一些简单说明。

shiro对象的配置:
主要是对shiro各个组件的实现进行定义配置,主要组件在前文已做过简单介绍,这里不再一一说明。

复制代码 代码如下:

<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>

shiro过滤器的配置
shiro主要是通过url过滤来进行安全管理,这里的配置便是指定具体授权规则定义。

复制代码 代码如下:

<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过滤器配置说明:

shiro可以通过配置文件实现基于url的授权验证。filterchain定义格式:
url_ant_path_expression = path_specific_filter_chain
每个url配置,表示匹配该url的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/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 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