Git Product home page Git Product logo

ms-identity-java-webapp's Introduction

page_type languages products description urlFragment
sample
java
powershell
html
azure-active-directory
This sample demonstrates a Java web application calling a Microsoft Graph that is secured using Azure Active Directory.
ms-identity-java-webapp

A Java Web application that signs in users with the Microsoft identity platform and calls Microsoft Graph

About this sample

This sample is also available as a quickstart for the Microsoft identity platform: Quickstart: Add sign-in with Microsoft to a Java web app

Overview

This sample demonstrates a Java web application calling a Microsoft Graph that is secured using Azure Active Directory.

  1. The Java web application uses the Microsoft Authentication Library for Java (MSAL4J) to obtain an:

    • Id Token from Azure Active Directory (Azure AD) to sign in an user

    • Access token that is used as a bearer token when calling the Microsoft Graph to get basic information of the signed-in user.

      Topology

For a java web app sample using Spring Security framework take a look at Spring Security webapp sample.

For a java web app sample using MSAL with Azure AD B2C take a look at MSAL B2C webapp sample.

Scenario

This sample shows how to build a Java web app that uses OpenId Connect to sign in/ sign out an user and to get access to the Microsoft Graph using MSAL4J. For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

How to run this sample

To run this sample, you'll need:

  • Working installation of Java and Maven
  • An Internet connection
  • An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
  • A user account in your Azure AD tenant.

Step 1: Download Java (8 and above) for your platform

To successfully use this sample, you need a working installation of Java and Maven.

Step 2: Clone or download this repository

From your shell or command line:

  • git clone https://github.com/Azure-Samples/ms-identity-java-webapp.git

Step 3: Register the sample with your Azure Active Directory tenant

To register these projects, you can:

  • either follow the steps in the paragraphs below
  • or use PowerShell scripts that:
    • automatically create for you the Azure AD applications and related objects (passwords, permissions, dependencies)

If you want to use this automation, read the instructions in App Creation Scripts. Please note that the configuration of your code (Step 4) still needs to be done manually.

First step: choose the Azure AD tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Azure portal.
  2. On the top bar, click on your account, and then on Switch Directory.
  3. Once the Directory + subscription pane opens, choose the Active Directory tenant where you wish to register your application, from the Favorites or All Directories list.
  4. Click on All services in the portal menu, and choose Azure Active Directory.

In the next steps, you might need the tenant name (or directory name) or the tenant ID (or directory ID). These are presented in the Properties of the Azure Active Directory window respectively as Name and Directory ID

Register the app (java-webapp)

  1. In the Azure Active Directory pane, click on App registrations and choose New registration.

  2. Enter a friendly name for the application, for example 'java-webapp', select Accounts in any organizational directory and personal Microsoft Accounts (e.g. Skype, Xbox, Outlook.com).

  3. Click Register to register the application.

  4. On the application Overview page:

    • copy Application (client) ID
    • copy Directory (tenant) ID
    • You'll need both of these values later to configure the project, so copy them in a safe place.
  5. On the application Authentication page, under Redirect URIs, select Web. You will need to enter two different redirect URIs: one for the sign-in page, and one for the graph page. For both, you should use the same host and port number, then followed by "/msal4jsample/secure/aad" for the sign-in page and "msal4jsample/graph/me" for the user info page. By default, the sample uses:

    • http://localhost:8080/msal4jsample/secure/aad.
    • http://localhost:8080/msal4jsample/graph/me

    Click on save.

  6. On the application menu, choose Certificates & Secrets and click on New client secret in the Client Secrets section:

    • Type a key description (for instance app secret),
    • Select a key duration of either In 1 year, In 2 years, or Never Expires.
    • The key value will display when you select Add. Copy the its value in a safe place.
    • You'll need this key later to configure the project. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Azure portal.

Step 4: Configure the sample to use your Azure AD tenant

Open application.properties in the src/main/resources folder. Fill in with your tenant and app registration information noted in registration step. Replace Enter_the_Tenant_Info_Here with the Tenant Id, Enter_the_Application_Id_here with the Application Id and Enter_the_Client_Secret_Here with the key value noted.

If you did not use the default redirect URIs, then you'll have to update aad.redirectUriSignin and aad.redirectUriGraph as well with the registered redirect URIs.

You can use any host and port number, but the path must stay the same (/msal4jsample/secure/aad and /msal4jsample/graph/me) as these are mapped to the controllers that will process the requests.

In order to use https with localhost fill in server.ssl.key properties.
Use keytool utility (included in JRE) if you want to generate self-signed certificate.

Example:  
keytool -genkeypair -alias testCert -keyalg RSA -storetype PKCS12 -keystore keystore.p12 -storepass password

server.ssl.key-store-type=PKCS12  
server.ssl.key-store=classpath:keystore.p12  
server.ssl.key-store-password=password  
server.ssl.key-alias=testCert

Step 5: Run the application

To run the project, you can either:

Run it directly from your IDE by using the embedded spring boot server or package it to a WAR file using maven and deploy it a J2EE container solution such as Apache Tomcat.

Running from IDE

If you running you web application from an IDE, click on run, then navigate to the home page of the project. For this sample, the standard home page URL is http://localhost:8080

Packaging and deploying to container

If you would like to deploy the web sample to Tomcat, you will need to make a couple of changes to the source code.

  1. Open ms-identity-java-webapp/pom.xml

    • Under <name>msal-web-sample</name> add <packaging>war</packaging>

    • Add dependency:

      <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
      </dependency>
  2. Open ms-identity-java-webapp/src/main/java/com.microsoft.azure.msalwebsample/MsalWebSampleApplication

    • Delete all source code and replace with the following:
     package com.microsoft.azure.msalwebsample;
    
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
     import org.springframework.boot.builder.SpringApplicationBuilder;
     import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
     @SpringBootApplication
     public class MsalWebSampleApplication extends SpringBootServletInitializer {
    
      public static void main(String[] args) {
       SpringApplication.run(MsalWebSampleApplication.class, args);
      }
    
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
       return builder.sources(MsalWebSampleApplication.class);
      }
     }
  3. Open a command prompt, go to the root folder of the project, and run mvn package

    • This will generate a msal-web-sample-0.1.0.war file in your /targets directory.
    • Rename this file to ROOT.war
    • Deploy this war file using Tomcat or any other J2EE container solution.
      • To deploy on Tomcat container, copy the .war file to the webapps folder under your Tomcat installation and then start the Tomcat server.

This WAR will automatically be hosted at http://<yourserverhost>:<yourserverport>/ - Tomcats default port is 8080. This can be changed by - Going to tomcat/conf/server.xml - Search "Connector Port" - Replace "8080" with your desired port number

Example: http://localhost:8080/msal4jsample

You're done

Click on "Login" to start the process of logging in. Once logged in, you'll see the account information for the user that is logged in. You'll then have the option to "Sign out" or to "Show User Info", which will display the basic information of the signed-in user.

Community Help and Support

Use Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [msal Java].

If you find a bug in the sample, please raise the issue on GitHub Issues.

To provide a recommendation, visit the following User Voice page.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

More information

For more information, see MSAL4J conceptual documentation

For more information about web apps scenarios on the Microsoft identity platform see Scenario: Web app that signs in users and Scenario: Web app that calls web APIs

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

ms-identity-java-webapp's People

Contributors

archieag avatar jmprieur avatar microsoftopensource avatar msftgits avatar navyasric avatar ramya25 avatar sangonzal avatar somkape avatar v-rajagt-zz avatar watahani avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.