[Retour index]

Tomcat - Authentification LDAP

L'idée est de montrer comment gérer l'authentification d'un utilisateur pour l'accès à un service web (sous Tomcat) auprès d'un annuaire LDAP.
1. Description de l'annuaire LDAP
2. Configuration du fichier web.xml de l'application
3. Configuration du fichier server.xml
4. Quelques liens

1. Description de l'annuaire LDAP

Avant de montrer comment authentifier un utilisateur enregistré dans un annuaire LDAP, décrivons brièvement la structure de l'annuaire utilisée (en l'occurrence, l'annuaire LDAP de l'ENST BRETAGNE à RENNES). Voici le suffixe (le point d'entrée de l'annuaire conforme au rfc 2377) : L'arbre de nommage est le suivant :
dc=fr
|_ dc=enst-bretagne
| |_ o=autres
| |	|_ ou=group
| |	|_ ou=netgroup
| |	|_ ou=divers
| |	...
| |	|_ cn=replicator
| |_ o=personnel
|	|_ ou=autre personnel
|	|_ ou=élèves
|	|_ cn=loginserver
|	|_ ou=permanents
|	|_ ou=thésards
Le distinguished name (dn) de chaque objet est basé sur l'attribut uid qui représente le nom de login et doit être unique sous chaque noeud de l'arbre. La figure qui suit donne un exemple d'entrée au format ldif:
[root@localhost root]# ldapsearch -b "dc=enst-bretagne,dc=fr" -h ldapserver 
-LLL -x uid=alenorcy
dn: uid=alenorcy,ou=autre personnel,o=personnel,dc=enst-bretagne,dc=fr
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: alenorcy
sn: alenorcy
uid: alenorcy
uidNumber: 8433
gidNumber: 903
homeDirectory: /users/disi/stage/alenorcy
gecos: LE NORCY Arnaud - Stag. DISI
loginShell: /bin/tcsh
mail: Arnaud.LENORCY@enst-bretagne.fr
registeredAddress: alenorcy@rennes

2. Configuration du fichier web.xml de l'application

Pour l'authentification, il y a plusieurs mécanismes : BASIC, DIGEST, FORM et CLIENT-CERT. Nous utiliserons, dans notre cas, l'authentification par un formulaire (authentification FORM), ainsi, cette méthode permet de s'intégrer aisément à la charte graphique du site web. Voici le code à insérer dans le fichier $TOMCAT_HOME/webapps/monApplication/WEB-INF/web.xml de l'application "monApplication" pour gérer l'authentification des utilisateurs :
 <security-constraint>
		<display-name>Tomcat Server Configuration Security 
			Constraint</display-name>
		<web-resource-collection>
			<web-resource-name>Protected Area</web-resource-name>
			<!-- Define the context-relative URL(s) to be protected -->
			<url-pattern>/services/*</url-pattern>
			<http-method>GET</http-method>
			<http-method>POST</http-method>
		</web-resource-collection>
		<auth-constraint>
			<role-name>*</role-name>
		</auth-constraint>
		<user-data-constraint>
			<transport-guarantee>CONFIDENTIAL</transport-guarantee>
		</user-data-constraint>
 </security-constraint>
	<!-- Login configuration uses form-based authentication -->
 <login-config>
		<auth-method>FORM</auth-method>
		<realm-name>Form-Based Authentication
			</realm-name>
		<form-login-config>
			<form-login-page>/login.jsp</form-login-page>
			<form-error-page>/error.jsp</form-error-page>
		</form-login-config>
 </login-config>
 <!-- Security roles referenced by this web application -->
 <security-role>
		<!--
		<description> The role that is required to log in to the 
		       Administration Application </description>
		-->
		<role-name>*</role-name>
 </security-role>
Voici le formulaire (balise HTML "form") à insérer dans un fichier nommé login.jsp (dans notre cas) :
  <form method="POST" action="j_security_check">
    <table>
      <tr>
        <td>Login :</td>
        <td><input type="text" name="j_username"></td>
      </tr>
      <tr>
        <td>Mot de passe :</td>
        <td><input type="password" name="j_password"></td>
      </tr>
      <tr>
        <td><input type="submit" value="Entrer"></td>
        <td><input type="reset" value="Annuler"></td>
      </tr>
    </table>
  </form>
Les variables action="j_security_check", name="j_username" et name="j_password" sont indispensables. Le fichier error.jsp est totalement libre, voici un exemple :
	<html>
	  <head>
	    <title>Echec de l'authentifciation</title>
	  </head>
	  <body>
	    <h2>Echec de l'authentifciation : 
	    <a href="/">nouvelle tentative?</a>
	    </h2>
	  </body>
	</html>

3. Configuration du fichier server.xml

Configuration du Realm dans $TOMCAT_HOME/conf/server.xml :
 <Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
	connectionURL="ldap://ldap.rennes.enst-bretagne.fr:389"
	userBase="o=personnel,dc=enst-bretagne,dc=fr"
	userSubtree="true"
	userSearch="(uid={0})"
 />
Signification des Attributs :
Permettre qu'aux permanent de s'authentifier :
 <Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
	connectionURL="ldap://ldap.rennes.enst-bretagne.fr:389"
	userBase="ou=permanents,o=personnel,dc=enst-bretagne,dc=fr"
	userSearch="(uid={0})"
 />
Solution similaire :
 <Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
	connectionURL="ldap://ldap.rennes.enst-bretagne.fr:389"
	userPattern="uid={0},ou=permanents,o=personnel,dc=enst-bretagne,dc=fr"
 />

4. Quelques liens


Un commentaire, une remarque? - 4 commentaire(s) :

UpsernVer a commenté, le 04/01/2012 à 10:03:32 :
<a href=http://www.flickr.com/people/73433313@N06/>buying online zithromax</a>

http://www.flickr.com/people/73433313@N06/

Nancy a commenté, le 01/10/2011 à 07:24:18 :
Thanks for the share!
Nancy.R

Kixwigire a commenté, le 27/09/2011 à 06:30:08 :
<a href=http://xvioqjqx.com>Hello :)</a>

Anthony a commenté, le 04/09/2011 à 11:07:30 :
Very nice, i suggest webmaster can set up a forum, so that we can talk and communicate.


MAJ le 15/06/2005
Valid XHTML 1.1! Valid CSS!