将用户详细信息注入为会话范围的Bean。

huangapple 未分类评论50阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月29日 15:28:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/62080824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定