WebAppConfiguration.java
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.bsth;
import com.bsth.filter.ResourceFilter;
import com.bsth.listener.UserLogoutSessionListener;
import com.bsth.websocket.WebSocketHandshakeInterceptor;
import com.bsth.websocket.handler.RealControlSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import javax.servlet.Filter;
@Configuration
@EnableWebSocket
@ComponentScan
public class WebAppConfiguration extends WebMvcConfigurerAdapter implements WebSocketConfigurer{
// @Autowired
// HttpOpLogInterceptor httpOpLogInterceptor;
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* @Title: httpPutFormContentFilter
* @Description: TODO(弥补浏览器不支持PUT/DELETE,对携带 _method 参数的请求进行转换)
*/
@Bean
public Filter httpPutFormContentFilter() {
return new HttpPutFormContentFilter();
}
/**
* @Title: characterEncodingFilter
* @Description: TODO(编码过滤器)
*/
@Bean
public Filter characterEncodingFilter(){
return new CharacterEncodingFilter("UTF-8");
}
/**
*
* @Title: resourceFilterRegistration
* @Description: TODO(静态资源过滤器, 只处理 /pages 目录下的片段请求 )
*/
@Bean
public FilterRegistrationBean resourceFilterRegistration(){
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ResourceFilter());
registration.addUrlPatterns("/pages/*");
return registration;
}
/**
*
* @Title: addInterceptors
* @Description: TODO(HTTP结构化访问日志记录 )
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(httpOpLogInterceptor);
}*/
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//线调webSocket
registry.addHandler(new RealControlSocketHandler(), "/sockjs/realcontrol").addInterceptors(new WebSocketHandshakeInterceptor())
.withSockJS();
}
/**
* 增加websocket的输出缓冲区
* @return
*/
@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(52768);
container.setMaxBinaryMessageBufferSize(52768);
logger.info("Websocket factory returned");
return container;
}
//--------------------- 配置listener ---------------------//
@Bean
public ServletListenerRegistrationBean<UserLogoutSessionListener> UserLogoutSessionListenerRegistrationBean() {
ServletListenerRegistrationBean<UserLogoutSessionListener> userLogoutSessionListenerRegistrationBean =
new ServletListenerRegistrationBean<>();
userLogoutSessionListenerRegistrationBean.setListener(userLogoutSessionListenerBean());
return userLogoutSessionListenerRegistrationBean;
}
@Bean
public UserLogoutSessionListener userLogoutSessionListenerBean() {
return new UserLogoutSessionListener();
}
}