shiro认证缓存配置和使用

一、文章前言

shiro认证缓存是提高web系统性能的一个重要部分,不是经常变动的东西都可以缓存起来,这样就可以减少数据库的查询次数,提高系统的性能,在这里我主要是使用Ehcache来实现。

二、shiro配置文件配置缓存

<!--
安全管理器
shiro中提供了对认证和授权的缓存,shiro是默认开始授权缓存而关闭认证缓存的
在SecurityManager中需要cacheManager这个参数
-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="realm" ref="jdbcRealm"/>
        <property name="sessionManager" ref="sessionManager"/>
</bean>

<!--relam配置-->

<bean id="jdbcRealm" class="com.IFox.shiro.bean.ShiroRealm">
        <property name="cachingEnabled" value="true"/>
        <property name="authenticationCachingEnabled" value="true"/>
        <property name="authenticationCacheName" value="authenticationCache"/>
        <property name="authorizationCachingEnabled" value="true"/>
        <property name="authorizationCacheName" value="authorizationCache"/>
</bean>

<!--通过session会话来管理缓存,开启sessionIdCookieEnabled-->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="sessionIdCookieEnabled" value="true"/>
</bean>

<!--缓存管理器-->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>

</bean>

三、Ehcache配置

<?xml version="1.1" encoding="UTF-8"?>
<ehcache name="shirocache">

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

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


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

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

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

    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="10"
           overflowToDisk="false">
    </cache>
</ehcache>

四、认证授权Relam使用

public class ShiroRealm extends AuthenticatingRealm{

     /**
      * 1、 doGetAuthenticationInfo 获取认证消息 如果数据库中没有数据 返回null 如果正确 返回封装的对象
      * 2、 AuthenticationInfo 可以使用SimpleAuthenticationInfo 实现类 封装给你正确的用户名和密码
      * 3、 token参数 就是我们要认证的token
      *
      * */
     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
         //将token转换成userNamePasswordToken
         UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
         SimpleAuthenticationInfo info = null;
         //获取用户名即可
         String userName = token.getUsername();

         //查询数据库,是否存在指定用户名和密码的用户
         try {
             Class.forName("com.mysql.jdbc.Driver");
             String url = "jdbc:mysql:///ifoxMember";
             Connection connection = DriverManager.getConnection(url,"mysql","mysqladmin");
             String sql = "select * from shiro_User where userName = ?";
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             preparedStatement.setString(1, token.getUsername());
             ResultSet resultSet = preparedStatement.executeQuery();

             if (resultSet.next()) {
                 Object principal = userName;
                 Object credentials = resultSet.getString(3);
                 String relamName = this.getName();
                 info = new SimpleAuthenticationInfo(principal,credentials,relamName);

             } else {
                 throw new AuthenticationException();
             }
         } catch (ClassNotFoundException | SQLException e) {
             e.printStackTrace();
         }

         //如果查询到了,封装查询结果,返回给我们的调用

         //如果没有查到抛出一个异常
         return info;
     }

五、总结

使用shiro安全认证缓存不仅可以让我们方便的管理用户,还可以提高系统的性能减小对数据库的访问。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值