can not find entity using seam managed persistence context

389 Views Asked by At

I'm trying to change the authentication approach in my seam application. I currently use a login form to authenticate. In the future, I'd like to delegate the authentication to another layer that will rewrite every request with a specific HTTP header containing the username of authenticated user.

I'm facing a weird problem: when using login page to authenticate, I'm able to extract the user through the entityManager. But when I query the entityManager using the information off the header, I'm unable to find the user. The entityManager behave like the user does not exist.

I already tried two approaches:

  • Creating a fake login page which triggers the authentication process
  • Creating a servlet which gets the request and starts the authentication process

Both times, the entityManager fails to return me any user.

I read a lot about how seam manages the persistence context, but I didn't find a single explanation which make this issue clear. Do you have any ideas? suggestions? or even guesses?

the code which uses the entityManager is the following:

@Name("userService")
@AutoCreate
public class UserService {
    @Logger
    private Log logger;

    @In
    private EntityManager entityManager;

    public User getUser(String email) {
            try {
                return entityManager
                        .createQuery("SELECT u FROM User u where u.email=:email",
                                User.class).setParameter("email", email.trim())
                        .getSingleResult();
            } catch (NoResultException e) {
                return null;
            }
    }
}

The configuration for persistence context is:

<persistence:managed-persistence-context startup="false" scope="stateless"
        auto-create="true" name="entityManager" persistence-unit-jndi-name="java:/EntityManagerFactory" />

I created an empty fake login page which executes a page action (authentication) in which i get the request user header as the following:

@Name("applicationAuthenticator")
public class ApplicationAuthenticator {
    @Logger
    private Log log;

    @In
    private Identity identity;
    @In
    private Credentials credentials;
    @In(required=true)
    private UserService userService;

@Begin
    public void login() throws LoginException {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String userName=request.getHeader("user");
        identity.unAuthenticate();
        credentials.setUsername(userName);
        credentials.setPassword("fake");
        identity.acceptExternallyAuthenticatedPrincipal(new SimplePrincipal(credentials.getUsername()));
        User user=userService.getUserByEmail(credentials.getUsername());
        identity.authenticate();
        identity.quietLogin();
    }
}

Thx in advance :-)

1

There are 1 best solutions below

3
On

Thx @DaveB for your reply, the code which uses the entityManager is the following:

public User getUser(String email) {
        try {
            return entityManager
                    .createQuery("SELECT u FROM User u where u.email=:email",
                            User.class).setParameter("email", email.trim())
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

The configuration for persistence context is:

<persistence:managed-persistence-context startup="false" scope="stateless"
        auto-create="true" name="entityManager" persistence-unit-jndi-name="java:/EntityManagerFactory" />

I created an empty fake login page which executes a page action (authentication) in which i get the request user header as the following:

HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
        String userName = request.getHeader("user");