gpt4 book ai didi

cdi - 纠察队、envers 和 cdi 注入(inject)

转载 作者:行者123 更新时间:2023-12-01 02:23:00 27 4
gpt4 key购买 nike

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

生产者类:

@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());
}
}

最佳答案

我有一个类似的问题。注入(inject)不起作用,因为 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);
}

关于cdi - 纠察队、envers 和 cdi 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com