1.配置环境
CentOS-8.1.1911-x86_64-dvd1.iso
SpringBoot 2.1.2.RELEASE
stomp-websocket 2.3.3-1
SpringSecurity
chrome浏览器。
2.chrome浏览器端,错误提示如下:
Opening Web Socket...
stomp.js:145 Whoops! Lost connection to http://192.168.7.3/ws/shootingpoint
VM321 htmlfile:8 Uncaught DOMException: Blocked a frame with origin "http://192.168.7.3" from accessing a cross-origin frame.
at http://192.168.7.3/ws/point/753/xql4b0gd/htmlfile?c=_jp.agt02wc:8:20
(anonymous) @ VM321 htmlfile:8
VM321 htmlfile:10 Uncaught TypeError: Cannot read property 'message' of undefined
at p (VM321 htmlfile:10)
at VM322 htmlfile:13
p @ VM321 htmlfile:10
(anonymous) @ VM322 htmlfile:13
iframe.html:10 GET https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js net::ERR_NAME_NOT_RESOLVED
一开始以为是spring-security的X-Frame-Options问题,它默认配置是DNEY拒绝,意思是禁止在iframe里面展示页面。防止被嵌套到别人的网站去,点击劫持。
然后我设置成SAMEORIGIN同源,如果是相同URI时允许的。
springsecurity配置的代码如下:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().sameOrigin();//设置 X-Frame-Options允许同源的内容展示在iframe里面。
}
}
同时为了防止https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js这句自动生成的html出现,因为我是内网,访问肯定报错ERR_NAME_NOT_RESOLVED
在java代码中增加下面的配置项.setClientLibraryUrl("http://192.168.7.3/static/vendors/socket/sockjs.min.js");让这个不要慌的iframe.html页面的sockjs.min.js在本地服务器找。
代码如下:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws/point").setAllowedOrigins("*").withSockJS().setClientLibraryUrl("http://192.168.7.3/static/vendors/socket/sockjs.min.js");
}
但是浏览器接到提示错误,上面的配置没能解决问题。
Opening Web Socket...
VM35 htmlfile:8 Uncaught DOMException: Blocked a frame with origin "http://192.168.7.5" from accessing a cross-origin frame.
at http://192.168.7.5/ws/point/884/fpmdavj3/htmlfile?c=_jp.aut3nay:8:20
(anonymous) @ VM35 htmlfile:8
VM35 htmlfile:10 Uncaught TypeError: Cannot read property 'message' of undefined
at p (VM35 htmlfile:10)
at VM36 htmlfile:13
p @ VM35 htmlfile:10
(anonymous) @ VM36 htmlfile:13
stomp.js:145 Whoops! Lost connection to http://192.168.7.5/ws/shootingpoint
sockjs.min1.js:2 GET http://192.168.7.5/ws/point/884/mcverdtb/jsonp?c=_jp.aedlpyr net::ERR_ABORTED 404
上面的修改都不能解决问题。最后只能更换centos服务器版本
安装CentOS-8.3.2011-x86_64-dvd1.iso,刷新,websocke是正确的。
而这个CentOS-8.1.1911-x86_64-dvd1.iso版本,配置的环境,websocke刷新的时候会报错。