Skip to content

Commit 3794ea7

Browse files
authored
Spring web app sample (#102)
1 parent 9ccc4b9 commit 3794ea7

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.8.RELEASE</version>
9+
<relativePath/>
10+
</parent>
11+
<groupId>com.microsoft.azure</groupId>
12+
<artifactId>spring-security-web-app</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-security-web-app</name>
15+
<description>Web app with Spring Security</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.thymeleaf.extras</groupId>
35+
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.thymeleaf</groupId>
40+
<artifactId>thymeleaf-spring5</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-security</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.security.oauth.boot</groupId>
50+
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
51+
<version>2.1.8.RELEASE</version>
52+
</dependency>
53+
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.azure.springsecuritywebapp;
5+
6+
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.core.annotation.Order;
9+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
12+
13+
@Configuration
14+
@EnableOAuth2Sso
15+
@Order(value = 0)
16+
public class AppConfiguration extends WebSecurityConfigurerAdapter {
17+
18+
@Override
19+
public void configure(HttpSecurity http) throws Exception {
20+
21+
http.antMatcher("/**")
22+
.authorizeRequests()
23+
.antMatchers("/", "/login**", "/error**")
24+
.permitAll()
25+
.anyRequest()
26+
.authenticated()
27+
.and()
28+
.logout()
29+
.deleteCookies()
30+
.invalidateHttpSession(true)
31+
.logoutSuccessUrl("/");
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.azure.springsecuritywebapp;
5+
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.servlet.ModelAndView;
9+
10+
@Controller
11+
public class SecurePageController {
12+
13+
@RequestMapping("/secure_page")
14+
public ModelAndView securePage(){
15+
ModelAndView mav = new ModelAndView("secure_page");
16+
17+
return mav;
18+
}
19+
20+
@RequestMapping("/")
21+
public ModelAndView indexPage() {
22+
ModelAndView mav = new ModelAndView("index");
23+
24+
return mav;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.azure.springsecuritywebapp;
5+
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
9+
@SpringBootApplication
10+
public class SpringSecurityWebAppApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(SpringSecurityWebAppApplication.class, args);
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
logging.level.org.springframework.*=DEBUG
2+
3+
ssoServiceUrl=https://login.microsoftonline.com/common
4+
5+
security.oauth2.client.client-id=
6+
security.oauth2.client.client-secret=
7+
security.oauth2.client.scope=openid profile
8+
security.oauth2.client.authentication-scheme=header
9+
security.oauth2.client.client-authentication-scheme=form
10+
11+
security.oauth2.issuer=https://login.microsoftonline.com/<TenantId>/v2.0
12+
13+
security.oauth2.client.access-token-uri=${ssoServiceUrl}/oauth2/v2.0/token
14+
security.oauth2.client.user-authorization-uri=${ssoServiceUrl}/oauth2/v2.0/authorize
15+
16+
security.oauth2.resource.user-info-uri=https://graph.microsoft.com/oidc/userinfo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<meta charset="UTF-8">
4+
<title>HomePage</title>
5+
</head>
6+
<body>
7+
<h3>Home Page</h3>
8+
9+
<form action="/secure_page">
10+
<input type="submit" value="Login">
11+
</form>
12+
13+
</body>
14+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
<!DOCTYPE html>
3+
4+
<html xmlns:th="https://www.thymeleaf.org"
5+
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
6+
7+
<head>
8+
<meta charset="UTF-8">
9+
<title>Main</title>
10+
</head>
11+
<body>
12+
13+
<div sec:authorize="isAuthenticated()">Authenticated as <span sec:authentication="name"></span></div>
14+
15+
16+
<form method="post" action="/logout">
17+
<input
18+
type="hidden"
19+
th:name="${_csrf.parameterName}"
20+
th:value="${_csrf.token}" />
21+
22+
<input type="submit" value="Logout">
23+
</form>
24+
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)