
package com.ssm.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ssm.filter.MemberLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebAppConfig implements WebMvcConfigurer {
//
//    @Value("${server.port}")
//    private int serverPort;
//
//    @Value("${http.port}")
//    private int serverHttpPort;

    @Bean
    public MemberLoginInterceptor loginInterceptor(){
        return new MemberLoginInterceptor();
    }



    //
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(loginInterceptor()).addPathPatterns("/demay/ssm/**")
                .excludePathPatterns("/demay/ssm/memberCs/selectPage","/demay/ssm/staffLogin/phoneLogin","/demay/ssm/staffLogin/phonePassLogin","/demay/ssm/staffLogin/getSmsCode","/demay/ssm/staffLogin/logout",
                        "/demay/ssm/memberLogin/phoneLogin","/demay/ssm/memberLogin/phonePassLogin","/demay/ssm/memberLogin/getSmsCode","/demay/ssm/memberLogin/logout","/demay/ssm/memberCs/list","/demay/ssm/staffLogin/list","/demay/ssm/staffLogin/phonePassLoginNew",
                        "/demay/ssm/staffLogin/phonePassLoginNews","/demay/ssm/staffLogin/phonePassLogindzz","/demay/ssm/staffLogin/phoneLoginNew","/demay/ssm/staffLogin/phoneLoginNews","/demay/ssm/staffLogin/phoneLoginNewzz"
                ,"/demay/ssm/memberLogin/phonePassLogin","/demay/ssm/memberCs/selectByMember","/demay/ssm/img/*","/demay/ssm/memberLogin/memberUpload");

//        registry.addInterceptor(storeLoginInterceptor()).addPathPatterns("/demay/ssm/**")
//                .excludePathPatterns("/demay/ssm/staffLogin/phoneLogin","/demay/ssm/staffLogin/phonePassLogin","/demay/ssm/staffLogin/getSmsCode","/demay/ssm/staffLogin/logout");
//        super.addInterceptors(registry);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry){
//        registry.addInterceptor(loginInterceptor).addPathPatterns("/ssm/**").excludePathPatterns("/ssm/login/accountLogin","/ssm/login/phoneLogin","/ssm/login/getSmsCode");
//        super.addInterceptors(registry);
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedHeaders("*").allowedOrigins("*").maxAge(3600).allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS").allowCredentials(true);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//        registry.addResourceHandler("/image/**").addResourceLocations("file:D:/demay/file/**/");
//        super.addResourceHandlers(registry);
    }

    @Bean
    public MappingJackson2HttpMessageConverter toStringConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        mapper.registerModule(simpleModule);
        converter.setObjectMapper(mapper);
        return converter;
    }


//    /**
//     * Tomcat配置Https
//     * @return
//     */
//    @Bean
//    public TomcatServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory  tomcat = new TomcatServletWebServerFactory () {
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint securityConstraint = new SecurityConstraint();
//                securityConstraint.setUserConstraint("CONFIDENTIAL");
//                SecurityCollection collection = new SecurityCollection();
//                collection.addPattern("/*");
//                securityConstraint.addCollection(collection);
//                context.addConstraint(securityConstraint);
//            }
//        };
//
//        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
//        return tomcat;
//    }
//
//    /**
//     * 配置监听端口
//     */
//    private Connector initiateHttpConnector() {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        connector.setScheme("http");
//        //Connector监听的http的端口号
//        connector.setPort(serverHttpPort);
//        connector.setSecure(false);
//        //监听到http的端口号后转向到的https的端口号
//        connector.setRedirectPort(serverPort);
//        return connector;
//    }
}
