I've got a Java doLogin()
method invoked from a JSF page that gets an id (String netId
) and password (String password
) from the user. doLogin()
initiates the authentication using netId
as the principal in an Active Directory login. After that, I would like to get other attributes besides principal name from the Directory that secures my app.
My security is configured in the container & it works, such that
HttpSession ses = FacesContext.getCurrentInstance().getExternalContext().getSession (false);
HttpServletRequest req = FacesContext.getCurrentInstance().getExternalContext().getRequest();
req.login(netID, password);
is successful and
req.getUserPrincipal().getName();
returns the user's netID
. However, my app uses the netId
only for authentication. Other attributes (commonName
for example) are needed for other parts of the app that access another database. I want to do something like
usefulLDAPobj = *getLDAPSession from "somewhere" in the HTTP Session, the FacesContext or some other available object*
String cn = usefulLDAPobj.getAttributeFromProfile ("cn");
ses.setAttribute("username", cn);
and from then on use username, stored in the session, in my Hibernate ORM.
I know the simple-minded usefulLDAPobj.getAttributeFromProfile ("cn")
will be more complex, but I can fill that out if I can find a starting point that gets me access to the LDAP Directory.
Since there is an obvious LDAP connection being set up by the container I feel there must be a way for me to make use of it without having to manually build up an LdapContext programatically; which would require the code to know all the LDAP server / bind-DN / bind-password configuration
that the web server (JBoss EAP 6.2) already knows about (from the <login-module>
defined in standalone.xml
). For example, methods like getUserPrincipal()
and isUserInRole()
need access to the very same Directory profile that I want access to.
So my question is: is there a way to get an LDAP connection or context from a FacesContext or a HTTPServletRequest or any objects accessible from an HTTPServlet?