package top.imwork.commons.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import top.imwork.commons.core.xss.XssFilter;

/**
 * Copyright (C), 2015-2021
 * FileName: XssConfiguration
 * Author(作者姓名):   jiangxiaowei
 * CreateDate(创建时间):     2021/8/2 15:19
 * UpdateDate(修改时间):     2021/8/2 15:19
 * Description(功能描述):    XSS攻击防护
 * Since(版本号)  1.0.0
 * History:
 */
@Configuration
@PropertySource(value = {"classpath:application.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8")
@ConditionalOnProperty(name = {"core.xss.filter"}, havingValue = "true", matchIfMissing = true)
public class XssConfiguration {
    static final Logger logger = LoggerFactory.getLogger(XssConfiguration.class);

    public XssConfiguration() {
        logger.info("XSS攻击防护配置");
    }

    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new XssFilter());
        String[] strings = new String[1];
        strings[0] = "/*";
        registration.addUrlPatterns(strings);
        registration.setOrder(1);
        return registration;
    }
}
