在 Hibernate 中更新后如何重新加载 Spring Security Principal? [英] How to reload Spring Security Principal after updating in Hibernate?
问题描述
这一定是一个普遍的问题……我觉得在谷歌搜索和 SOing 之后,我一定是没有仔细环顾四周寻找答案,或者没有人问过它……所以请原谅我.
This must be a common issue... and I feel that after googling, and SOing I must have just not looked around thoroughly for the answer enough or that no1 has asked it... so please forgive me.
我正在使用 Spring Security 和 Hibernate 等
I am using Spring Security with Hibernate etc.
因此用户/委托人已登录并对其个人资料进行了一些更改.
So a User/principal has logged in and made some changes to their profile.
我使用我的 DAO 更新配置文件 (UserDetails),并且我希望我的 Principal 自动反映此更新.
I use my DAO to update the profile (UserDetails), and I want my Principal to automatically reflect this update.
然而,当我再次获得 Principal 时,我得到了脏版本(从我最初的登录开始).
However when I get the Principal again, I get the dirty version (from my initial login).
有谁知道如何让 Spring Security 从 Hibernate 重新加载更新的 UserDetails?
Does anyone know of how I can get Spring Security to reload from Hibernate the updated UserDetails?
推荐答案
OK 翻来覆去终于找到答案了.
OK dug around and finally found the answer.
我们可以创建一个 UsernamePasswordAuthenticationToken 并将更新的 Principal 分配给上下文.
We can create a UsernamePasswordAuthenticationToken and assign the updated Principal to the context.
Authentication authentication = new UsernamePasswordAuthenticationToken(userObject, userObject.getPassword(), userObject.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
另请参阅如何在 Spring Security/SpringMVC 中手动设置经过身份验证的用户".
这篇关于在 Hibernate 中更新后如何重新加载 Spring Security Principal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!