I have created a Javae EE app using JAX-RS 2.0 and JPA. I created a special provider of my User entity (using Qualifiers) to provide the current user (logged in) as entity from applications user database. To get current user i use
@Context
private SecurityContext secContext;
The problem is that this is null. The security is setup fine (Wildfly 8.2) - the app asks for Authentication (basic) but SecurityContext
is null. Here is the code:
@RequestScoped
public class CurrentUserProducer implements Serializable {
/**
* Default
*/
private static final long serialVersionUID = 1L;
@Context
private SecurityContext secContext;
/**
* Tries to find logged in user in user db (by name) and returns it. If not
* found a new user with role {@link UserRole#USER} is created.
*
* @return found user a new user with role user
*/
@Produces
@CurrentUser
public User getCurrentUser() {
if (secContext == null) {
throw new IllegalStateException("Can't inject security context - security context is null.");
//... code to retrieve or create new user
return user;
}
}
As you see I check secContext
for null and i see my exception as soon as i try to reach a resource that injects @CurrentUser
.
So how to fix this? Why is SecurityContext
null.