spring boot 添加自定义 fliter

2023-10-20 12:24:45 浏览数 (4)

自定义的 springboot 过滤器要注册到 spring boot中, 首先先添加一个过滤器,然后在spring boot 进行注册即可。

代码语言:javascript复制
import com.reapal.openapi.web.filter.XSSFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Title: 注册自定义过滤器
 * Description:
 *
 * @author liu kai
 * @date 2018/6/26 16:06.
 */
@Configuration
public class XssFilterConfig {

    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new XSSFilter());
        registration.addUrlPatterns("/*");
        registration.setName("xssFilter");
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }
}

1 人点赞