4

我正在使用 picketlink 对项目中的用户进行身份验证。我还创建了一个 @produces 注释方法,因此我可以在其他地方注入经过身份验证的用户。现在,我正在使用 envers,除了默认信息之外,我还想存储执行该操作的用户,但我无法将它注入到 envers 侦听器中。它始终为空。我怎样才能进行这种注射,或检索这些信息?

生产者类:

@SessionScoped
public class Resources implements Serializable {
    private static final long serialVersionUID = 1L;

    @EJB
    private AuthenticationManagerBean authenticator;

    @Inject
    private Identity credentials;

    @CurrentUser
    private AuthenticatedUser currentUser;

    @Produces
    @CurrentUser
    @SessionScoped
    private AuthenticatedUser createAuthenticatedUser() {
        AuthenticatedUser user = new AuthenticatedUser();
        org.picketlink.idm.model.basic.User loggedInUser = (org.picketlink.idm.model.basic.User) credentials.getAccount();

        User pu = authenticator.getUserRoles(loggedInUser.getLoginName());

        if (pu != null) {
            user.setUser(pu.getName());

            for (Role role : pu.getRoles()) {
                user.getRoles().add(role.getName());
            }
        }

        return user;
    }

    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

和 envers 监听器:

public class AuditListener implements RevisionListener, Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    @CurrentUser
    private AuthenticatedUser identity; //this is always null

    public void newRevision(Object revisionEntity) {

        System.out.println(identity.getUser());
    }
}
4

1 回答 1

3

我有一个类似的问题。注入不起作用,因为 RevisionListener 不受 CDI 管理。这样,您必须自己查找 bean。这是你可以做到的方式:

public AuthenticatedUser getAuthenticatedUser() {
    BeanManager beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
    Bean<AuthenticatedUser> bean = (Bean<AuthenticatedUser>) beanManager.getBeans(AuthenticatedUser.class, new AnnotationLiteral<CurrentUser>() {
    }).iterator().next();

    CreationalContext<AuthenticatedUser> ctx = beanManager.createCreationalContext(bean);
    return (AuthenticatedUser) beanManager.getReference(bean, AuthenticatedUser.class, ctx);
}
于 2013-11-01T14:43:35.063 回答