网站首页 > 技术文章 正文
1.如果security的配置中使用了sessionManagement()会话管理,如下:
2.则前端的axios需要开启withCredentials为true的设置。如果不设置或者设置为false,登录成功后,其他接口会报“还没登录”方面的错误(302的错误)。
说明:
withCredentials:跨域请求是否提供凭据信息(cookie、HTTP认证及客户端SSL证明等)
3.如果后端不配置allowCredentials(true)。接口就会报错。如下:
Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:8090' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
4.所有后端需要在跨域的配置地方,配置allowCredentials(true)。如下:
5.还要注意后端配置时,要使用allowedOriginPatterns(),而不是allowedOrigin(),否则也会报跨域问题。
6.登录之后,怎样获取用户的信息数据呢
可以从SecurityContextHolder上下文的持有者中获取上下文,再获取登录认证的实体,从而拿到保存用户信息的Principal对象。
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- 上一篇: 30s 就可以掌握的 Nginx 片段
- 下一篇: 前后端分离session问题
猜你喜欢
- 2025-01-21 用vue3实现一个简单的登录功能
- 2025-01-21 SpringBoot与Vue交互解决跨域问题「亲测已解决」
- 2025-01-21 细解跨域以及跨域的解决方案
- 2025-01-21 52、Vue 怎么实现跨域(必会)
- 2025-01-21 聊聊axios-axios的介绍以及请求方式
- 2025-01-21 跨域请求如何携带cookie?不小心都拿了Offer
- 2025-01-21 前后端分离session问题
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)