SSM 框架 Shiro 的实现

集成 Shiro 更多内容请参照:https://blog.csdn.net/Roobert_Chao/article/details/89891034

SSM 框架 Shiro 的实现

  1. 结合官网的步骤来写
  2. Shiro 一直支持 Spring Web 应用程序。在 Web 应用程序中,所有可通过 Shiro 访问的 Web 请求都必须通过主 Shiro 过滤器。此过滤器本身非常强大,允许基于任何 URL 路径表达式执行临时自定义过滤器链。
  3. Spring Boot 集成 Apache Shiro :https://blog.csdn.net/Roobert_Chao/article/details/89971828
  1. Web.xml 文件中定义 Shiro 的过滤器<filter> 以及 过滤器映射 <filter-mapping>。
    【注意】:Web.xml配置,Shiro 的 Filter 必须放在其他 Filter 之前。
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  2. applicationContent.xml 中,配置内容比较多,可以单独的引用出来 applicationContent-shiro.xml 。
    【realm】
    <bean id="myRealm" class="cn.chao.shiro.shiromyself.shiro.MyShiroRealm"></bean>
    【lifecycleBeanPostProcessor】
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
    【配置 Shiro 的 SecurityManager Bean】
     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="realm" ref="myRealm"/>
    </bean>
    
    【配置 ShiroFilter bean】
    [ 注意事项 ]:bean 的 id 必须 和 web.xml 文件中配置的 shiroFilter 的 name 一致
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 装配 securityManager -->
        <property name="securityManager" ref="securityManager"/>
        <!-- 配置登陆页面 -->
        <property name="loginUrl" value="/index.html"/>
        <!-- 登陆成功后的一面 -->
        <property name="successUrl" value="/success.html"/>
        <property name="unauthorizedUrl" value="/unauthorized.html"/>
        <!-- 具体配置需要拦截哪些 URL, 以及访问对应的 URL 时使用 Shiro 的默认的 Filter 进行拦截.  -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 配置登出: 使用 logout 过滤器 -->
                /logout = logout	退出拦截器
                /open/** = anon	匿名拦截器:拦截 路径是 open 的所有
                /user/** = roles[user]	用户拥有角色[user]:角色拦截器-->拦截所有的 /user/** 路径
                /admin/** = roles[admin]
                /** = authc	所有路径,都需要认证。
            </value>
        </property>
    </bean>
    
    【配置緩存管理器】
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!-- 加载 ehcache 的配置文件,文末 ehcache-shiro.xml 配置文件。-->
        <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcacheManager"/>
        <property name="transactionAware" value="true"/>
    </bean>
    <!-- cache注解,和spring-redis.xml中的只能使用一个 -->
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
    
  3. SpringMVC 的配置文件。

之前我们讲过。
Aop 切面编程,为什么 @Aspect 不能作用于 controller 层 # 三

Shiro 官网给出。如图红框所示内容,只需要将这两个 Bean 定义 添加到 applicationContent.xml 中,但是,现在需要将这两个 Bean 放在 springmvc.xml 文件中。
在这里插入图片描述
4. 编写 MyRealm.java 文件。
在这里插入图片描述
5. 编写 LoginController.java 文件。
在这里插入图片描述

文末的 ehcache.xml 文件配置详解 。

  1. ehcache.xml 配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="shiroCache" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
	updateCheck="false">
    <diskStore path="java.io.tmpdir" />
    <defaultCache
            maxElementsInMemory="10000"
            maxElementsOnDisk="0"
            eternal="true"
            overflowToDisk="true"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="0"
            diskSpoolBufferSizeMB="50"
            diskExpiryThreadIntervalSeconds="120"
			memoryStoreEvictionPolicy="LFU"/>
			
    <cache name="authorizationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
		</cache>           
  
    <cache name="authenticationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <!-- 缓存半小时 -->
    <cache name="halfHour" 
        maxElementsInMemory="10000"
        maxElementsOnDisk="100000" 
        eternal="false" 
        timeToIdleSeconds="1800"
        timeToLiveSeconds="1800" 
        overflowToDisk="false" 
        diskPersistent="false" />
</ehcache>
  1. ehcache.xml 配置文件的详情。
  • diskStore:为缓存路径,ehcache 分为内存和磁盘两级,此属性定义磁盘的缓存位置。
  • defaultCache:默认缓存策略,当 ehcache 找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
  • name:缓存名称。
  • maxElementsInMemory:缓存最大数目
  • eternal:对象是否永久有效,一但设置了,timeout 将不起作用。
  • maxElementsOnDisk:硬盘最大缓存个数。
  • overflowToDisk:是否保存到磁盘,当系统当机时
  • diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
  • timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  • timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存.
  • clearOnFlush:内存数量最大时是否清除。
  • memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
    • FIFO( first in first out )先进先出
    • LFU ( Less Frequently Used )一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
    • LRU( Least Recently Used )最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
  1. 缓存的依赖也要加载 pom.xml 中
<!--开启 cache 缓存-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache 缓存 -->
<dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值