Picket, enver and cdi injection

I use picketlink to authenticate a user in a project. I also created @produces annotated method, so I could introduce the authenticated user to other places. Now I use envers, and in addition to the default information, I would like to save the user who performed the action, but I cannot enter it into the envers receiver. It is always zero. How can I make this injection or get this information?

Manufacturer Class:

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

and receiver 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
source share
1 answer

I had a similar problem. Injection does not work because the RegisionListener is not controlled by CDI. So you need to search for a bean yourself. So you can do this:

 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); } 
+3
source

Source: https://habr.com/ru/post/1502789/


All Articles