WebAppConfiguration.java 2.65 KB
package com.bsth;

import javax.servlet.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
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.InterceptorRegistry;
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 com.bsth.filter.ResourceFilter;
import com.bsth.oplog.http.HttpOpLogInterceptor;
import com.bsth.websocket.WebSocketHandshakeInterceptor;
import com.bsth.websocket.handler.RealControlSocketHandler;

@Configuration
@EnableWebSocket
@ComponentScan
public class WebAppConfiguration extends WebMvcConfigurerAdapter implements WebSocketConfigurer{
	
//	@Autowired
//	HttpOpLogInterceptor httpOpLogInterceptor;
	
	/**
	 * @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();
	}
}