package com.gz.config;

import com.gz.filter.AuthFilter;
import com.gz.filter.CorsFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.AntPathMatcher;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 过滤器配置
 * @author LiuchangLan
 * @date 2021/1/15 13:28
 */
@Configuration
@Slf4j
public class FilterConfig {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${auth.login.force-urls}")
    private String forceUrls;

    @Value("${auth.login.skip-urls}")
    private String skipUrls;



    @Bean
    public FilterRegistrationBean registCorsFilter(){
        log.info("注册跨域过滤器");
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CorsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("crosFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean registAuthFilter() {
        log.info("注册登录验证过滤器");
        FilterRegistrationBean registration = new FilterRegistrationBean();
        AuthFilter authFilter = new AuthFilter();
        authFilter.setStringRedisTemplate(stringRedisTemplate);
        authFilter.setSkipUrls(skipUrls);
        authFilter.setForceUrls(forceUrls);
        registration.setFilter(authFilter);
        registration.addUrlPatterns("/*");
        registration.setName("authFilter");
        registration.setOrder(2);
        return registration;
    }

}

