Skip to content

Commit 8732c96

Browse files
author
Pankaj Kumar
committed
Azure AD SSO Example Java Web Application
1 parent cd1191c commit 8732c96

File tree

11 files changed

+597
-0
lines changed

11 files changed

+597
-0
lines changed

AzureAD-SSO-Example/pom.xml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.microsoft.azure</groupId>
5+
<artifactId>adal4jsample</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>adal4jsample</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<spring.version>3.0.5.RELEASE</spring.version>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.microsoft.azure</groupId>
17+
<artifactId>adal4j</artifactId>
18+
<version>1.1.1</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.nimbusds</groupId>
22+
<artifactId>oauth2-oidc-sdk</artifactId>
23+
<version>4.5</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.json</groupId>
27+
<artifactId>json</artifactId>
28+
<version>20090211</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>javax.servlet</groupId>
32+
<artifactId>javax.servlet-api</artifactId>
33+
<version>3.0.1</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-log4j12</artifactId>
39+
<version>1.7.5</version>
40+
</dependency>
41+
<!-- Spring 3 dependencies -->
42+
<dependency>
43+
<groupId>org.springframework</groupId>
44+
<artifactId>spring-core</artifactId>
45+
<version>${spring.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework</groupId>
49+
<artifactId>spring-web</artifactId>
50+
<version>${spring.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework</groupId>
54+
<artifactId>spring-webmvc</artifactId>
55+
<version>${spring.version}</version>
56+
</dependency>
57+
58+
</dependencies>
59+
60+
<build>
61+
<finalName>sample-for-adal4j</finalName>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<configuration>
67+
<source>1.7</source>
68+
<target>1.7</target>
69+
<encoding>UTF-8</encoding>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-war-plugin</artifactId>
75+
<version>2.4</version>
76+
<configuration>
77+
<warName>${project.artifactId}</warName>
78+
<source>${project.basedir}\src</source>
79+
<target>${maven.compiler.target}</target>
80+
<encoding>utf-8</encoding>
81+
</configuration>
82+
</plugin>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-dependency-plugin</artifactId>
86+
<executions>
87+
<execution>
88+
<id>install</id>
89+
<phase>install</phase>
90+
<goals>
91+
<goal>sources</goal>
92+
</goals>
93+
</execution>
94+
</executions>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-resources-plugin</artifactId>
99+
<version>2.5</version>
100+
<configuration>
101+
<encoding>UTF-8</encoding>
102+
</configuration>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
107+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.azilen.adal;
2+
3+
import java.text.ParseException;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpSession;
7+
8+
import org.apache.commons.logging.Log;
9+
import org.apache.commons.logging.LogFactory;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.ModelMap;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
15+
import com.microsoft.aad.adal4j.AuthenticationResult;
16+
import com.nimbusds.jwt.JWT;
17+
import com.nimbusds.jwt.JWTParser;
18+
19+
@Controller
20+
@RequestMapping("/secure/aad")
21+
public class AadController {
22+
23+
Log log = LogFactory.getLog(AadController.class);
24+
25+
/**
26+
* getScurePage: Will check for JWT token details and returns aad.jsp view
27+
* @param model
28+
* @param httpRequest
29+
* @return
30+
*/
31+
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
32+
public String getScurePage(ModelMap model, HttpServletRequest httpRequest) {
33+
HttpSession session = httpRequest.getSession();
34+
log.debug("session: " + session);
35+
AuthenticationResult result = (AuthenticationResult) session.getAttribute(CommonUtil.PRINCIPAL_SESSION_NAME);
36+
if (result == null) {
37+
model.addAttribute("error", new Exception("AuthenticationResult not found in session."));
38+
return "/error";
39+
} else {
40+
try {
41+
log.debug("JWT token details:-");
42+
JWT jwt = JWTParser.parse(result.getIdToken());
43+
for (String key : jwt.getJWTClaimsSet().getAllClaims().keySet()) {
44+
log.info(key + ":" + jwt.getJWTClaimsSet().getAllClaims().get(key));
45+
}
46+
model.addAttribute("user", jwt.getJWTClaimsSet().getStringClaim("unique_name"));
47+
} catch (ParseException e) {
48+
log.error("Exception:", e);
49+
}
50+
51+
}
52+
return "/secure/aad";
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)