|
|
@@ -1,121 +0,0 @@
|
|
|
-package com.shkpr.service.customgateway.core.config;
|
|
|
-
|
|
|
-import com.shkpr.service.customgateway.core.utils.SpringContextUtil;
|
|
|
-import com.shkpr.service.customgateway.core.properties.AsyncTaskProperties;
|
|
|
-import com.shkpr.service.customgateway.core.properties.TempFileProperties;
|
|
|
-import com.shkpr.service.customgateway.core.constants.ApiURI;
|
|
|
-import com.shkpr.service.customgateway.core.interfaces.URIInterceptorIntef;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.core.Ordered;
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-import org.springframework.web.cors.CorsConfiguration;
|
|
|
-import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
-import org.springframework.web.filter.CorsFilter;
|
|
|
-import org.springframework.web.servlet.config.annotation.*;
|
|
|
-
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.TreeMap;
|
|
|
-
|
|
|
-/**
|
|
|
- * 该类主要用来注册各种Controller拦截器
|
|
|
- * WebSecurityConfig类中注册的过滤器JWTLoginFilter、JWTAuthenticationFilter优先于该类中注册的拦截器URIInterceptorIntef
|
|
|
- * 过滤拦截顺序: JWTLoginFilter--->JWTAuthenticationFilter--->URIInterceptorIntef
|
|
|
- */
|
|
|
-@Configuration
|
|
|
-@EnableWebMvc
|
|
|
-public class WebMvcConfig implements WebMvcConfigurer {
|
|
|
- private TempFileProperties tempFileProperties;
|
|
|
- private AsyncTaskProperties asyncTaskProperties;
|
|
|
-
|
|
|
- public WebMvcConfig(TempFileProperties tempFileProperties , AsyncTaskProperties asyncTaskProperties) {
|
|
|
- this.tempFileProperties = tempFileProperties;
|
|
|
- this.asyncTaskProperties = asyncTaskProperties;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * {@inheritDoc}
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
- //临时文件映射
|
|
|
- registry.addResourceHandler(ApiURI.URI_GIS_SURVEY_H + "/" + ApiURI.URI_XXX_TEMP_FILES + "/**")
|
|
|
- .addResourceLocations("file:" + tempFileProperties.getResourcePath() + "/");
|
|
|
- //异步结果映射
|
|
|
- registry.addResourceHandler(ApiURI.URI_GIS_SURVEY_H + "/" + ApiURI.URI_XXX_ASYNC_RESULTS + "/**")
|
|
|
- .addResourceLocations("file:" + asyncTaskProperties.getResultPath() + "/");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void addInterceptors(InterceptorRegistry registry) {
|
|
|
- @SuppressWarnings("static-access")
|
|
|
- Map<String, URIInterceptorIntef> mapURLInterceptorByOrder = new TreeMap<String, URIInterceptorIntef>(); //TreeMap默认按键值升序
|
|
|
- Map<String, URIInterceptorIntef> mapURLInterceptor = SpringContextUtil.getApplicationContext().getBeansOfType(URIInterceptorIntef.class); //获取URIInterceptorIntef类型的所有Component组件
|
|
|
- if (!StringUtils.isEmpty(mapURLInterceptor)){
|
|
|
- Iterator it = mapURLInterceptor.entrySet().iterator();
|
|
|
- while (it.hasNext()){
|
|
|
- Map.Entry entry = (Map.Entry)it.next();
|
|
|
- URIInterceptorIntef urlInterceptor = (URIInterceptorIntef)entry.getValue();
|
|
|
- if (urlInterceptor != null){
|
|
|
- mapURLInterceptorByOrder.put(String.valueOf(urlInterceptor.order()), urlInterceptor);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!StringUtils.isEmpty(mapURLInterceptorByOrder)){
|
|
|
- Iterator it = mapURLInterceptorByOrder.entrySet().iterator();
|
|
|
- while (it.hasNext()){
|
|
|
- Map.Entry entry = (Map.Entry)it.next();
|
|
|
- //System.out.println("WebMvcConfig::addInterceptors() key="+(String) entry.getKey());
|
|
|
- URIInterceptorIntef urlInterceptor = (URIInterceptorIntef)entry.getValue();
|
|
|
- if (urlInterceptor != null){
|
|
|
- String[] arrForbidUrls = urlInterceptor.forbidPathPatterns();
|
|
|
- if (arrForbidUrls == null)
|
|
|
- arrForbidUrls = new String[]{};
|
|
|
- String[] arrExcludeUrls = urlInterceptor.excludePathPatterns();
|
|
|
- if (arrExcludeUrls == null){
|
|
|
- //多个拦截器将按照链接的顺序依次匹配拦截(注:有可能多个拦截器都匹配成功,则多个拦截器依次执行)
|
|
|
- registry.addInterceptor(urlInterceptor).addPathPatterns(arrForbidUrls);
|
|
|
- }else{
|
|
|
- registry.addInterceptor(urlInterceptor).addPathPatterns(arrForbidUrls).excludePathPatterns(arrExcludeUrls);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //super.addInterceptors(registry);
|
|
|
- }
|
|
|
-
|
|
|
- //解决跨域问题
|
|
|
- private CorsConfiguration buildConfig() {
|
|
|
- CorsConfiguration corsConfiguration = new CorsConfiguration();
|
|
|
- corsConfiguration.addAllowedOrigin("*");
|
|
|
- //corsConfiguration.addAllowedMethod("*");
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.POST);
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.GET);
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.PUT);
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.OPTIONS);
|
|
|
- corsConfiguration.addAllowedMethod(HttpMethod.HEAD);
|
|
|
- corsConfiguration.addAllowedHeader("x-requested-with,Content-Type,Authorization,user-agent,Timestamp,Sequence,Signature");
|
|
|
- return corsConfiguration;
|
|
|
- }
|
|
|
-
|
|
|
- //解决跨域问题
|
|
|
- @Bean
|
|
|
- public CorsFilter corsFilter() {
|
|
|
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
- source.registerCorsConfiguration("/**", buildConfig());
|
|
|
- return new CorsFilter(source);
|
|
|
- }
|
|
|
-
|
|
|
- //配置该服务的默认首页用于阿里云服务SLB的http head健康检查
|
|
|
- @Override
|
|
|
- public void addViewControllers(ViewControllerRegistry registry) {
|
|
|
- registry.addViewController("/").setViewName("index");
|
|
|
- registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
|
|
- //super.addViewControllers(registry);
|
|
|
- }
|
|
|
-
|
|
|
-}
|