英文:
Inject user details as session scoped bean
问题
很久之前,我看到过一种关于在Spring控制器中获取有关当前登录用户信息的优雅方法的示例。但现在我似乎记不清楚了,也找不到任何示例(大多数示例都使用SecurityContextHolder或其他旧的方法来实现)。据我记得,大致是这样的:
@Controller
public class SomeController {
@Autowired
private Principal principal;
然后它会根据上下文(调用背后的用户是谁)注入不同的对象。但我无法确切地回忆起具体是如何完成的,或者注入了哪个对象。有人能帮我恢复记忆吗?我正在使用Keycloak作为IDP。
英文:
A while back I came across an example of an elegant way of getting hold of information about the currently logged in user in Spring controllers. But now I can't seem to remember exactly how, and I'm unable to find any examples (most are with SecurityContextHolder or other older ways of doing this). As far as I remember it was something like:
@Controller
public class SomeController {
@Autowired
private Principal principal;
And then it would inject a different object based on the context (which user was behind the invocation). But I can't exactly recall how it was done, or which object was injected. Can anyone refresh my memory? I'm using Keycloak as IDP.
专注分享java语言的经验与见解,让所有开发者获益!
评论