admin
2024-10-15 6d313a4a6f9f9d7ea65ec50ab0d4491e595edb97
提交 | 用户 | 时间
e57a89 1 package com.jcdm.framework.config;
2
3 import java.util.concurrent.TimeUnit;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.http.CacheControl;
8 import org.springframework.web.cors.CorsConfiguration;
9 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
10 import org.springframework.web.filter.CorsFilter;
11 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
12 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
13 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
14 import com.jcdm.common.config.MesConfig;
15 import com.jcdm.common.constant.Constants;
16 import com.jcdm.framework.interceptor.RepeatSubmitInterceptor;
17
18 /**
19  * 通用配置
20  * 
21  * @author jc
22  */
23 @Configuration
24 public class ResourcesConfig implements WebMvcConfigurer
25 {
26     @Autowired
27     private RepeatSubmitInterceptor repeatSubmitInterceptor;
28
29     @Override
30     public void addResourceHandlers(ResourceHandlerRegistry registry)
31     {
32         /** 本地文件上传路径 */
33         registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
34                 .addResourceLocations("file:" + MesConfig.getProfile() + "/");
35
36         /** swagger配置 */
37         registry.addResourceHandler("/swagger-ui/**")
38                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
39                 .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic());;
40     }
41
42     /**
43      * 自定义拦截规则
44      */
45     @Override
46     public void addInterceptors(InterceptorRegistry registry)
47     {
48         registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
49     }
50
51     /**
52      * 跨域配置
53      */
54     @Bean
55     public CorsFilter corsFilter()
56     {
57         CorsConfiguration config = new CorsConfiguration();
58         config.setAllowCredentials(true);
59         // 设置访问源地址
60         config.addAllowedOriginPattern("*");
61         // 设置访问源请求头
62         config.addAllowedHeader("*");
63         // 设置访问源请求方法
64         config.addAllowedMethod("*");
65         // 有效期 1800秒
66         config.setMaxAge(1800L);
67         // 添加映射路径,拦截一切请求
68         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
69         source.registerCorsConfiguration("/**", config);
70         // 返回新的CorsFilter
71         return new CorsFilter(source);
72     }
73 }