package org.springside.examples.quickstart.config;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.realm.AuthorizingRealm;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springside.examples.quickstart.service.account.AccountService;
import org.springside.examples.quickstart.service.account.ShiroDbRealm;
import org.apache.shiro.cache.ehcache.EhCacheManager;
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.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfiguration{

    private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();

    @Bean(name = "securityManager")
    @Autowired
    public DefaultWebSecurityManager getDefaultWebSecurityManager(AuthorizingRealm realm) {
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
        dwsm.setRealm(realm);
        dwsm.setCacheManager(getEhCacheManager());
        return dwsm;
    }

    @Bean(name = "realm")
    @Autowired
    public AuthorizingRealm getShiroRealm(@Qualifier("accountService") AccountService accountService) {
        ShiroDbRealm shiroDbRealm=new ShiroDbRealm();
        shiroDbRealm.setAccountService(accountService);
        return shiroDbRealm;
    }

    @Bean(name = "shiroFilter")
    @Autowired
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean
                .setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/");
        filterChainDefinitionMap.put("/login", "authc");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/static/**","anon");
        filterChainDefinitionMap.put("/api/**","anon");
        filterChainDefinitionMap.put("/register/**","anon");
        filterChainDefinitionMap.put("/admin/**","roles[admin]");
        filterChainDefinitionMap.put("/**","user");

        shiroFilterFactoryBean
                .setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean(name = "shiroEhcacheManager")
    public EhCacheManager getEhCacheManager() {
        EhCacheManager em = new EhCacheManager();
        em.setCacheManagerConfigFile("classpath:ehcache/ehcache-shiro.xml");
        return em;
    }

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

    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        daap.setProxyTargetClass(true);
        return daap;
    }

    @Bean
    @Autowired
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
        aasa.setSecurityManager(securityManager);
        return new AuthorizationAttributeSourceAdvisor();
    }



}

@@ -1040,18 +1040,19 @@
 guration
+ 
 %7B%0A
-
 %0A    pri
@@ -1180,39 +1180,24 @@
 tyManager%22)%0A
-    @Autowired%0A
     public D
@@ -1485,31 +1485,16 @@
 realm%22)%0A
-    @Autowired%0A
     publ
@@ -1531,37 +1531,8 @@
 alm(
-@Qualifier(%22accountService%22) 
 Acco
@@ -1593,17 +1593,19 @@
 oDbRealm
-=
+ = 
 new Shir
@@ -1740,31 +1740,16 @@
 ilter%22)%0A
-    @Autowired%0A
     publ
@@ -2283,24 +2283,25 @@
 /static/**%22,
+ 
 %22anon%22);%0A   
@@ -2340,24 +2340,25 @@
 t(%22/api/**%22,
+ 
 %22anon%22);%0A   
@@ -2406,16 +2406,17 @@
 ter/**%22,
+ 
 %22anon%22);
@@ -2465,16 +2465,17 @@
 min/**%22,
+ 
 %22roles%5Ba
@@ -2526,16 +2526,17 @@
 t(%22/**%22,
+ 
 %22user%22);
@@ -3358,23 +3358,8 @@
 ean%0A
-    @Autowired%0A
     
@@ -3691,15 +3691,15 @@
 ();%0A
+
     %7D%0A%0A%0A
 %0A%7D%0A
@@ -3695,11 +3695,10 @@
     %7D%0A%0A%0A
-%0A
 %7D%0A

improve it.