package com.crm.rely.backend.configuration;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.util.SensitiveWordFilterUtil;
import com.crm.rely.backend.util.SensitiveWordInitUtil;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.filter.GlobalFilter;
import com.crm.rely.backend.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

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

/**
 * 全局过滤器
 *
 * @author houn
 */
public class GlobalFilterConfiguration {

    @Autowired
    private RedisService redisService;

    /**
     * 过滤敏感字和xss攻击
     *
     * @return
     * @throws ServiceException
     */
//    @Bean
//    public FilterRegistrationBean globalFilterRegistrationBean() throws ServiceException {
//
//        if (redisService.isConnect()) {
//            /**
//             * 获取敏感字词库 并将它们加入到  SensitiveWordFilterUtil.setSensitiveWordMap变量中 用于请求是过滤敏感字
//             */
//            Set<String> values = redisService.getObject(Constants.SENSITIVE_WORD_REDIS);
//            if (values != null && values.size() > 0) {
//                Map map = new SensitiveWordInitUtil().initKeyWord(values);
//                SensitiveWordFilterUtil.setSensitiveWordMap(map);
//            }
//        }
//
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(new GlobalFilter());
//        filterRegistrationBean.setOrder(1);
//        filterRegistrationBean.setEnabled(true);
//        filterRegistrationBean.addUrlPatterns("/*");
//        Map<String, String> initParameters = new HashMap<>();
//        initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
//        initParameters.put("isIncludeRichText", "true");
//        filterRegistrationBean.setInitParameters(initParameters);
//        return filterRegistrationBean;
//    }
}

