package com.sysu.admin.support.cfg;

import java.util.Map;

import org.apache.shiro.cache.MemoryConstrainedCacheManager;
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.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Maps;
import com.sysu.admin.support.shiro.ShiroAuthenRealm;

/**
 * @title ShiroConfig
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:01:25
 */
@Configuration
public class ShiroCfg {

    /**
     * 开启注解式 权限拦截
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * Aop
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    /**
     * 权限管理
     *
     * @return
     */
    @Bean
    public ShiroAuthenRealm myShiroRealm() {
        ShiroAuthenRealm realm = new ShiroAuthenRealm();
        return realm;
    }

    // /**
    // * 记住密码
    // *
    // * @return
    // */
    // @Bean
    // public SimpleCookie rememberMeCookie() {
    // SimpleCookie simpleCookie = new SimpleCookie("shiro-cookie");
    // simpleCookie.setHttpOnly(true);
    // simpleCookie.setMaxAge(60 * 60 * 24);// 单位(秒)1天
    // return simpleCookie;
    // }
    //
    // /**
    // * 记住密码（管理器）
    // *
    // * @return
    // */
    // @Bean
    // public CookieRememberMeManager cookieRememberMeManager() {
    // CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
    // cookieRememberMeManager.setCipherKey(Base64.decode("Z3VucwAAAAAAAAAAAAAAAA=="));
    // cookieRememberMeManager.setCookie(rememberMeCookie());
    // return cookieRememberMeManager;
    // }

    /**
     * 权限管理（管理器）
     *
     * @return
     */
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        securityManager.setCacheManager(new MemoryConstrainedCacheManager());
        // securityManager.setRememberMeManager(cookieRememberMeManager());
        return securityManager;
    }

    /**
     * 设置对应的过滤条件和跳转条件
     *
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> map = Maps.newConcurrentMap();
        // 登录
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 首页
        shiroFilterFactoryBean.setSuccessUrl("/admin");
        // 未授权
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        // 登出
        map.put("/logout", "logout");
        // 认证
        map.put("/**", "authc");
        // 匿名
        map.put("/admin/**", "anon");// 后台页面
        map.put("/api/**", "anon");// api请求
        map.put("/site/**", "anon");// api请求
        map.put("/reg/**", "anon");// 注册
        map.put("/static/**", "anon");// 静态资源
        map.put("/images/**", "anon");// 静态资源
        map.put("/geojson/**", "anon");// 静态资源
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }
}
