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

  • 相关阅读:
    spoj694 DISUBSTR
    poj3261 Milk Patterns
    poj1743 Musical Theme
    Hdu1403 Longest Common Substring
    bzoj3779 重组病毒
    bzoj3083 遥远的国度
    bzoj3514 Codechef MARCH14 GERALD07加强版
    PAT 乙级 1027 打印沙漏(20) C++版
    PAT 乙级 1060 爱丁顿数(25) C++版
    windows编程之窗口抖动
  • 原文地址:https://www.cnblogs.com/starmoon1994/p/9302015.html
Copyright © 2011-2022 走看看