inleft
2022-08-23 124ca536917b5eebe059bb98745f6743c52a6836
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.config;
26
27 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
28 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
29 import org.springframework.boot.web.servlet.FilterRegistrationBean;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.context.annotation.Import;
33 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
34 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
35 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
36 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
37 import vip.xiaonuo.core.web.SnowyRequestResponseBodyMethodProcessor;
38 import vip.xiaonuo.sys.core.error.SnowyErrorAttributes;
39 import vip.xiaonuo.sys.core.filter.RequestNoFilter;
40 import vip.xiaonuo.sys.core.filter.xss.XssFilter;
41 import vip.xiaonuo.sys.core.validator.SnowyValidator;
42
43 import javax.annotation.PostConstruct;
44 import javax.annotation.Resource;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Objects;
48
49 /**
50  * web配置
51  *
52  * @author yubaoshan
53  * @date 2020/4/11 10:23
54  */
55 @Configuration
56 @Import({cn.hutool.extra.spring.SpringUtil.class})
57 public class WebMvcConfig implements WebMvcConfigurer {
58
59     /**
60      * 错误信息提示重写
61      *
62      * @author yubaoshan
63      * @date 2020/4/14 22:27
64      */
65     @Bean
66     public SnowyErrorAttributes snowyErrorAttributes() {
67         return new SnowyErrorAttributes();
68     }
69
70     /**
71      * 静态资源映射
72      *
73      * @author yubaoshan
74      * @date 2020/4/11 10:23
75      */
76     @Override
77     public void addResourceHandlers(ResourceHandlerRegistry registry) {
78         //swagger增强的静态资源映射
79         registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
80         registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
81         //flowable设计器静态资源映射
82         registry.addResourceHandler("/designer/**").addResourceLocations("classpath:/designer/");
83     }
84
85     /**
86      * xss过滤器
87      *
88      * @author yubaoshan
89      * @date 2020/6/21 10:30
90      */
91     @Bean
92     public FilterRegistrationBean<XssFilter> xssFilterFilterRegistrationBean() {
93         FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>(new XssFilter());
94         registration.addUrlPatterns("/*");
95         return registration;
96     }
97
98     /**
99      * 请求唯一编号生成器
100      *
101      * @author yubaoshan
102      * @date 2020/6/21 10:30
103      */
104     @Bean
105     public FilterRegistrationBean<RequestNoFilter> requestNoFilterFilterRegistrationBean() {
106         FilterRegistrationBean<RequestNoFilter> registration = new FilterRegistrationBean<>(new RequestNoFilter());
107         registration.addUrlPatterns("/*");
108         return registration;
109     }
110
111     /**
112      * json自定义序列化工具,long转string
113      *
114      * @author yubaoshan
115      * @date 2020/5/28 14:48
116      */
117     @Bean
118     public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
119         return jacksonObjectMapperBuilder ->
120                 jacksonObjectMapperBuilder
121                         .serializerByType(Long.class, ToStringSerializer.instance)
122                         .serializerByType(Long.TYPE, ToStringSerializer.instance);
123     }
124
125     /**
126      * 自定义的spring参数校验器,重写主要为了保存一些在自定义validator中读不到的属性
127      *
128      * @author xuyuxiang
129      * @date 2020/8/12 20:18
130      */
131     @Bean
132     public SnowyValidator snowyValidator() {
133         return new SnowyValidator();
134     }
135
136
137     /**
138      * 自定义的SnowyRequestResponseBodyMethodProcessor,放在所有resolvers之前
139      *
140      * @author xuyuxiang
141      * @date 2020/8/21 21:09
142      */
143     @Configuration
144     public static class MethodArgumentResolver {
145
146         @Resource
147         private RequestMappingHandlerAdapter adapter;
148
149         @PostConstruct
150         public void injectSelfMethodArgumentResolver() {
151             List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
152             argumentResolvers.add(new SnowyRequestResponseBodyMethodProcessor(adapter.getMessageConverters()));
153             argumentResolvers.addAll(Objects.requireNonNull(adapter.getArgumentResolvers()));
154             adapter.setArgumentResolvers(argumentResolvers);
155         }
156     }
157
158 }