shiro

本次缓存使用ehcache

pom.xml
      <shiro.version>1.3.2</shiro.version>

      <!-- Shiro依赖包 -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-all</artifactId>
          <version>${shiro.version}</version>
      </dependency>


web.xml


开头加入spring文件。 spring-model.xml包括spring-model-shiro.xml  spring-model-ehcache.xml  spring-model.xml
<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:conf/spring-model*.xml</param-value>

</context-param>



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


spring-model-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" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <description>Shiro配置信息</description>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>

    <!--自定义Realm -->
    <bean id="customRealm" class="com.common.MyRealm">
        <!--启用缓存,默认 false-->
        <!--<property name="cachingEnabled" value="true"/>-->
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="MD5"></property> <!-- 加密算法的名称 -->
                <property name="hashIterations" value="1024"></property> <!-- 配置加密的次数 -->
            </bean>
        </property>
    </bean>

    <!-- shiro封装cacheManager -->
    <bean id="shiroCacheManager"  class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory" />
    </bean>
    <!-- Shiro 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="customRealm"></property>
        <property name="cacheManager" ref="shiroCacheManager"></property>
    </bean>

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"></property>
        <property name="loginUrl" value="/login"></property>
        <property name="filterChainDefinitions">
            <value>
                /callMBG = anon
                /test/** = anon
                /static/** = anon<!--资源路径-->
                /css/** = anon<!--资源路径-->
                /js/** = anon<!--资源路径-->
                /scripts/** = anon<!--资源路径-->
                /images/** = anon<!--资源路径-->
                /login/** = anon<!--登录相关,包含登录页、验证码、请求等-->
                /error/** = anon<!--错误提示页面-->
                /** = authc<!--其他所有请求都走认证和防止重复登录的过滤器-->
            </value>
        </property>
    </bean>

    <!-- 启用Shiro的注解 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor"/>

    <!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,
       并在必要时进行安全逻辑验证 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"></property>
    </bean>

</beans>

最关键部分,"com.common.MyRealm"

MyRealm.java
package com.common;

import com.dataSource.DataSourceEnum;
import com.dataSource.DataSourceHolder;
import com.entity.TPriResource;
import com.entity.TPriRole;
import com.entity.TPriUser;
import com.entity.TPriUserRoleRel;
import com.service.TPriResourceService;
import com.service.TPriRoleService;
import com.service.TPriUserRoleRelService;
import com.service.TPriUserService;
import com.util.JsonUtil;
import com.util.SessionUtil;
import com.util.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title:
 * @Description:
 * @Author:ChenZey
 * @Company:
 * @Create:2019-08-27 16:54
 * @Version:V1.0
 **/
public class MyRealm  extends AuthorizingRealm {
    protected Logger logger = LoggerFactory.getLogger(MyRealm.class);

    @Autowired
    private TPriUserService userService;
    @Autowired
    private TPriRoleService roleService;
    @Autowired
    private TPriResourceService tPriResourceService;
    @Autowired
    private TPriUserRoleRelService tPriUserRoleRelService;
//    @Autowired
//    private ResourceService resourceService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(SessionUtil.getRoles());//添加角色
        info.addStringPermissions(SessionUtil.getResources());//添加按钮资源
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        DataSourceHolder.setDataSources(DataSourceEnum.RASDATA.getKey());
        String userName = (String) token.getPrincipal();
        TPriUser user = userService.getByUserName(userName);
        if (user == null) {
            // 没找到帐号
            throw new UnknownAccountException();
        } else {
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, user.getPassword(), getName());
            System.out.println(JsonUtil.toJson(user));
            //session中保存用户信息
            SessionUtil.setAttribute("user", user);
            //保存机构信息
           /* Org org = orgService.getById(user.getOrgId());
            SessionUtil.setAttribute("org", org);*/
            //保存角色信息
            TPriUserRoleRel tPriUserRoleRel = tPriUserRoleRelService.userRole(user.getUserId());
            TPriRole tPriRole = roleService.tPriRole(tPriUserRoleRel.getRoleId());
            List<String> roleStrs = new ArrayList<>();
            roleStrs.add(tPriRole.getRoleCode());
            SessionUtil.setAttribute("roles",roleStrs );

            //保存资源信息
             List<TPriResource> resources = null;
            if ("admin".equals(user.getStaffName())){
                resources = tPriResourceService.getAll();
            }else{
                resources = tPriResourceService.getResourceListByUserId(user.getUserId());
            }
            List<String> resourceStrs = new ArrayList<>();
            for (TPriResource resource : resources) {
     //permission 页面使用name中值 与数据库中该按钮注册存储值需一致。用法:
     //  -----------------------------------------------
     //  <shiro:hasPermission name="ht-function-test">
     //    <div class="icon" id="delete" title="测试" onclick="deleteRole()">
     //       <a><img src="${contextPath}/static/images/delete.png"        
     //onmouseover="this.src='${contextPath}/static/images/delete_a.png'"  
     //onmouseout="this.src='${contextPath}/static/images/delete.png'" /><span>删除</span></a>
     //        </div>
     //   </shiro:hasPermission>
     //     -------------------------------------------------
                String permission = resource.getPermission();
                //3为资源表按钮类型资源  此处存储按钮权限,用来结合JSP使用标签来解决权限验证
                if ("3".equals(resource.getResourceType()) && StringUtils.isNotEmpty(permission)) {
                    resourceStrs.add(permission);
                }
            }
             SessionUtil.setAttribute("resources", resourceStrs);
            return authenticationInfo;
        }
    }
}

SessionUtil.java

package com.util;

import com.entity.TPriUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.util.List;

public class SessionUtil {
    /**
     * 获取Shiro 的 Session
     *
     * @return
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取Session中的属性
     *
     * @param attributeName
     * @return
     */
    public static Object getAttribute(String attributeName) {
        return SecurityUtils.getSubject().getSession().getAttribute(attributeName);
    }

    /**
     * 设置Session属性
     *
     * @param attributeName
     * @param attribute
     */
    public static void setAttribute(String attributeName, Object attribute) {
        SecurityUtils.getSubject().getSession().setAttribute(attributeName, attribute);
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public static TPriUser getUser() {
        return (TPriUser) getAttribute("user");
    }

    /**
     * 获取当前登录用户ID
     * @return
     */
    public static String getUserId() {
        //判断当前登录用户是否存在
        TPriUser user = getUser();
        if(user == null){
            return null;
        }
        return getUser().getUserId();
    }

    /**
     * 获取当前登录用户名
     * @return
     */
    public static String getUsername() {
        //判断当前登录用户是否存在
        TPriUser user = getUser();
        if(user == null){
            return null;
        }
        return getUser().getUserName();
    }


    /**
     * 获取当前登录用户IP
     * @return
     */
    public static String getIp() {
        return getSession().getHost();
    }


    /**
     * 返回当前用户的角色列表
     * @return
     */
    public static List<String> getRoles() {
        return (List<String>) getSession().getAttribute("roles");
    }

    /**
     * 返回当前用户的资源列表
     * @return
     */
    public static List<String> getResources() {
        return (List<String>) getSession().getAttribute("resources");
    }

}


spring-model-ehcache.xml 其中引用ehcache.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"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd
       ">

    <description>EhCache配置信息</description>
    <!--<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean>-->
    <!-- 启用缓存注解功能-->
    <cache:annotation-driven cache-manager="springCacheManager"/>
    <!-- spring 封装ehcache缓存管理器-->
    <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager"  ref="cacheManagerFactory"/>
    </bean>
    <!--缓存配置-->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <!--创建工具类ApplicationUtil,以便普通class根据bean id动态获取spring管理的bean-->
    <bean id="springContextHolder" class="com.common.spring.SpringContextHolder" />
    <bean id="applicationUtil" class="com.common.ehcache.EHCacheUtils"></bean>
</beans>

ehcache.xml

<?xml version="1.1" encoding="UTF-8"?>
<ehcache updateCheck="true" monitoring="autodetect" dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="2000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
    />

    <!--     <cache name="diskCache"
               maxEntriesLocalHeap="2000"
               eternal="false"
               timeToIdleSeconds="300"
               timeToLiveSeconds="0"
               overflowToDisk="false"
               statistics="true">
        </cache> -->

    <cache name="passwordRetryCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="0"
           overflowToDisk="false"
    >
    </cache>

    <cache name="authorizationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false"
    >
    </cache>

    <cache name="authenticationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false"
    >
    </cache>

    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false"
    >
    </cache>
</ehcache>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容