shiro+SpringMVC集成

一.本文主要是本人对使用shiro方面的总结。重点在介绍shiro使用。

二.1.导入所需要的架包

maven导入

 
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-aspectj</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-ehcache</artifactId>
    <version>1.3.2</version>
</dependency>

2.shiro的xml配置

spring-context-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- Shiro生命周期处理器 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"> </bean>

    <!--配置权限管理器(核心)-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--自定义登录权限模块-->
        <property name="realm" ref="jdbcRealm"> </property>
        <!-- 使用下面配置的缓存管理器 -->
        <property name="cacheManager" ref="cacheManager"/>
    </bean>

    <!-- 进行缓存的操作配置 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
    </bean>

    <!-- 安全管理器 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!--&lt;!&ndash; 缓存管理器 使用Ehcache实现 &ndash;&gt;-->
    <!--<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">-->
        <!--<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />-->
    <!--</bean>-->

    <!-- 凭证匹配器 -->
    <bean id="credentialsMatcher"
          class="shiro.RetryLimitHashedCredentialsMatcher">
        <constructor-arg ref="cacheManager" />
        <property name="hashAlgorithmName" value="md5" />
        <!-- md5盐值加密3次 -->
        <property name="hashIterations" value="3" />
        <property name="storedCredentialsHexEncoded" value="true" />
    </bean>

    <!--&lt;!&ndash; 自定义的jdbcrealm &ndash;&gt;-->
    <!--<bean id="jdbcRealm" class="shiro.JdbcRealmCustom">-->
        <!--&lt;!&ndash; 凭证匹配器 &ndash;&gt;-->
        <!--&lt;!&ndash;<property name="credentialsMatcher" ref="credentialsMatcher" />&ndash;&gt;-->
        <!--&lt;!&ndash;<property name="cachingEnabled" value="true" />&ndash;&gt;-->
        <!--&lt;!&ndash; 如需要自定义缓存时间放开以下.修改 ehcache.xml &ndash;&gt;-->
        <!--<property name="authenticationCachingEnabled" value="true" />-->
        <!--<property name="authenticationCacheName" value="authenticationCache" />-->
        <!--<property name="authorizationCachingEnabled" value="true" />-->
        <!--<property name="authorizationCacheName" value="authorizationCache" />-->
    <!--</bean>-->

    <!--
     使用Shiro自带的JdbcRealm类
     指定密码匹配所需要用到的加密对象
     指定存储用户、角色、权限许可的数据源及相关查询语句
    -->
    <!--<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">-->
        <!--<property name="credentialsMatcher">-->
            <!--<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">-->
                <!--&lt;!&ndash; 加密算法的名称 &ndash;&gt;-->
                <!--<property name="hashAlgorithmName" value="MD5"> </property>-->
                <!--&lt;!&ndash; 配置加密的次数 &ndash;&gt;-->
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值