import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
Logger logger = LoggerFactory.getLogger(WebConfiguration.class);
@Bean
public FilterRegistrationBean filterRegistrationBean() {
logger.info("字符过滤start----》");
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//CharacterEncodingFilter SpringWeb提供的过滤器类
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setForceEncoding(true);//强制编码
characterEncodingFilter.setEncoding("UTF-8");
registrationBean.setFilter(characterEncodingFilter);//注册的过滤器拦截的路径
registrationBean.addUrlPatterns("/*");
logger.info("字符过滤end----》");
return registrationBean;
}
/* 注入拦截器 */
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration r1 = registry.addInterceptor(new MyInterceptor());
//添加拦截请求
r1.addPathPatterns("/**");
//r1.addPathPatterns("/*");使用这个发送请求拦截器无效,后改成/**
//添加不拦截的请求
r1.excludePathPatterns("/login");
}
}
/* 是拦截所有的文件夹,不包含子文件夹,如 /user, /query 等
/** 是拦截所有的文件夹及里面的子文件夹,如/user, /user/add… 等