spring boot 整合WebSocket 遇到的一个坑
·
遇到这个坑的原因喃是因为使用 的容器不同
一种是使用spring boot本身自带的tomcat容器,坑都是在这种方式中.,
另外一种是排除掉spring boot 自带的tomcat容器,使用外置的tomcat.这种方式基本上没什么问题.
坑一:
socketJQ.js:101 Failed to load http://10.2.52.73:9090/checkUsernameAppType?appType=AT1505728361851: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
ajax @ socketJQ.js:101
window.onload @ socketJQ.js:11
load (async)
(anonymous) @ socketJQ.js:4
socketJQ.js:101 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://10.2.52.73:9090/checkUsernameAppType?appType=AT1505728361851'.
at ajax (http://fushengruomengzhang_admin.gitee.io/jsandcss/WebSocket/socketJQ.js:101:13)
at window.onload (http://fushengruomengzhang_admin.gitee.io/jsandcss/WebSocket/socketJQ.js:11:5)
解决办法:
添加Filter
import cn.zfs.websocket.wrapper.GetRequest;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/*")
//@Configuration
public class WebSocketFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("执行filter");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
// 设置跨域
HttpServletResponse httpServletResponse1 = (HttpServletResponse) response;
httpServletResponse1.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse1.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
httpServletResponse1.setHeader("Access-Control-Max-Age", "3600");
httpServletResponse1.setHeader("Access-Control-Allow-Headers", "x-requested-with");
// 设置request的编码方式
if(httpServletRequest.getMethod().equalsIgnoreCase("GET")) {
if(!(httpServletRequest instanceof GetRequest)) {
httpServletRequest = new GetRequest(httpServletRequest, "UTF-8");//处理get请求编码
}
} /*else {
req.setCharacterEncoding("UTF-8");//处理post请求编码
}*/
chain.doFilter(httpServletRequest,httpServletResponse);
}
@Override
public void destroy() {
System.out.println("GIME over");
}
}
注意,在这里我把Configuration注解注掉了,是因为在外置的tomcat中.不需要该注解,也不需要下边的坑的解决方案就可以解决问题,但是
如果使用内容的tomcat的话,就需要使用该注解,不然浏览器就会抛出如上异常.
坑二:
在坑1的解决方法基础上,然后你会遇到下边的坑
WebSocket connection to 'ws://10.2.52.73:9090/websocket/sdhfftyhkyuffghj33232jhfhds/AT1505728361851' failed: Error during WebSocket handshake: Unexpected response code: 404
进而无法成功升级http协议为ws协议,解决方法喃,还是参考了一下这个博客https://www.cnblogs.com/bianzy/p/5822426.html,上面讲使用内置的tomcat容器时,<<首先要注入ServerEndpointExporter,这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint。要注意,如果使用独立的servlet容器,而不是直接使用springboot的内置容器,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。>>,所以解决方案也就是在配置文件中加一个bean.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
@ComponentScan("cn.zfs.websocket")
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}更多推荐
所有评论(0)