spring boot+secruity 跨域问题,cookie问题解决

spring boot+secruity 跨域问题,未带cookie,导致session失效问题解决

前端vue修改

import axios from "axios"
axios.defaults.withCredentials=true //允许cookie跨域
Vue.config.productionTip = false
Vue.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.添加过滤器 配置
@Configuration
public class cors {
    @Bean
    public WebMvcConfigurer corsConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedHeaders("*")
                        .allowedMethods("*")
                        .allowedOriginPatterns("*") 
                        .allowCredentials(true); //允许cookie跨域
            }
        };
    }
}

到此成功解决跨越cookie问题

如果还没成功的话 前端 vue.comfig.js配置 代理

module.exports = {
    devServer: {
        host: 'localhost',
        port: 8080, //前端项目端口
        proxy: {
            '/':{
                ws: false,
// 代理ip:端口
                target: 'http://xxxxxxxxx:xxx',
                changeOrigin: true,
                pathRewrite: {
                '^/': ''
                }
            }
        }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值