Skip to main content

Simple LDAP Authentication

This is a simple example through which we will connect to the LDAP Server and authenticate user.
I have used ApacheDS Server as a LDAP Server. Install ApacheDS server and run it.

Following is a servlet used. make login.html and use as it Login.java


package ajaxdemo.action;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
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 javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

 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 env = new Hashtable(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=system");
  env.put(Context.SECURITY_CREDENTIALS, 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 = request.getRequestDispatcher(strUrl);
  rd.forward(request, response);

 }

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

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  processRequest(request, response);
 }
}
Enjoy LDAP!! :)

Comments

  1. Can I have java code to reset ldap user password ?

    ReplyDelete
  2. Hi I am getting NameNotFoundException while using LDAP against Active directory , any help ?

    ReplyDelete
  3. Nice tutorial,by the way LDAP authentication is quite easy to implement for active directory or any other LDAP server if you use spring security. you can do this by just adding few configs and it lot more maintainable. See How to authenticate against LDAP active directory using Spring for step by step guide

    ReplyDelete

Post a Comment

Popular posts from this blog

Getting height and width of image in Java

Yesterday I was working on something that requires manipulation of image for getting its properties. After searching over the internet I found a very intersting class on Java i.e ImageIO and BufferedImage class. These classes are good enough to manipulate images. there are many other claases also. But what I need i got it from these. I found many developers searching over internet for getting properties of image and there is no good small example for that. Thats why I thought why not creating a simple code to manipulate the image and help the developers. Here is the small code that helps to get the height and width of the image. Enjoy it!! import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class GetImage { public static void main(String[] args) { try { File f = new File("E:/Vinod/Docs/Pics/krishna_01.jpg"); BufferedImage image = ImageIO.read(f); int height = image.getHeight(); in...

First Time Deployment of war file to IBM Cloud

In this post, I will put a very basic deployment of Java war file on the IBM Cloud . Previously, IBM Cloud was known as IBM Bluemix . But in this post, I will prefer using the IBM Cloud and not the bluemix. I was facing problem while deploying the war file on the IBM Cloud for the first time. I am not a DevOps person who knows about the servers as well. But my job was to deploy the application on the IBM Cloud I need to do it. So, I tried googling around but have no idea about how to do it. There are some examples are given but they are not so basic to be understood by a developer who wants to deploy the application in Cloud. But I manage to do it. And I will explain all the steps that are needed to deploy the application to the Cloud. The application I am going to deploy is the Spring Boot application. Requirements 1. IBM Cloud account. 2. IBM Bluemix CLI 3. war file (spring-boot application) IBM Cloud  Setup After signing for IBM Cloud, go to the IBM Cloud Dashboard...

My JUnit Write Ups

I haven't written to my blog for a long time. There are times when you are busy and didn't get the time to work on the blogs. I am writing for Java Code Geeks for some time and busy with one of the projects that I was building for a client. Most of the blogs that I am writing was on JUnit. If you like this blogs do follow me on the Java Code Geeks at my author page . Also, if possible do upvote my links on DZone . I will be regularly writing on JUnit there. Hope you like those.