WebMvcConfiguration.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.shkpr.service.alambizplugin.configuration;
  2. import com.shkpr.service.alambizplugin.SpringContextUtil;
  3. import com.shkpr.service.alambizplugin.commproperties.AsyncTaskProperties;
  4. import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
  5. import com.shkpr.service.alambizplugin.constants.ApiURI;
  6. import com.shkpr.service.alambizplugin.interfaces.URIInterceptorIntef;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.core.Ordered;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.util.StringUtils;
  12. import org.springframework.web.cors.CorsConfiguration;
  13. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  14. import org.springframework.web.filter.CorsFilter;
  15. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  16. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  17. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  18. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.TreeMap;
  23. /**
  24. * 该类主要用来注册各种Controller拦截器
  25. * WebSecurityConfig类中注册的过滤器JWTLoginFilter、JWTAuthenticationFilter优先于该类中注册的拦截器URIInterceptorIntef
  26. * 过滤拦截顺序: JWTLoginFilter--->JWTAuthenticationFilter--->URIInterceptorIntef
  27. */
  28. @Configuration
  29. @EnableWebMvc
  30. public class WebMvcConfiguration implements WebMvcConfigurer {
  31. private final TempFileProperties tempFileProperties;
  32. private final AsyncTaskProperties asyncTaskProperties;
  33. public WebMvcConfiguration(TempFileProperties tempFileProperties , AsyncTaskProperties asyncTaskProperties) {
  34. this.tempFileProperties = tempFileProperties;
  35. this.asyncTaskProperties = asyncTaskProperties;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. @Override
  41. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  42. //临时文件映射
  43. registry.addResourceHandler(ApiURI.URI_GIS_SURVEY_H + "/" + ApiURI.URI_XXX_TEMP_FILES + "/**")
  44. .addResourceLocations("file:" + tempFileProperties.getResourcePath() + "/");
  45. //异步结果映射
  46. registry.addResourceHandler(ApiURI.URI_GIS_SURVEY_H + "/" + ApiURI.URI_XXX_ASYNC_RESULTS + "/**")
  47. .addResourceLocations("file:" + asyncTaskProperties.getResultPath() + "/");
  48. }
  49. @Override
  50. public void addInterceptors(InterceptorRegistry registry) {
  51. @SuppressWarnings("static-access")
  52. Map<String, URIInterceptorIntef> mapURLInterceptorByOrder = new TreeMap<String, URIInterceptorIntef>(); //TreeMap默认按键值升序
  53. Map<String, URIInterceptorIntef> mapURLInterceptor = SpringContextUtil.getApplicationContext().getBeansOfType(URIInterceptorIntef.class); //获取URIInterceptorIntef类型的所有Component组件
  54. if (!StringUtils.isEmpty(mapURLInterceptor)){
  55. Iterator it = mapURLInterceptor.entrySet().iterator();
  56. while (it.hasNext()){
  57. Map.Entry entry = (Map.Entry)it.next();
  58. URIInterceptorIntef urlInterceptor = (URIInterceptorIntef)entry.getValue();
  59. if (urlInterceptor != null){
  60. mapURLInterceptorByOrder.put(String.valueOf(urlInterceptor.order()), urlInterceptor);
  61. }
  62. }
  63. }
  64. if (!StringUtils.isEmpty(mapURLInterceptorByOrder)){
  65. Iterator it = mapURLInterceptorByOrder.entrySet().iterator();
  66. while (it.hasNext()){
  67. Map.Entry entry = (Map.Entry)it.next();
  68. //System.out.println("WebMvcConfig::addInterceptors() key="+(String) entry.getKey());
  69. URIInterceptorIntef urlInterceptor = (URIInterceptorIntef)entry.getValue();
  70. if (urlInterceptor != null){
  71. String[] arrForbidUrls = urlInterceptor.forbidPathPatterns();
  72. if (arrForbidUrls == null)
  73. arrForbidUrls = new String[]{};
  74. String[] arrExcludeUrls = urlInterceptor.excludePathPatterns();
  75. if (arrExcludeUrls == null){
  76. //多个拦截器将按照链接的顺序依次匹配拦截(注:有可能多个拦截器都匹配成功,则多个拦截器依次执行)
  77. registry.addInterceptor(urlInterceptor).addPathPatterns(arrForbidUrls);
  78. }else{
  79. registry.addInterceptor(urlInterceptor).addPathPatterns(arrForbidUrls).excludePathPatterns(arrExcludeUrls);
  80. }
  81. }
  82. }
  83. }
  84. //super.addInterceptors(registry);
  85. }
  86. //解决跨域问题
  87. private CorsConfiguration buildConfig() {
  88. CorsConfiguration corsConfiguration = new CorsConfiguration();
  89. corsConfiguration.addAllowedOrigin("*");
  90. //corsConfiguration.addAllowedMethod("*");
  91. corsConfiguration.addAllowedMethod(HttpMethod.POST);
  92. corsConfiguration.addAllowedMethod(HttpMethod.GET);
  93. corsConfiguration.addAllowedMethod(HttpMethod.PUT);
  94. corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
  95. corsConfiguration.addAllowedMethod(HttpMethod.OPTIONS);
  96. corsConfiguration.addAllowedMethod(HttpMethod.HEAD);
  97. corsConfiguration.addAllowedHeader("x-requested-with,Content-Type,Authorization,user-agent,Timestamp,Sequence,Signature");
  98. return corsConfiguration;
  99. }
  100. //解决跨域问题
  101. @Bean
  102. public CorsFilter corsFilter() {
  103. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  104. source.registerCorsConfiguration("/**", buildConfig());
  105. return new CorsFilter(source);
  106. }
  107. //配置该服务的默认首页用于阿里云服务SLB的http head健康检查
  108. @Override
  109. public void addViewControllers(ViewControllerRegistry registry) {
  110. registry.addViewController("/").setViewName("index");
  111. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  112. //super.addViewControllers(registry);
  113. }
  114. }