Thursday, November 05, 2020

Integration of Spring MVC with Azure SSO

Generally you will find the integration of Azure with Spring Boot. In this example we will show you the integration of Spring MVC with Azure.
In depth we are going to use Spring Boot as it is a wrapper over Spring MVC and provide many additional feature for connecting Azure.

We had taken a simple mvc example inving index.jsp which will be displayed through our controller HelloWorldController which is mapped with url “/”.

We also have WebConfig class which extends WebMvcConfigurerAdapter indicating it is an spring MVC example.

We have RootConfig which states the basePackages and MyWebAppInitializer class that extends AbstractAnnotationConfigDispatcherServletInitializer used by the spring MVC dispatcher.

When you run the appication you will be able to see the screen as shown belwo

Now lets move to integrate our Azure SSO with this Spring MVC. We are going to use Spring BOOT for the same.

For this we are going to add follwoing items in our pom.xml

     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-security</artifactId>     </dependency>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>     </dependency>     <dependency>         <groupId>com.microsoft.azure</groupId>         <artifactId>azure-active-directory-spring-boot-starter</artifactId>     </dependency>     <dependency>         <groupId>org.springframework.security</groupId>         <artifactId>spring-security-oauth2-client</artifactId>         <version>5.3.4.RELEASE</version>     </dependency>     <dependency>         <groupId>org.springframework.security</groupId>         <artifactId>spring-security-oauth2-jose</artifactId>         <version>5.3.4.RELEASE</version>     </dependency> <dependencyManagement>     <dependencies>         <dependency>             <groupId>com.microsoft.azure</groupId>             <artifactId>azure-spring-boot-bom</artifactId>             <version>${azure.version}</version>             <type>pom</type>             <scope>import</scope>         </dependency>     </dependencies> </dependencyManagement> <plugin>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-maven-plugin</artifactId>         </plugin> <!-- By siddhu end] -->

Then we are going to add follwing class in the respective packages.

SecurityConfigInitializer class inside package com.siddhu.spring.config;

This class extends AbstractSecurityWebApplicationInitializer and it will help the spring to identify the class that we are going to use for Spring Security i.e. WebSecurityConfig

Out this class WebSecurityConfig will extends WebSecurityConfigurerAdapter and will be used as a main class to connect the Azure SSO.

We will also create a simple SiddhuSpringSecurityAzureMVCSimpleApplication that will be our @SpringBootApplication

Please make sure to add application.properties inside our src/main/resource folder

This will contains the items that are used by the spring Security to connect Azure.

Specifies your Active Directory ID:

azure.activedirectory.tenant-id=

Specifies your App Registration’s Application ID:

spring.security.oauth2.client.registration.azure.client-id=

Specifies your App Registration’s secret key:

spring.security.oauth2.client.registration.azure.client-secret=

Specifies the list of Active Directory groups to use for authorization:

azure.activedirectory.user-group.allowed-groups=

server.port=8181

Thanks it and then run your application as Spring Boot or deploy you war/jar in tomcat and hit the url you will be asked to enter the window Azure user login as shown belwo.

Download:- You can down the example from
https://github.com/shdhumale/spring-mvc-boot-helloworld-example.git

Above example will work when you execute the code using Spring boot i.e. Right click and run as spring boot application. But when you create a war file and try to deploy the war in tomcat and run the application you war will not be deployed the reason behind is it will give you error of intantiating OAuth2UserService and further it will give error saying multiple context is defined.

This all error can be removed by doing following changes in the code
1- In pom.xml add following entries

1
2
3
4
5
6
7
8
9
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

2- Modify you SiddhuSpringSecurityAzureMVCSimpleApplication @SpringBootApplication with below code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootApplication
//public class SiddhuSpringSecurityAzureMVCSimpleApplication extends SpringBootServletInitializer{
public class SiddhuSpringSecurityAzureMVCSimpleApplication  extends SpringBootServletInitializer {
 
     @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(SiddhuSpringSecurityAzureMVCSimpleApplication.class);
       }
      
    public static void main(String[] args) {
        SpringApplication.run(SiddhuSpringSecurityAzureMVCSimpleApplication.class, args);      
        /*SpringApplication app = new SpringApplication(SiddhuSpringSecurityAzureSimpleApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "8181"));
        app.run(args);*/
    }
 
}

3- Remove two class MyWebAppInitializer.java and SecurityConfigInitializer.java

Now create a war and deploy it on tomcat.

Download:- You can download the code from belwo given url

https://github.com/shdhumale/spring-mvc-boot-helloworld-example-tomcat.git