vue、springboot集成websocket跨域问题解决

2023-05-16

       由于浏览器连接的是服务器上的websocket,导致出现了如下跨域错误:

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://a.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute。

     先分析下出现这个问题的原因:由于前后端分离导致我们通常会写跨域配置(如下代码)。但是报错很明显指出request's credentials mode is 'include',那么响应就the response must not be the wildcard '*’。所以直接配置Access-Control-Allow-Origin的值为通配是行不通的。具体怎么解决我接下来会讲。

package cn.datainvest.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨越配置类
 * @author yangfeng
 *
 */
@Configuration
public class CorsConfig {

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * 跨域过滤器
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

springboot集成websocket步骤如下:

1.引入依赖

        <!--Websocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

2:WebSocketConfig

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * @author yangfeng
 **/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
    }
}

3.业务代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
public class Weather {

    @Resource
    private IWeatherService weatherService;

    private static Logger LOG = LoggerFactory.getLogger(WeatherScheduler.class);

    @Autowired
    private SimpMessagingTemplate messagingTemplate;


    /**
     * 保存天气信息
     */
    public void saveWeather() {
        ServiceResult<WeatherAO> serviceResult = weatherService.getWeatherAndSave();
        if (serviceResult != null && serviceResult.isSucceed() && serviceResult.getData() != null) {
            WeatherAO weather = serviceResult.getData();
            //发送数据到webscoket
            messagingTemplate.convertAndSend("/topic/weather", weather);
        }
    }

}

4.vue连接socket,开发环境访问地址:localhost:8089。很明显连接这里出现跨域了。

<template>
  <div class="">、
    <h1>Stock Price</h1>
    <div>
      <button @click="senMessage">发送消息</button>
      <ul>
        <li v-for="m in list1" :key="m.name">{{ m.name }}: {{ m.price }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
  import SockJS from 'sockjs-client'
  import Stomp from 'webstomp-client'
  export default {
    data() {
      return {
        list1: [],
        stompClient: null

      }
    },
    mounted() {
      this.initWebSocket()
    },
    methods: {
      senMessage() {
        this.stompClient.send('/app/hello')
      },
      initWebSocket() {
        this.connection()
        // 需要有一个失败重连得到问题
      },
      connection() {
        // 更换that指针
        const socket = new SockJS('http://112.174.126.131:8080/websocket')
        this.stompClient = Stomp.over(socket)
//建立连接,订阅主题
        this.stompClient.connect({}, (frame) => {
          console.log(frame)
          this.stompClient.subscribe('/topic/terminalTrace', (val) => {
            // this.list1 = JSON.parse(val.body)
            console.log('-------++++++++++++++++++++++++++++++------------')
//下面会报错,应该是json的问题,但是数据可以接收到
            console.log(val.body)
          })
          this.stompClient.subscribe('/topic/weather', (val) => {
            // this.list1 = JSON.parse(val.body)
            console.log('-------++++++++++++++++++++++++++++++------------')
//下面会报错,应该是json的问题,但是数据可以接收到
            console.log(val.body)
          })
        })
        // 回调函数 3 end
      }
    }
  }
</script>

5.使用filter来解决

   把之前写的跨域类CorsConfig 删除,加入下面的filter。

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Component
public class CorsFilter implements Filter {

    // This is to be replaced with a list of domains allowed to access the server
    //You can include more than one origin here
    private final List<String> allowedOrigins = Arrays.asList("http://localhost:8089");

    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // Lets make sure that we are working with HTTP (that is, against HttpServletRequest and HttpServletResponse objects)
        if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;

            // Access-Control-Allow-Origin
            String origin = request.getHeader("Origin");
            response.setHeader("Access-Control-Allow-Origin", allowedOrigins.contains(origin) ? origin : "*");
            response.setHeader("Vary", "Origin");

            // Access-Control-Max-Age
            response.setHeader("Access-Control-Max-Age", "3600");

            // Access-Control-Allow-Credentials
            response.setHeader("Access-Control-Allow-Credentials", "true");

            // Access-Control-Allow-Methods
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");

            // Access-Control-Allow-Headers
            response.setHeader("Access-Control-Allow-Headers",
                    "Origin, X-Requested-With, Content-Type, Accept, " + "X-CSRF-TOKEN");
        }

        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {
    }
}

加入origin的路径配置: 

6.测试

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue、springboot集成websocket跨域问题解决 的相关文章

随机推荐

  • keil 工程头文件包涵及结构体重定义问题

    最近在写一个太阳能电池控制板的项目 xff0c 这两天真是收获颇丰 xff0c 把指针又从新学了一遍 xff0c 还是项目坑死人啊啊啊啊啊 下面是我在建立工程时遇到的问题 xff1a 1 头文件包涵问题 xff1a 原先写的程序都是小程序
  • Openmediavault 4.1.3 镜像下载地址

    Openmediavault 4 1 3 镜像下载地址 https nchc dl sourceforge net project openmediavault 4 1 3 openmediavault 4 1 3 amd64 iso
  • 【leetcode】【77】Combinations

    一 问题描述 Given two integers n and k return all possible combinations of k numbers out of 1 n For example If n 61 4 and k 6
  • 激光雷达介绍

    全球汽车行业正在进行自动化变革 xff0c 这将彻底改变交通运输的安全和效率水平 戴姆勒在S级豪华车型中引入L3级自动驾驶 xff08 L3 xff0c 在特定条件下自动驾驶 xff0c 人类驾驶员一旦被请求就会随时接管 xff09 是自动
  • get请求 包括username和password

    方法一 xff1a public static String wisdomNetMessagePlat String sn String encoding throws Exception String result 61 34 34 St
  • Arduino控制L298N

    一般做智能小车或者DIY开发的童鞋需要用到Arduino这狂开发板 xff0c 作为一名好奇心很强的软件开发人员 xff0c 我自然也想尝尝鲜特别是利用Arduino做一些人机交互的项目 xff0c 如果结合了Kinect AR增强现实 x
  • gitlab打tag的作用,以及如何基于tag切分支

    在 GitLab 中 xff0c Tag 是将 Git 项目中特定的提交标记为版本的一种方式 Tag 能够用于标记发布的版本 xff0c 以便于对代码的每个版本进行管理和追踪 打 Tag 的作用主要有以下几点 xff1a 标记版本发布 xf
  • 解析包含用户名密码的FTPURL

    include lt stdio h gt include lt string h gt void parse ftpurl char url char user char passwd char ipaddr uint port char
  • nvidia jetson xavier nx Deepstream Yolov3示例模型运行

    最近在玩nvidia jetson xavier nx的板子 xff0c 特在此做一些笔记 1 进入sample目录 cd opt nvidia deepstream deepstream sources objectDetector Yo
  • go-mysql-elasticsearch+mysql 同步 ElasticSearch(标贝科技)

    标贝科技 https ai data baker com source 61 qwer12 填写邀请码fwwqgs xff0c 每日免费调用量还可以翻倍 一 Elasticsearch xff1a https www elastic co
  • Intel Realsense D400系列相机自校准细节,减少踩坑!!

    自校准 xff08 对任意环境非白墙 注意距离0 4 2米之间 xff0c 深度 xff08 相机拍摄内容中物体所占空间 xff09 50 xff09 xff1a 打开Intel realSense viewer xff1a 连接设备 xf
  • QT实现NMea截取指定片段字符串——$GNGGA(小白第一次编,后期有机会完善代码,高勿)

    总体思路 xff1a 1 读取所有数据 xff08 readAll xff08 xff09 xff09 QByteArray readbuf 61 serial gt readAll 2 查找标志位 xff08 QString中使用inde
  • linux c&&c++关于赋值问题(char*传给另一个char*)

    仅列出四种 xff0c 欢迎补充 xff01 方法一 xff1a 直接 char buf1 61 char buf2 xff0c xff08 xff01 xff01 xff01 这里的赋值是将buf2的地址赋给了buf1 xff0c 此后b
  • B6AC使用说明

    1 接线方式 接线如下图所示 xff0c 如果电池是XT60 xff0c 可以用接鳄鱼夹子的T插夹在XT60的接口上 2 设置方法 2 1 锂电池充电 1 xff09 Batt Type gt 切换选择 LiPo BATT gt Enter
  • 整数转换为字符串

    include lt stdio h gt 反转字符串 char reverse char s char temp char p 61 s p指向s的头部 char q 61 s q指向s的尾部 while q 43 43 q q 交换移动
  • 【工具使用】Modbus Poll软件使用详解

    软件介绍 Modbus Poll是一个模拟Modbus协议主机的上位机软件 xff0c 主要用于模拟测试跟其他从机设备通信的过程 与之成套存在的另一个软件 Modbus Slave xff0c 则是模拟Modbus协议从机的上位机软件 该软
  • c++下程序的运行(第3方库的安装及安装gdal)

    C 43 43 安装第三方库 1 概览2 编译 xff0c 首先要熟悉程序编译过程 xff08 预处理 gt 编译 gt 汇编 gt 链接 xff09 2 1 编译流程4步2 1 1 1 预处理2 1 2 2 编译2 1 3 3 汇编2 1
  • 制作IEC101/104报文解析工具

    文章目录 准备 一 IEC101 104协议 二 制作解析工具步骤 1 建立协议族 2 建立协议 3 建立术语 4 建立报文块 5 建立报文解析规则 总结 准备 这里是基于 在线解析二进制报文 文章中介绍的物联网助手来快速搭建IEC101
  • PHP各版本技术特性(标贝科技)

    文章目录 PHP8JIT流程配置测试小结 match表达式 PHP7HugepageHugepage简介Hugepage配置 Opcache file cacheOpcache file cache简介Opcache file cache配
  • vue、springboot集成websocket跨域问题解决

    由于浏览器连接的是服务器上的websocket xff0c 导致出现了如下跨域错误 xff1a The value of the 39 Access Control Allow Origin 39 header in the respons