You need to create a so-called "LDAP Realm" on the servletcontainer. How to do this depends on the servletcontainer used. As you didn't give any details about the servletcontainer used, it's hard to give a suitable answer, but in general just reading the servletcontainer's documentation about Realm configuration ought to be sufficient. In case of for example Tomcat, it's the Realm Configuration HOW-TO. For Tomcat, you would need a JNDIRealm
. More detail can be found in the JSP wiki.
Then you need to configure your web application to require a login for the particular pages by declaring the appropriate <security-constraint>
entry in web.xml
. You can configure the login and error page in <login-config>
entry in the very same web.xml
.
<security-constraint>
<web-resource-collection>
<web-resource-name>secured</web-resource-name>
<url-pattern>/secured/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>X</role-name> <!-- Should be your AD group name. -->
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/error.xhtml</form-error-page>
</form-login-config>
</login-config>
The login form should POST to j_security_check
and use j_username
and j_password
as input field names.
<form action="j_security_check" method="post">
<input type="text" name="j_username" />
<input type="password" name="j_password" />
<input type="submit" value="login" />
</form>
If you want more fine grained control over validation and thus want to use JSF <h:inputText required="true" />
and so on, then you could also submit to a backing bean action method which in turn invokes HttpServletRequest#login()
. See also Performing user authentication in Java EE / JSF using j_security_check
As to getting the name of the logged-in user, just use ExternalContext#getRemoteUser()
in JSF context or HttpServletRequest#getRemoteUser()
in servlet context. You can access it in JSF EL as well as follows:
<p>Welcome, #{request.remoteUser}</p>
The system property indeed returns the server's own user which makes absoutely no sense in this context.