package com.zhiqiyun.open.config;

import com.zhiqiyun.open.utils.request.ReplaceStreamFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> encodingFilter() {
        FilterRegistrationBean<CharacterEncodingFilter> filterRegistration = new FilterRegistrationBean<>();
        filterRegistration.setFilter(new CharacterEncodingFilter());
        filterRegistration.addUrlPatterns("/*");
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("encoding", "UTF-8");
        initParameters.put("forceEncoding", "true");
        filterRegistration.setInitParameters(initParameters);
        filterRegistration.setName("encodingFilter");
        return filterRegistration;
    }

    /**
     * 注册过滤器
     *
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean<ReplaceStreamFilter> someFilterRegistration() {
        FilterRegistrationBean<ReplaceStreamFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new ReplaceStreamFilter());
        registration.addUrlPatterns("/*");
        registration.setName("streamFilter");
        return registration;
    }
}
