Yea, hunt around for references to "Programmatic Login" for Glassfish. It's a container specific API, but will allow you to log in on demand using the same authentication system that the container is configured with.
[Message sent by forum member 'whartung' (whartung)]
http://forums.java.net/jive/thread.jspa?messageID=287947