Pages

Sunday 18 August 2013

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.


2 comments: