【问题标题】:Spring @AuthenticationPrincipal as Autowired objectSpring @AuthenticationPrincipal 作为 Autowired 对象
【发布时间】:2020-03-09 09:04:19
【问题描述】:

我发现我可以在methods 中使用@AuthenticationPrincipal 来获取当前用户/主体。有没有办法为一个类使用相同/相似的属性?例如

public class MySample  {
  @AutowireCurrentUser // (?)
  public UserObject currentUser; 
}

我发现类似的东西是possible,但我想在创建类后立即自动分配值。但是怎么做?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    我非常关注this answer。如果您想将此代码提取到您自己的项目中,请注意两点:

    1. 密切关注 class 注释 @Component - 否则 BeanPostProcessor 不会注册
    2. 确保使用正确的接口(例如UserDetails) - 如果您想获得例如getId() [如果你自己实现这个方法]。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2016-02-10
      • 2018-09-25
      • 1970-01-01
      • 2020-03-10
      • 2014-12-18
      • 2020-09-01
      相关资源
      最近更新 更多