WebMvcConfigurerAdapter的出现是为了替代xml出现的,在这个类型里可以配置拦截器相关配置,这个类的使用是弱化繁杂xml配置的一个缩影,Adapter中文译作适配器,转换器,意思就是作为xml配置的一种适配,一种取代。我们使用的方式就是继承,
@Component
public class WebMvcConf extends WebMvcConfigurerAdapter {
@Autowired
private AuthActionInterceptor authActionInterceptor;
@Autowired
private AuthInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加拦截器
registry.addInterceptor(authInterceptor).excludePathPatterns("/static").addPathPatterns("/**");
registry.addInterceptor(authActionInterceptor).addPathPatterns("/house/toAdd").addPathPatterns("/accounts/profile").addPathPatterns("/accounts/profileSubmit").addPathPatterns("/house/bookmarked").addPathPatterns("/house/del")
.addPathPatterns("/house/ownlist").addPathPatterns("/house/add").addPathPatterns("/house/toAdd").addPathPatterns("/agency/agentMsg").addPathPatterns("/comment/leaveComment").addPathPatterns("/comment/leaveBlogComment");
// 调用父类
super.addInterceptors(registry);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// 进入限制
registry.addMapping("/**")
// 放行哪些原始域
.allowedOrigins("*")
// 是否发送Cookie信息
.allowCredentials(true)
// 放行哪些原始域(请求方式)
.allowedMethods("GET", "POST", "PUT", "DELETE")
// 放行哪些原始域(头部信息)
.allowedHeaders("*")
// 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
.exposedHeaders("Header1", "Header2");
super.addCorsMappings(registry);
}
}