package com.qmth.teachcloud.report.business.filter;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

/**
 * @Description: 加入web.xml filter
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/12/30
 */
@Configuration
public class MyWebApplicationInitializer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext sc) {
        sc.addListener(new SingleSignOutHttpSessionListener());
        FilterRegistration.Dynamic encodingFilter = sc.addFilter("encoding-filter", CharacterEncodingFilter.class);
        encodingFilter.setInitParameter("encoding", "UTF-8");
        encodingFilter.addMappingForUrlPatterns(null, false, "/*");

        FilterRegistration.Dynamic CASSingleSignOutFilter = sc.addFilter("CAS Single Sign Out Filter", SingleSignOutFilter.class);
        CASSingleSignOutFilter.addMappingForUrlPatterns(null, false, "/*");

        FilterRegistration.Dynamic CASFilter = sc.addFilter("CASFilter", AuthenticationFilter.class);
        CASFilter.setInitParameter("casServerLoginUrl", "http://cas.whu.edu.cn/authserver/login");
        CASFilter.setInitParameter("serverName", "https://ggjckcjfx.whu.edu.cn");
        CASFilter.addMappingForUrlPatterns(null, false, "/api/report/wuda/open/authentication/*", "/api/report/wuda/open/user/authentication/*");

        FilterRegistration.Dynamic CASValidationFilter = sc.addFilter("CAS Validation Filter", Cas20ProxyReceivingTicketValidationFilter.class);
        CASValidationFilter.setInitParameter("casServerUrlPrefix", "http://cas.whu.edu.cn/authserver");
        CASValidationFilter.setInitParameter("serverName", "https://ggjckcjfx.whu.edu.cn");
        CASValidationFilter.setInitParameter("encoding", "UTF-8");
        CASValidationFilter.addMappingForUrlPatterns(null, false, "/api/report/wuda/open/authentication/*", "/api/report/wuda/open/user/authentication/*");

        FilterRegistration.Dynamic CASHttpServletRequestWrapperFilter = sc.addFilter("CAS HttpServletRequest Wrapper Filter", HttpServletRequestWrapperFilter.class);
        CASHttpServletRequestWrapperFilter.addMappingForUrlPatterns(null, false, "/*");
    }
}
