package com.vincent.rsf.openApi.config; import com.vincent.rsf.openApi.entity.constant.Constants; import com.vincent.rsf.openApi.security.filter.AppIdAuthenticationFilter; import com.vincent.rsf.openApi.utils.Http; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.AsyncHandlerInterceptor; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * WebMvc配置, 拦截器、资源映射等都在此配置 * * @author vincent * @since 2019-06-12 10:11:16 */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired private AppIdAuthenticationFilter appIdAuthenticationFilter; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getAsyncHandlerInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/swagger-resources/**", "/webjars/**","/erp/**", "/v2/**","/v3/**","/doc.html/**", "/swagger-ui.html/**"); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .exposedHeaders(Constants.TOKEN_HEADER_NAME) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH") .allowCredentials(true) .maxAge(3600); } @Bean public AsyncHandlerInterceptor getAsyncHandlerInterceptor() { return new AsyncHandlerInterceptor(){ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Http.cors(response); return true; } }; } }