content: package com.smart.common.shiro;
  
  
  import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
  import org.apache.shiro.codec.Base64;
  import org.apache.shiro.mgt.SecurityManager;
  import org.apache.shiro.mgt.SessionsSecurityManager;
  import org.apache.shiro.spring.LifecycleBeanPostProcessor;
  import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
  import org.apache.shiro.web.mgt.CookieRememberMeManager;
  import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  import org.apache.shiro.web.servlet.SimpleCookie;
  import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
  import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.DependsOn;
  
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.servlet.Filter;
  import java.security.NoSuchAlgorithmException;
  import java.util.LinkedHashMap;
  import java.util.Map;
  
  /**
   * Shiro权限配置
   * 爪哇笔记：https://blog.52itstyle.vip
   * @author 小柒2012
   */
  @Configuration
  public class ShiroConfig {
  
      @Bean
      public UserRealm userRealm() {
          return new UserRealm();
      }
  
      @Bean
      public ShiroFilterFactoryBean shiroFilterFactoryBean (SecurityManager securityManager) {
          ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
          shiroFilterFactoryBean.setSecurityManager(securityManager);
          shiroFilterFactoryBean.setLoginUrl("/login.html");
          shiroFilterFactoryBean.setUnauthorizedUrl("/403");
          Map<String, Filter> filtersMap = new LinkedHashMap<>();
          shiroFilterFactoryBean.setFilters(filtersMap);
          Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
          /**
           * 静态文件
           */
          filterChainDefinitionMap.put("/css/**","anon");
          filterChainDefinitionMap.put("/lib/**","anon");
          filterChainDefinitionMap.put("/images/**","anon");
          filterChainDefinitionMap.put("/js/**","anon");
          filterChainDefinitionMap.put("/file/**","anon");
  
          /**
           * 登录注册
           */
          filterChainDefinitionMap.put("/login.html","anon");
          filterChainDefinitionMap.put("/sys/logout","anon");
          filterChainDefinitionMap.put("/sys/login","anon");
          filterChainDefinitionMap.put("/sys/captcha","anon");
  
          /**
           * 管理后台
           */
          filterChainDefinitionMap.put("/**", "user");
          shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
          return shiroFilterFactoryBean;
      }
  
      @Bean
      public SessionsSecurityManager securityManager() {
          DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
          securityManager.setRealm(userRealm());
          securityManager.setSessionManager(sessionManager());
          securityManager.setRememberMeManager(rememberMeManager());
          return securityManager;
      }
  
      @Bean
      public DefaultWebSessionManager sessionManager() {
          DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
          sessionManager.setSessionIdUrlRewritingEnabled(false);
          long time = 2*60*60*1000;
          sessionManager.setGlobalSessionTimeout(time);
          return sessionManager;
      }
  
      @Bean
      public ShiroDialect shiroDialect(){
          return new ShiroDialect();
      }
  
      @Bean
      public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
          return new LifecycleBeanPostProcessor();
      }
  
      @Bean
      @DependsOn("lifecycleBeanPostProcessor")
      public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
          DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
          defaultAdvisorAutoProxyCreator.setUsePrefix(true);
          return defaultAdvisorAutoProxyCreator;
      }
  
      /**
       * cookie管理对象
       * @return
       */
      @Bean
      public CookieRememberMeManager rememberMeManager(){
          CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
          cookieRememberMeManager.setCookie(rememberMeCookie());
          /**
           * 生产环境一定要自行修改秘钥
           */
          cookieRememberMeManager.setCipherKey(Base64.decode("5AvVhmFLUs0KTA3Kprsdag=="));
          return cookieRememberMeManager;
      }
  
      @Bean
      public SimpleCookie rememberMeCookie(){
          SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
          simpleCookie.setHttpOnly(true);
          simpleCookie.setPath("/");
          simpleCookie.setMaxAge(259200);
          return simpleCookie;
      }
  
      /**
       * 秘钥生成方式
       * @param args
       * @throws NoSuchAlgorithmException
       */
      public static void main(String[] args) throws NoSuchAlgorithmException {
          // 可通过以下代码生成对应Key
          KeyGenerator keygen = KeyGenerator.getInstance("AES");
          SecretKey aesKey = keygen.generateKey();
          System.out.println(org.apache.shiro.codec.Base64.encodeToString(aesKey.getEncoded()));
      }
  }