Skip to content

Azure AD based authentication micro-service written in spring boot

Notifications You must be signed in to change notification settings

khandelwal-arpit/azure-ad-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authentication Using Azure Active Directory

Overview

This micro-service intends to provide a jump start solution for all those who are looking to do authenticaation using Azure Active Directory.

Technology Stack

Solution

The solution provided here assumes that you have an existing Azure AD with users enrolled in it. The next requirement is to create a client app registered with this active directory. Steps to do the same are as follows:

client app

We make use of the following two important ADAL classes for this solution:

Steps Involved

  • Create context with address of authority, throws exception if url incorrect:
AuthenticationContext context = new AuthenticationContext(authority_url, false, service);
  • Acquire a security token from the authority using a username/password flow:
Future<AuthenticationResult> future = context.acquireToken(graphURL, clientID, email, password, null);

Environment Variables

Please set the following env variables for this micro-service to work properly:

  1. PORT : Port for tomcat server
  2. CLIENT_ID : Client ID obtained from the registered client app
  3. TENANT_ID : Tenant ID regisred with the Azure AD

Running the server locally

To be able to run this Spring Boot micro-service you will need to first build it. To build and package a Spring Boot app into a single executable Jar file with a Maven, use the below command. You will need to run it from the project folder which contains the pom.xml file.

maven package

or you can also use

mvn install

To run the Spring Boot app from a command line in a Terminal window you can you the java -jar command. This is provided your Spring Boot app was packaged as an executable jar file.

java -jar target/azure-ad-auth-1.0.jar

You can also use Maven plugin to run the app. Use the below example to run your Spring Boot app with Maven plugin :

mvn spring-boot:run

If you do not have a mongo instance running and still just want to create the JAR, then please use the following command:

mvn install -DskipTests

This will skip the test cases and won't check the availability of a mongodb instance and allow you to create the JAR.

You can follow any/all the above commands, or simply use the run configuration provided by your favorite IDE and run/debug the app from there for development purposes. Once the server is setup you should be able to access the REST API at the following path:

The input body in the post request should look something like:

{
    "email" : "[email protected]",
    "password" : "Y2hhbmdlaXQ="
}

In case you are wondering about the password not being in plain-text format, it is needed in Base64 encoded format as the input in request body.

If everything goes as planned, you should see the following response from the api:

{
    "status": 200,
    "user-name": "Arpit",
    "user-email": "[email protected]",
    "timestamp": "2020-10-21T16:26:49.734+00:00",
    "family-name": "Khandelwal",
    "tenant-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

Contributors

Arpit Khandelwal

License

This project is licensed under the terms of the MIT license.

About

Azure AD based authentication micro-service written in spring boot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages