在centos下使用SockJS和STOMP.js运行时报错

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刷新的时候会报错。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值