package cn.com.victorysoft.business.config;

import cn.com.victorysoft.business.filter.CorsFilter;
import cn.com.victorysoft.business.filter.JwtFilter;
import com.sinopec.siam.agent.web.AccessEnforcer;
import com.sinopec.siam.agent.web.SAMLProfileFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangyong
 */
@Configuration
public class AuthFilterConfig {

    /**
     * 跨域
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> registerCorsFilter() {
        FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CorsFilter());
        registration.setEnabled(true);
        registration.addUrlPatterns("*");
        registration.setOrder(0);
        return registration;
    }

    /**
     * 中石化统一认证
     */
    @Bean
    public FilterRegistrationBean<SAMLProfileFilter> registerSAMLProfileFilter() {
        FilterRegistrationBean<SAMLProfileFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new SAMLProfileFilter());
        registration.setEnabled(true);
        registration.addUrlPatterns("/SSO/*");
        registration.setOrder(1);
        return registration;
    }

    /**
     * 中石化统一认证 - 登录
     */
    @Bean
    public FilterRegistrationBean<AccessEnforcer> registerAccessEnforcer() {
        FilterRegistrationBean<AccessEnforcer> registration = new FilterRegistrationBean<>();
        registration.setFilter(new AccessEnforcer());
        // 下面的 param-value 内容根据实际情况进行修改，修改后的内容不被 filter 拦截
        registration.addInitParameter("noFilterURLs", "");
        registration.setEnabled(true);
        // 下面的 url-pattern 内容根据实际情况进行修改，修改后的内容被 filter拦截，跳转到 IDP
        registration.addUrlPatterns("/siam", "/");
        registration.setOrder(2);
        return registration;
    }

    /**
     * JWT过滤器
     */
    @Bean
    public FilterRegistrationBean<JwtFilter> registerJwtFilter() {
        FilterRegistrationBean<JwtFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new JwtFilter());
        // 统一认证入口、模型训练回调不拦截
        registration.addInitParameter("noFilterUrls", "/siam");
        registration.setEnabled(true);
        registration.addUrlPatterns("*");
        registration.setOrder(3);
        return registration;
    }
}
