zoukankan      html  css  js  c++  java
  • 2522-Shiro系列--使用缓存对认证session和授权Cache进行存储

    如何进行session的缓存?

    原理:

    Shiro有1个类,AuthorizingRealm AuthenticatingRealm,里面有个获取认证信息的方法,
    AuthenticatingRealm getAuthenticationInfo;getAuthenticationInfo方法中

       public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
            AuthenticationInfo info = getCachedAuthenticationInfo(token);
            if (info == null) {
                //otherwise not cached, perform the lookup:
                info = doGetAuthenticationInfo(token);
                log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
                if (token != null && info != null) {
                    cacheAuthenticationInfoIfPossible(token, info);
                }
            } else {
                log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
            }
    
            if (info != null) {
                assertCredentialsMatch(token, info);
            } else {
                log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);
            }
    
            return info;
        }
    

    先获取缓存认证信息AuthenticationInfo

    • 如果info为空,就调用doGetAuthenticationInfo去取认证信息,并调用cacheAuthenticationInfoIfPossible去缓存该认证信息。
    • 如果缓存信息不为空,就进行token和认证信息的比对,然后返回info
    实现

    Shiro提供了对缓存操作的接口AbstractSessionDAO,只需实现该接口,对缓存进行操作,底层的缓存库是哪个库都可以,这里使用的是MongoDB。

    假设实现类是ShiroMongoSessionDao,只需在DefaultWebSessionManager中注入,然后将其注入到SecurityManager即可。

    参考代码:

        /**
         * shiro session的管理
         */
        @Bean
        public DefaultWebSessionManager sessionManager() {
    
            DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    
            // 注入自定义sessionDao操作的实现类
            sessionManager.setSessionDAO(shiroMongoSessionDao);
    
            // 设置安全cookie的名字为g_s和过期时间 此Cookie是shiro提供的规范
            sessionManager.setSessionIdCookieEnabled(true);
            SimpleCookie simpleCookie = new SimpleCookie();
            simpleCookie.setName("g_s");
            simpleCookie.setMaxAge(60 * 60 * 24 * 30);
            sessionManager.setSessionIdCookie(simpleCookie);
            sessionManager.setGlobalSessionTimeout(60 * 60 * 24 * 30 * 1000);
    
            return sessionManager;
        }
        
        @Bean
        public SecurityManager securityManager() {
            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
            // 自定义缓存session和cache的实现 使用redis和mongoDB皆可
            securityManager.setCacheManager(shiroMongoCacheManager);
            securityManager.setSessionManager(sessionManager());
            securityManager.setRealm(myShiroRealm);
            return securityManager;
        }
    

    如何进行授权信息Cache的缓存?

    原理:
    Shiro有1个类,AuthorizingRealm ,里面有个获取授权信息的方法,
    AuthorizingRealm getAuthorizationInfo

    基本原理和session缓存类似

    代码参考地址

    https://github.com/starmoon1994/shiro-collection

  • 相关阅读:
    青蛙学Linux—MySQL常用命令(二)
    青蛙学Linux—MySQL常用命令(一)
    青蛙学Linux—MySQL安装和初始化
    青蛙学Linux—MySQL
    青蛙学Linux—Apache提供HTTPS服务
    青蛙学Linux—Apache配置PHP支持
    青蛙学Linux—Apache+Tomcat实现动静分离以及负载均衡
    青蛙学Linux—Apache负载均衡的实现
    青蛙学Linux—Apache反向代理的实现
    青蛙学Linux—Apache的MPM模式和httpd-mpm.conf
  • 原文地址:https://www.cnblogs.com/starmoon1994/p/9302015.html
Copyright © 2011-2022 走看看