package com.xhkjedu.slive.interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author WN
 * @description
 * @date 2019/4/3 15:50
 **/
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Autowired
    private RequestInterceptor requestInterceptor;
    @Resource
    private HttpServletFilter httpServletFilter;

    /**
     * 拦截器addPathPatterns用于添加拦截规则 excludePathPatterns 用于排除拦截
     * @Param: registry
     * @return void
     * @author wn
     * @date 2019/4/8
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor).addPathPatterns("/**");
    }

    /**
     * 允许跨域
     * @Param: registry
     * @return void
     * @author wn
     * @date 2019/4/8
     */
    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET","POST")
                .maxAge(3600);
    }

    @Bean
    public FilterRegistrationBean registerFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(httpServletFilter);
        registration.addUrlPatterns("/*");
        registration.setOrder(1);
        return registration;
    }
}
