Reputation: 9358
I use JAAS authentication in my application. Think think that I have User Entity class like this:
@Entity
public class User {
@Id
public Long id;
public String name;
...
and a rest API like this:
@Stateless
@LocalBean
@Path("/users")
public class Users {
// INJECT USER OBJECT HERE
@Inject
private User user;
@Path("/me")
@Produces({ MediaType.APPLICATION_JSON })
@GET
public User getUser() {
return user;
}
...
Is there anyway I create a @Produces
method to generate User entity object from @SessionContext.getCallerPrincipal()
and EntityManager and Inject it directly into the all rest apis that I want?
Upvotes: 1
Views: 456
Reputation: 151
When you saved the id of user as principle name following way should work. You need a qualifier and a producer method. A good way is to create another class to get the user entity.
public class Resource {
@Inject
private EntityManager em;
@Resource
private SessionContext ctx;
@Produces
@Sessionscoped
@Running // the qualifier
public User getUser() {
User user = em.find(User.class, ctx.getCallerPrincipal().getName());
return user;
}
}
In your Users
class, you have to annotate the injected user with the qualifier to. Otherwise the User entity of the producer method can not be alloacted to the injection.
@Inject
@Running
private User user;
Upvotes: 2