package me.chyxion.tigon.shiro;

import javax.servlet.Filter;
import static javax.servlet.DispatcherType.*;
import org.springframework.context.annotation.Bean;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Mar 06, 2017 22:40:11
 */
@Configuration
public class TigonShiroConfig {

    @Bean
    public FilterRegistrationBean someFilterRegistration(
        @Qualifier("shiroFilter") ShiroFilterFactoryBean filterFactoryBean) throws Exception {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setAsyncSupported(true);
        registration.addUrlPatterns("/*");
        registration.addInitParameter("targetFilterLifecycle", "true");
        registration.setEnabled(true);
        registration.setName("shiroFilter");
        registration.setOrder(Integer.MAX_VALUE);
        // registration.setOrder(0);
        registration.setDispatcherTypes(REQUEST, FORWARD, INCLUDE, ERROR);

        DelegatingFilterProxy filterProxy =
            new DelegatingFilterProxy((Filter) filterFactoryBean.getObject());
        filterProxy.setTargetFilterLifecycle(true);
        filterProxy.setTargetBeanName("shiroFilter");

        registration.setFilter(filterProxy);
        return registration;
    }
}
