package com.sqx.config;

import com.sqx.modules.sys.oauth2.OAuth2Filter;
import com.sqx.modules.sys.oauth2.OAuth2Realm;
import icu.xuyijie.secureapi.model.SecureApiPropertiesConfig;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 *
 */
@Configuration
public class ShiroConfig {

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager, SecureApiPropertiesConfig secureApiPropertiesConfig) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter(secureApiPropertiesConfig));
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/my-check/**", "anon");
        filterMap.put("/app/wxPay/notifyJsApi", "anon");

        filterMap.put("/**/open/**", "anon");

        filterMap.put("/app/wxPay/notifyMp", "anon");
        filterMap.put("/app/wxPay/notify", "anon");
        filterMap.put("/app/aliPay/notifyApp", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/activity/**", "anon");
        filterMap.put("/banner/**", "anon");
        filterMap.put("/courseClassification/selectCourseClassification", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/sys/adminLogin", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/chatSocket/**", "anon");
        filterMap.put("/websocket/**", "anon");
        filterMap.put("/search/**", "anon");
        filterMap.put("/alioss/**","anon");
        filterMap.put("/adminerrand","anon");
        filterMap.put("/invite/**","anon");
        filterMap.put("/app/shoptype/**","anon");
        filterMap.put("/app/shop/**","anon");
        filterMap.put("/admin/shoptype/**","anon");
        filterMap.put("/ordersChat/**","anon");
        filterMap.put("/wbsocket/**","anon");
        filterMap.put("/shop/illegalType/**","anon");
        filterMap.put("/shop/ordersChat/**","anon");
        filterMap.put("/app/ordersChat/**","anon");
        filterMap.put("/timedtask/**","anon");
        filterMap.put("/admin/ordermessage/**","anon");
        filterMap.put("/admin/goodsShop/quickGoodsShop","anon");
        filterMap.put("/admin/goodsShop/selectGoodsShop","anon");
        filterMap.put("/admin/goodsShop/updateGoodsShop","anon");

        filterMap.put("/admin/goods/**","anon");
        filterMap.put("/selfGoodsRule/**","anon");
//        站点
        filterMap.put("/admin/riderStation/**","anon");
        //
        filterMap.put("/**", "oauth2");
        filterMap.put("/shop/ordersChat/selectShopAllUnreadCount", "oauth2");
        //filterMap.put("/app/userinfo","anon");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
