package com.qianhe.web.controller.config;

import com.sinopec.siam.agent.web.AccessEnforcer;
import com.sinopec.siam.agent.web.SAMLProfileFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

//*
//* @Title: 统一认证配置管理
//* @author wamgqi
//* @date 2022年07月26


@Configuration
public class SPConfig {
	@Bean
	public ServletContextInitializer initializer() {
	    return new ServletContextInitializer() {

	        @Override
	        public void onStartup(ServletContext servletContext) throws ServletException {
	            servletContext.setInitParameter("spSimpleConfigFile", "classpath:/conf/sp-simple-config.properties");
	        }
	    };
	}

	@Bean
	public FilterRegistrationBean<SAMLProfileFilter> registerSPFilter() {
		FilterRegistrationBean<SAMLProfileFilter> bean = new FilterRegistrationBean<>();
		System.err.println("FilterRegistrationBean");
        System.err.println("开始注册SAMLProfileFilter");
		bean.setDispatcherTypes(DispatcherType.REQUEST);
		bean.setFilter(new SAMLProfileFilter());
		bean.addUrlPatterns("/SSO/*");
		bean.setName("SAMLProfileFilter");
		bean.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        Map<String, String> initParameters = new HashMap<String, String>();
        bean.setInitParameters(initParameters);
		return bean;
	}

	@Bean
	public FilterRegistrationBean<AccessEnforcer> registerMergeFilter() {
		FilterRegistrationBean<AccessEnforcer> bean = new FilterRegistrationBean<>();
		System.err.println("AccessEnforcer");
        System.err.println("开始注册AccessEnforcer");
		bean.setDispatcherTypes(DispatcherType.REQUEST);
		bean.setFilter(new AccessEnforcer());
		bean.addUrlPatterns("/siamlogin");
		bean.setName("AccessEnforcerAuthen");
		bean.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("noFilterURLs", "/favicon.ico;/ruoyi.png;/html/*;/css/*;/docs/*;/fonts/*;/img/*;/ajax/*;/js/*;/zhimin/*;/captcha/captchaImage;;/common/*;/SSO/**;/profile/upload/*;/register/;/login;/loginauth;/noaccess;/api/**");
        bean.setInitParameters(initParameters);
		return bean;
	}
}
