shiro 之 Cache

本文介绍了Shiro的Cache功能,包括其作为Shiro组件间缓存服务的概念、应用场景,如Realm和Session管理。Shiro的Cache设计允许使用各种缓存产品作为后端,如ehcache或Redis。在Realm中,认证和授权信息会被缓存,提高性能。在Session管理中,通过配置CacheManager,可以优化Session的存储和检索。总之,Shiro Cache提供了灵活的缓存解决方案。
摘要由CSDN通过智能技术生成

shiro 之 Cache

本节将学习以一下Shiro 的核心功能之一 Cache。关于Shiro 的 Cache 我再次先声明一下下:Shiro 的Cache 的服务范围一般在Shiro 的服务内。关于缓存我们之前有学习过Redis,在一些Application中我们也是用到了 Redis作为缓存来处理数据。因而我们可以得出一个结论:其他缓存可以作为 Shiro 的缓存代理,但是Shiro 自身的缓存(比如ehcache)只能服务于Shiro(比如认证授权时的Subject和一些Session等。)


概念

  • Shiro Cache

    • 官方介绍

      • The CacheManager creates and manages Cache instance lifecycles used by other Shiro components. Because Shiro can access many back-end data sources for authentication, authorization and session management, caching has always been a first-class architectural feature in the framework to improve performance while using these data sources. Any of the modern open-source and/or enterprise caching products can be plugged in to Shiro to provide a fast and efficient user-experience.
    • 通常而言

      • Shiro提供了类似于Spring的Cache抽象,即Shiro本身不实现Cache,但是对Cache进行了又抽象,方便更换不同的底层Cache实现。Shiro Cache 常用于Authentication、Authorization和SessionManagement。

Shiro 提供了三种 Cache Abstract Interface

  • Cache

    package org.apache.shiro.cache;
    import java.util.Collection;
    import java.util.Set;
    
    public interface Cache<K, V> {
    
        public V get(K key) throws CacheException;
    
        public V put(K key, V value) throws CacheException;
    
        public V remove(K key) throws CacheException;
    
        public void clear() throws CacheException;
    
        public int size();
    
        public Set<K> keys();
    
        public Collection<V> values();
    }
    
  • CacheManager

    public interface CacheManager {
    
        public <K, V> Cache<K, V> getCache(String name) throws CacheException;
    }
  • CacheManagerAware: 将CacheManagerAware用于注入CacheManager

这是一个shiro的入门Demo.. 使用了Spring MVC,mybaits等技术.. 数据库设计 : User : name--password Role : id--userid--roleName Function : id--userid--url tinys普通用户只能访问index.jsp admin用户通过添加了admin的permission,所以可以访问admin.jsp role用户通过添加了role角色,所以可以访问role.jsp 这是最基本的shiro的运用..目的是让你快速了解shiro的机制.. 这个Demo体现shiro的地方主要在两个类以及shiro.xml的配置文件 CustomRealm : 处理了登录验证以及授权.. ShiroAction : 用来传递登录时的用户数据..转换为token传递给realm...之后根据结果做相应的逻辑处理.. shiro.xml : shiro的主要配置... 规则定义在以下地方 : /login.jsp* = anon /index.jsp* = authc /index.do* = authc /admin.jsp*=authc,perms[/admin] /role.jsp*=authc,roles[role] ------------------------------------------------------------------------------------------------------------------------------------------------------------- 2015-10-28更新 --通过添加了以下内容来使用注解方式配置权限.... unauth login --修改了过滤链 //简单的讲就是把需要特别处理的路径写到前面,越特殊写到越前 /shiro/login.do*=anon /login.jsp* = anon /admin.jsp*=authc,perms[/admin] /role.jsp*=authc,roles[role] /** = authc --------------------------------------------------------------------------------------------------------------------------------------------------- 15-10-29 添加了使用ehcache缓存机制 添加了redis缓存...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值