package cn.com.victorysoft.business.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.HashMap;
import java.util.Map;

/**
 * Filter配置
 *
 * @author stareast
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean parameterFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new ParameterFilter());
        registration.addUrlPatterns("*");
        registration.setName("parameterFilter");
        registration.setOrder(2);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("scriptRegx", "<|>|=|\"|and");
        initParameters.put("sqlRegx", "'|;|or|--|(|)");
        initParameters.put("notProtect", "");
        registration.setInitParameters(initParameters);
        return registration;
    }

//    @Bean
    public FilterRegistrationBean referFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new ReferFilter());
        registration.addUrlPatterns("*");
        registration.setName("referFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean ParamFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new ParamFilter());
        registration.addUrlPatterns("*");
        registration.setName("paramFilter");
        registration.setOrder(2);
        return registration;
    }
}
