content: package nuc.bysjxtglxt.authentication;
  
  import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
  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.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  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;
  
  /**
   * Shiro 配置类
   *
   * @author jurui
   */
  @Configuration
  public class ShiroConfig {
      /**
       * 日志打印
       */
      private final static Logger log = LoggerFactory.getLogger(ShiroConfig.class);
  
  
      @Bean
      public HashedCredentialsMatcher hashedCredentialsMatcher() {
          HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
          hashedCredentialsMatcher.setHashAlgorithmName("md5");
          hashedCredentialsMatcher.setHashIterations(2);
          return hashedCredentialsMatcher;
      }
  
      @Bean
      public ShiroAuthRealm shiroAuthRealm() {
          ShiroAuthRealm shiroAuthRealm = new ShiroAuthRealm();
          shiroAuthRealm.setCredentialsMatcher(hashedCredentialsMatcher());
          return shiroAuthRealm;
      }
  
      /**
       * @desc: 配置核心安全事务管理
       *
       * @date: 2017/12/18
       */
      @Bean
      public SecurityManager securityManager() {
          DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
          securityManager.setRealm(shiroAuthRealm());
          log.info("========================shiro已经加载======================");
          return securityManager;
      }
  
      @Bean
      public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
          ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
          shiroFilterFactoryBean.setSecurityManager(securityManager);
          Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
  
  
          filterChainDefinitionMap.put("/nucUser/regist", "anon");
          filterChainDefinitionMap.put("/logout", "logout");
          filterChainDefinitionMap.put("/login", "anon");
          //测试环境下开放所有权限
          filterChainDefinitionMap.put("/**", "authc");
  
          shiroFilterFactoryBean.setLoginUrl("/login");
          shiroFilterFactoryBean.setUnauthorizedUrl("");
          shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
          return shiroFilterFactoryBean;
      }
      /**
       * 开启aop注解支持
       *
       * @param securityManager
       * @return
       */
      @Bean
      public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
          AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
          authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
          return authorizationAttributeSourceAdvisor;
      }
  
  
  
  }
  