懒羊羊
2023-09-19 3d2401cf8ea9ae3d830c0568e7751e2e8cc8db22
提交 | 用户 | 时间
1ac2bc 1 /**
2  * Copyright 2018-2020 stylefeng & fengshuonan (sn93@qq.com)
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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 package cn.stylefeng.guns.config.web;
17
18 import cn.stylefeng.guns.sys.core.exception.page.GunsErrorView;
19 import cn.stylefeng.guns.sys.core.listener.ConfigListener;
20 import cn.stylefeng.roses.core.xss.XssFilter;
21 import com.alibaba.druid.pool.DruidDataSource;
22 import com.alibaba.druid.support.http.StatViewServlet;
23 import com.alibaba.druid.support.http.WebStatFilter;
24 import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
25 import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
26 import com.google.code.kaptcha.impl.DefaultKaptcha;
27 import com.google.code.kaptcha.util.Config;
28 import org.springframework.aop.Advisor;
29 import org.springframework.aop.support.DefaultPointcutAdvisor;
30 import org.springframework.aop.support.JdkRegexpMethodPointcut;
31 import org.springframework.boot.web.servlet.FilterRegistrationBean;
32 import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
33 import org.springframework.boot.web.servlet.ServletRegistrationBean;
34 import org.springframework.context.annotation.Bean;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.http.converter.HttpMessageConverter;
37 import org.springframework.web.context.request.RequestContextListener;
38 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
39 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
40 import org.springframework.beans.factory.annotation.Value;
41
42
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.Properties;
46
47 /**
48  * web 配置类
49  *
50  * @author fengshuonan
51  * @date 2016年11月12日 下午5:03:32
52  */
53 @Configuration
54 public class WebConfig implements WebMvcConfigurer {
55     // 通过读取配置项获取的文件上传路径
56     @Value("$file.uploadFolder")
57     private String basePath;
58
59     /**
60      * 配置string解析器放在json解析器前边
61      *
62      * @author fengshuonan
63      * @Date 2019/8/7 23:09
64      */
65     @Override
66     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
67         HttpMessageConverter<?> httpMessageConverter = converters.get(0);
68         converters.remove(0);
69         converters.add(2, httpMessageConverter);
70     }
71
72     /**
73      * 静态资源映射
74      */
75     @Override
76     public void addResourceHandlers(ResourceHandlerRegistry registry) {
77
78         //swagger
79         registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
80         registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
81
82         //本应用
83         registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
84
85         //流程设计器
86         registry.addResourceHandler("/activiti-editor/**").addResourceLocations("classpath:/activiti-editor/");
87
88         /*
89          * 资源映射路径
90          * addResourceHandler:访问映射路径
91          * addResourceLocations:资源绝对路径
92          */
93         registry.addResourceHandler("/img/**").addResourceLocations("file:D:/img/");
94     }
95
96     /**
97      * 默认错误页面,返回json
98      */
99     @Bean("error")
100     public GunsErrorView error() {
101         return new GunsErrorView();
102     }
103
104     /**
105      * druidServlet注册
106      */
107     @Bean
108     public ServletRegistrationBean druidServletRegistration() {
109         ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
110         registration.addUrlMappings("/druid/*");
111         return registration;
112     }
113
114     /**
115      * druid监控 配置URI拦截策略
116      */
117     @Bean
118     public FilterRegistrationBean druidStatFilter() {
119         FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
120         //添加过滤规则.
121         filterRegistrationBean.addUrlPatterns("/*");
122         //添加不需要忽略的格式信息.
123         filterRegistrationBean.addInitParameter(
124                 "exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid,/druid/*");
125         //用于session监控页面的用户名显示 需要登录后主动将username注入到session里
126         filterRegistrationBean.addInitParameter("principalSessionName", "username");
127         return filterRegistrationBean;
128     }
129
130     /**
131      * druid数据库连接池监控
132      */
133     @Bean
134     public DruidStatInterceptor druidStatInterceptor() {
135         return new DruidStatInterceptor();
136     }
137
138     @Bean
139     public JdkRegexpMethodPointcut druidStatPointcut() {
140         JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut();
141         String patterns = "cn.stylefeng.guns.modular.*.service.*";
142         //可以set多个
143         druidStatPointcut.setPatterns(patterns);
144         return druidStatPointcut;
145     }
146
147     /**
148      * druid数据库连接池监控
149      */
150     @Bean
151     public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
152         BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
153         beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
154         beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
155         return beanTypeAutoProxyCreator;
156     }
157
158     /**
159      * druid 为druidStatPointcut添加拦截
160      *
161      * @return
162      */
163     @Bean
164     public Advisor druidStatAdvisor() {
165         return new DefaultPointcutAdvisor(druidStatPointcut(), druidStatInterceptor());
166     }
167
168     /**
169      * xssFilter注册
170      */
171     @Bean
172     public FilterRegistrationBean xssFilterRegistration() {
173         XssFilter xssFilter = new XssFilter();
174         xssFilter.setUrlExclusion(Arrays.asList("/notice/add", "/notice/update", "/excelExportDeploy/editItem", "/excelExportDeploy/addItem"));
175         FilterRegistrationBean registration = new FilterRegistrationBean(xssFilter);
176         registration.addUrlPatterns("/*");
177         return registration;
178     }
179
180     /**
181      * RequestContextListener注册
182      */
183     @Bean
184     public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
185         return new ServletListenerRegistrationBean<>(new RequestContextListener());
186     }
187
188     /**
189      * ConfigListener注册
190      */
191     @Bean
192     public ServletListenerRegistrationBean<ConfigListener> configListenerRegistration() {
193         return new ServletListenerRegistrationBean<>(new ConfigListener());
194     }
195
196     /**
197      * 验证码生成相关
198      */
199     @Bean
200     public DefaultKaptcha kaptcha() {
201         Properties properties = new Properties();
202         properties.put("kaptcha.border", "no");
203         properties.put("kaptcha.border.color", "105,179,90");
204         properties.put("kaptcha.textproducer.font.color", "blue");
205         properties.put("kaptcha.image.width", "125");
206         properties.put("kaptcha.image.height", "45");
207         properties.put("kaptcha.textproducer.font.size", "45");
208         properties.put("kaptcha.session.key", "code");
209         properties.put("kaptcha.textproducer.char.length", "4");
210         properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
211         Config config = new Config(properties);
212         DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
213         defaultKaptcha.setConfig(config);
214         return defaultKaptcha;
215     }
216 }