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