spring boot+secruity 跨域问题,未带cookie,导致session失效问题解决
前端vue修改
import axios from "axios"axios.defaults.withCredentials=true //允许cookie跨域Vue.config.productionTip = falseVue.prototype.$axios = axios//初始化axios
后端修改
1. 修改 securyconfig 继承 WebSecurityConfigurerAdapter中 configure(HttpSecurity http)方法加上.cors().and().csrf().disable()
protected void configure(HttpSecurity http) throws Exception {authorizeRequests().permitAll().and().cors().and().csrf().disable()
2.添加过滤器 配置
@Configurationpublic class cors {@Beanpublic WebMvcConfigurer corsConfigurer(){return new WebMvcConfigurer() {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedHeaders("*").allowedMethods("*").allowedOriginPatterns("*") .allowCredentials(true); //允许cookie跨域}};}}
到此成功解决跨越cookie问题
如果还没成功的话 前端 fig.js配置 代理
module.exports = {devServer: {host: 'localhost',port: 8080, //前端项目端口proxy: {'/':{ws: false,// 代理ip:端口target: 'http://xxxxxxxxx:xxx',changeOrigin: true,pathRewrite: {'^/': ''}}}},