300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > spring boot+secruity 跨域问题 cookie问题解决

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

时间:2021-12-18 16:04:54

相关推荐

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

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: {'^/': ''}}}},

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。