shiro初始化资源和权限

1、身份验证相关的默认拦截器名

拦截器名拦截器类功能
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter基于表单的拦截器,如果没有登陆会跳转到相应的登录页面登录
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilterBasicHttp身份验证拦截器
logoutorg.apache.shiro.web.filter.authc.LogoutFilter登出拦截器,登出之后页面重定向
userorg.apache.shiro.web.filter.authc.UserFilter用户拦截器,用户已经身份认证/记住我都可访问
anonorg.apache.shiro.web.filter.authc.AnonymousFilter匿名拦截器,不需要登录即可访问,一般用于过滤静态资源

2、shiro配置filterChainDefinitions实现初始化页面权限保护

在正常情况下我们的资源和权限都是从数据库中读取出来的,而在shiro中通过配置文件中的ShiroFilterFactoryBean的filterChainDefinitions属性来配置各个页面的权限。

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/to_login"/>
        <property name="successUrl" value="/list"/>
        <!-- 未授权跳转页面 -->
        <property name="unauthorizedUrl" value="/unauthorized"/>
        <!--
            配置哪些页面需要受保护.(满足ant风格)
            以及访问这些页面需要的权限.
            1). anon 可以被匿名访问
            2). authc 必须认证(即登录)后才可能访问的页面.
            3). logout 登出.
            4). roles 角色过滤器
        -->
        <property name="filterChainDefinitions">
            <value>
                /login = anon
                /to_login = anon
                /logout = logout
                /unauthorized = anon

                /list = roles[user]
                /user = roles[user]
                /admin = roles[admin]

                /test = anon
                /testRequiresAuthentication = anon
                /testRequiresUser = anon
                /testRequiresGuest = anon
                /testRequiresRoles = anon
                /testRequiresPermissions = anon

                # everything else requires authentication:
                /** = authc
            </value>
        </property>

    </bean>

3、通过数据库初始化资源和权限的方式

1)创建一个可以其中有返回LinkedHashMap方法的实体类

public class FilterChainDefinitionMapBuilder {
    /**
     * 可以通过该方法从数据库中读出要保护的页面
     */
    public LinkedHashMap<String,String> filterChainDefinitionMapBuilder(){

        LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();
        //从数据库中读取的数据
        linkedHashMap.put("/login","anon");
        linkedHashMap.put("/to_login","anon");
        linkedHashMap.put("/logout","logout");
        linkedHashMap.put("/unauthorized","anon");
        linkedHashMap.put("/list","roles[user]");
        linkedHashMap.put("/user","roles[user]");
        linkedHashMap.put("/admin","roles[admin]");
        linkedHashMap.put("/test","anon");
        linkedHashMap.put("/testRequiresAuthentication","anon");
        linkedHashMap.put("/testRequiresUser","anon");
        linkedHashMap.put("/testRequiresGuest","anon");
        linkedHashMap.put("/testRequiresRoles","anon");
        linkedHashMap.put("/testRequiresPermissions","anon");
        linkedHashMap.put("/**","authc");
        return linkedHashMap;
    }
}

2)spring配置文件中配置如以下代码*号包括部分,之后将配置的类写入shiroFilter的filterChainDefinitionMap属性中(通过实例工厂的方式初始化)

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/to_login"/>
        <property name="successUrl" value="/list"/>
        <!-- 未授权跳转页面 -->
        <property name="unauthorizedUrl" value="/unauthorized"/>
        <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property>
    </bean>
*******************************************
    <bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="filterChainDefinitionMapBuilder">
    </bean>
	 <!-- 配置一个bean,该bean实际上是一个Map。通过实例工厂方法的方式 -->
    <bean class="com.shiro.test.factory.FilterChainDefinitionMapBuilder" id="filterChainDefinitionMapBuilder">
    </bean>
*******************************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值