Pages

Showing posts with label ldap. Show all posts
Showing posts with label ldap. Show all posts

Sunday, 18 August 2013

Installation of Apache Directory for creating LDAP server

This post will show you how to install Apache Directory to create a LDAP server and insert few user records in it which will be used as LDAP authentication for our application.

Firstly we need to download the Apache Directory studio, please visit apache site http://directory.apache.org/studio/download/download-windows.html and download the required version.

Once it is downloaded double click on the exe and follow the steps given below:







Now your installation of Apache directory is done. We will now create an LDAP server on it.

For creating a LDAP server  open the apache directory and go to the LDAP Servers tab and right click --> New --> New Server


Give your server a name and choose one of the listed apache foundation servers and click finish

You can view the configuration properties of your server by right clicking on the server --> Open Configuration. These properties will be used while connecting to the applications.

Now you need to start the server. Right click on the server --> run.

Once the server is started. Right click on the server and create a new LDAP connection by clicking --> Create a connection.

You will get a message for the creation of the new server.

Once the connection is created now we are going to add few user credentials for this go to the LDAP browser and expand DIT --> ou=system --> ou=users as shown below. Right click on ou=users --> New  --> New Entry.

Create entry from scratch --> Next

Select inetOrgPerson from the available object classes --> Add --> Next.

For RDN select uid from the list and enter a username against it. It should be a unique value. This will act as the user for your application.

Update sn and cn value where sn = surname and cn = common name.


As we need a password also for the user against which the user will be authenticated we have to add a new attribute for the password. Right click --> New Attribute


Select userPassword from the list --> Next --> finish.

Enter the password in the Password Editor and press OK.

Now the user has been added to the directory and you can use it for the LDAP authentication. You can now access your LDAP server at ldap://localhost:10389 (see server configurations). Hope this will help you.

LDAP implementation

LDAP stands for  Lightweight Directory Access Protocol and it is used for user authentication, user provisioning, authorization, feeds, and views.



This post will help you implement a LDAP onto your java application.

Here first we will be preparing a Login page where we accept user credentials and we authenticate the user against the LDAP and only when the user is authenticated as per the active directory he/she will be allowed to access the application. For now i have just put the success and failure pages which will be redirected based on the user authentication you can customize according to your needs.

If you don't have any directory (LDAP Url) where you can test the LDAP the best way is to install an Apache directory Studio and insert few user credentials in it and test on it. The detailed instructions on how to install an Apache directory studio and insert user credentials in it is available in my other post http://technsolution.blogspot.in/2013/08/installation-of-apache-directory-for.html

Here is the code:


login.html

<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;color:#00FF00;>
Simple Login Page
</h1>
<form name="login" action="Login" method="post">
Username : <input type="text" name="username"/>
Password : <input type="password" name="password"/>
<input type="submit" name="submit" value="Enter" style="background-color: #FFA500;width: 100 ">

</form>


</body>


</html>


Login.java

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

public class Login extends HttpServlet implements Servlet {


/**


*/
private static final long serialVersionUID = 1L;



public Login() {

super();
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


final String SUCCESS = "/success.html";

final String FAILURE = "/failure.html";
String strUrl = "/login.html";
String username = request.getParameter("username");
String password = request.getParameter("password");

Hashtable<String,String> env = new Hashtable<String,String>(11);

boolean b = false;


env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +",ou=users,ou=system");
env.put(Context.SECURITY_CREDENTIALS, password);

System.out.println("User str :: "+ "uid="+ username +",ou=users,ou=system");
System.out.println("Password Str :: "+ password);


       

try {
// Create initial context
DirContext ctx = new InitialDirContext(env);

// Close the context when we're done
b = true;
ctx.close();

} catch (NamingException e) {

b = false;
}finally{
if(b){
System.out.print("Success");
strUrl = SUCCESS;
}else{
System.out.print("Failure");
strUrl = FAILURE;
}
}

RequestDispatcher rd = getServletContext().getRequestDispatcher(strUrl);
rd.forward(request, response);


}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

processRequest(request,response);

}


success.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success Page</title>
</head>
<body>
 <h1>Success</h1>
</body>

</html>


failure.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Failure Page</title>
</head>
<body>
 <h1>Failure</h1>
</body>

</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Test</display-name>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
<servlet-name>login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>

</web-app>

Thats it you are done with a basic application having LDAP implemented. Hope this would have helped you.