身份验证数据的 @ManagedProperty 对象为 null

发布于 2024-12-14 13:25:10 字数 1618 浏览 3 评论 0原文

我有以下托管 bean,它在容器身份验证后存储登录数据:

@ManagedBean(name = "authenticatedUserController")
@SessionScoped
public class AuthenticatedUserController implements Serializable {

@EJB
private jpa.UtentiportaleFacade ejbFacade;

  public Utentiportale getAuthenticatedUser() {
    if (AuthenticatedUser == null) {
        Principal principal =   FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if (principal != null) {
            AuthenticatedUser = ejbFacade.findByLogin(principal.getName()).get(0);
        }
    }
    return AuthenticatedUser;
}

getAuthenticatedUser 在每个页面中都会被调用,因为我将用户名放在右上角的 Facelets 模板中。 在 PermessimerceController(另一个托管bean)中,我需要访问登录数据,以便轻松快速地注入上述会话范围控制器:

@ManagedProperty(value = "#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

我遇到了以下问题:尝试访问链接到 PermessimerceController 的页面而不经过身份验证我被重定向到登录页面(这是可以的),但之后我得到一个空指针异常,因为当它被注入到 PermessimerceController 中时,authentiatedUserController 为空。 有问题的页面同时使用 PermessimerceController 和 AuthenticatedUserController,所以我应该猜测由于某种原因 PermessimerceController 是在 AuthenticatedUserController 之前创建的。你能建议一个简单的方法来解决这个问题吗? 或者,如何将登录数据存储在易于访问的位置?

谢谢 Filippo

我尝试编辑这篇文章,希望更好地澄清我遇到的问题并找到有用的答案。 使用 Facelets 模板,我通过 AuthenticatedUserController 的属性显示用户登录名。其余内容链接到 PermessimerceController,它需要一些有关用户的信息来过滤数据。 @ManagedBean 注释是实现此目的的简单方法。不幸的是,如果用户在未经身份验证的情况下访问该页面,则注入的 AuthenticatedUserController 将为 null。所以看来 PermessimerceController 是在 AuthenticatedUserController 之前创建的,我想知道为什么。我可以使用一个技巧来确保 AuthenticatedUserController 之前已创建吗?

I have the following managed bean which stores the login data after container authentication:

@ManagedBean(name = "authenticatedUserController")
@SessionScoped
public class AuthenticatedUserController implements Serializable {

@EJB
private jpa.UtentiportaleFacade ejbFacade;

  public Utentiportale getAuthenticatedUser() {
    if (AuthenticatedUser == null) {
        Principal principal =   FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if (principal != null) {
            AuthenticatedUser = ejbFacade.findByLogin(principal.getName()).get(0);
        }
    }
    return AuthenticatedUser;
}

getAuthenticatedUser is called in every page because I put the user name in a facelets template on the top right side.
In PermessimerceController, another managedbean, I need to access login data so it is easy and fast to inject the above session scoped controller:

@ManagedProperty(value = "#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

I experienced the following problem: trying to access the page which is linked to PermessimerceController without being authenticated I'm redirected to the login page (and this is OK) but after that I get a null pointer exception because authenticatedUserController is null when it is injected inside PermessimerceController.
The page in question uses both PermessimerceController and AuthenticatedUserController so I should guess that for some reason PermessimerceController is created before AuthenticatedUserController. Can you suggest a simple way to solve this problem ?
Alternatively how can I store the login data in an easy to access place ?

Thanks
Filippo

I try to edit this post in the hope to clarify better the problem I have and find useful answers.
Using facelets templating I show the user login name throught a property of AuthenticatedUserController. The rest of the content is linked to PermessimerceController which needs some informations about the user for filtering data. The @ManagedBean annotation is an easy way to accomplish this. Unfortunately if the user access that page without being authenticated the injected AuthenticatedUserController is null. So it seems PermessimerceController is created before AuthenticatedUserController and I wonder why. Is there a trick I can use for being sure AuthenticatedUserController is create before ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2024-12-21 13:25:10

您显然是在 bean 的构造函数中访问它:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

public PermessimerceController() {
    authenticatedUserController.getAuthenticatedUser(); // Fail!
}

这确实不会那样工作。 bean 是在注入依赖项之前构建的(想一想;依赖项注入管理器如何注入它?)

最早的访问点是 @PostConstruct 方法:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

@PostConstruct
public void init() {
    authenticatedUserController.getAuthenticatedUser(); // Success!
}

You were apparently accessing it in the bean's constructor:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

public PermessimerceController() {
    authenticatedUserController.getAuthenticatedUser(); // Fail!
}

This will indeed not work that way. The bean is constructed before the dependencies are injected (think about it; how else would the dependency injection manager inject it?)

The earliest access point is a @PostConstruct method:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

@PostConstruct
public void init() {
    authenticatedUserController.getAuthenticatedUser(); // Success!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文