Vue+SpringBoot解决session跨域问题

做了一个前后端分离,因为前后端的 session id不一致,导致前端请求时,后端的session读取不到对应的值,造成登录问题。

解决方法:

SpringBoot项目:

添加一个跨域配置

代码如下:

或者controller使用@CrossOrigin

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173") //前端项目的地址
                .allowCredentials(true)
                .allowedMethods("GET", "POST")
                .allowedHeaders("*")
                .maxAge(3600);
    }
}

前端,axios请求带cookie:

import axios from 'axios'
axios.defaults.withCredentials = true

亲测可以解决:

Vue+SpringBoot解决session跨域问题_第1张图片

更复杂的情景没有试过

你可能感兴趣的:(vue.js,javascript,前端)