1、vue工程
前端在vue的main文件全局添加一下代码:
import axios from 'axios';
axios.defaults.withCredentials=true;
也可以借鉴此博客自己编写过滤器
https://blog.csdn.net/weixin_42309402/article/details/105602926
2、后台添加cors-filter 配置
https://blog.csdn.net/M100782530qrcord/article/details/79957075
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
web.xml中添加配置
<filter>
<description>跨域过滤器</description>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
总结:分为两步
前台vue中添加axios 开启跨域
后台自己编写过滤器,或者使用cors-filter 插件