content: package com.orange.verify.adminweb.config;
  
  import com.orange.verify.adminweb.realm.CustomRealm;
  import org.apache.shiro.mgt.SecurityManager;
  import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
  import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
  import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
  import org.crazycake.shiro.RedisCacheManager;
  import org.crazycake.shiro.RedisManager;
  import org.crazycake.shiro.RedisSessionDAO;
  import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import java.util.LinkedHashMap;
  import java.util.Map;
  
  @Configuration
  public class ShiroConfig {
  
      @Value("${spring.redis.host}")
      private String redisHost;
  
      @Value("${spring.redis.port}")
      private int redisPort;
  
      @Value("${spring.redis.password}")
      private String redisPassword;
  
      @Value("${spring.redis.timeout}")
      private int redisTimeout;
  
      @Value("${spring.redis.expire}")
      private int redisExpire;
  
      @Bean
      public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
  
          ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  
          // 必须设置 SecurityManager
          shiroFilterFactoryBean.setSecurityManager(securityManager);
          //随便写个路径都行，因为开启了注解认证登陆权限了
          shiroFilterFactoryBean.setLoginUrl("/login");
          shiroFilterFactoryBean.setUnauthorizedUrl("/notRole");
  
          // 设置拦截器(不需要设置了，因为开启了注解了)
          Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
  
          shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
          return shiroFilterFactoryBean;
      }
  
      /**
       *  开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证
       * 配置以下两个bean(DefaultAdvisorAutoProxyCreator和AuthorizationAttributeSourceAdvisor)即可实现此功能
       * @return
       */
      @Bean
      public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
          DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
          advisorAutoProxyCreator.setProxyTargetClass(true);
          return advisorAutoProxyCreator;
      }
  
      /**
       * 开启aop注解支持
       * @param securityManager
       * @return
       */
      @Bean
      public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
          AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
          authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
          return authorizationAttributeSourceAdvisor;
      }
  
      /**
       * 注入 securityManager
       */
      @Bean
      public SecurityManager securityManager() {
          DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
          // 设置realm.
          securityManager.setRealm(customRealm());
  
          securityManager.setCacheManager(cacheManager());
  
          securityManager.setSessionManager(sessionManager());
  
          return securityManager;
      }
  
      public RedisCacheManager cacheManager() {
          RedisCacheManager redisCacheManager = new RedisCacheManager();
          redisCacheManager.setRedisManager(redisManager());
          return redisCacheManager;
      }
  
      public RedisManager redisManager() {
  
          RedisManager redisManager = new RedisManager();
          redisManager.setExpire(redisExpire);
          redisManager.setHost(redisHost);
          redisManager.setPassword(redisPassword);
          redisManager.setTimeout(redisTimeout);
          redisManager.setPort(redisPort);
  
          return redisManager;
      }
  
      @Bean
      public DefaultWebSessionManager sessionManager() {
          DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
          sessionManager.setSessionDAO(redisSessionDAO());
          return sessionManager;
      }
  
      @Bean
      public RedisSessionDAO redisSessionDAO() {
          RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
          redisSessionDAO.setRedisManager(redisManager());
          return redisSessionDAO;
      }
  
      /**
       * 自定义身份认证 realm;
       */
      @Bean
      public CustomRealm customRealm() {
          return new CustomRealm();
      }
  
  }
  