| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.shkpr.service.alambizplugin.configuration;
- import com.shkpr.service.alambizplugin.SpringContextUtil;
- import com.shkpr.service.alambizplugin.commproperties.AsyncTaskProperties;
- import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
- import com.shkpr.service.alambizplugin.constants.ApiURI;
- import com.shkpr.service.alambizplugin.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.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- /**
- * 该类主要用来注册各种Controller拦截器
- * WebSecurityConfig类中注册的过滤器JWTLoginFilter、JWTAuthenticationFilter优先于该类中注册的拦截器URIInterceptorIntef
- * 过滤拦截顺序: JWTLoginFilter--->JWTAuthenticationFilter--->URIInterceptorIntef
- */
- @Configuration
- @EnableWebMvc
- public class WebMvcConfiguration implements WebMvcConfigurer {
- private final TempFileProperties tempFileProperties;
- private final AsyncTaskProperties asyncTaskProperties;
- public WebMvcConfiguration(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);
- }
- }
|