Skip to content

Commit 81b1cc1

Browse files
rwinchBuzzardo
authored andcommitted
Migrate to Spring Boot 3.0.0
1 parent 1a5a197 commit 81b1cc1

File tree

11 files changed

+48
-49
lines changed

11 files changed

+48
-49
lines changed

README.adoc

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,13 @@ the classpath, Spring Boot {SpringBootSecurity}[automatically secures all HTTP e
106106
with "`basic`" authentication. However, you can further customize the security settings.
107107
The first thing you need to do is add Spring Security to the classpath.
108108

109-
With Gradle, you need to add two lines (one for the application and one for testing) in
109+
With Gradle, you need to add three lines (one for the application, one for Thymeleaf & Spring Security integration, and one for testing) in
110110
the `dependencies` closure in `build.gradle`, as the following listing shows:
111111

112112
====
113113
[source,java]
114114
----
115-
implementation 'org.springframework.boot:spring-boot-starter-security'
116-
implementation 'org.springframework.security:spring-security-test'
115+
include::complete/build.gradle[tags=security-dependencies,indent=0]
117116
----
118117
====
119118

@@ -122,7 +121,7 @@ The following listing shows the finished `build.gradle` file:
122121
====
123122
[source,text]
124123
----
125-
include::complete/build.gradle[]
124+
include::complete/build.gradle[tags=**]
126125
----
127126
====
128127

@@ -132,15 +131,7 @@ testing) to the `<dependencies>` element in `pom.xml`, as the following listing
132131
====
133132
[source,zml]
134133
----
135-
<dependency>
136-
<groupId>org.springframework.boot</groupId>
137-
<artifactId>spring-boot-starter-security</artifactId>
138-
</dependency>
139-
<dependency>
140-
<groupId>org.springframework.security</groupId>
141-
<artifactId>spring-security-test</artifactId>
142-
<scope>test</scope>
143-
</dependency>
134+
include::complete/pom.xml[tags=security-dependencies,indent=0]
144135
----
145136
====
146137

@@ -149,7 +140,7 @@ The following listing shows the finished `pom.xml` file:
149140
====
150141
[source,text]
151142
----
152-
include::complete/pom.xml[]
143+
include::complete/pom.xml[tags=**]
153144
----
154145
====
155146

@@ -210,10 +201,12 @@ include::complete/src/main/resources/templates/hello.html[]
210201
----
211202
====
212203

213-
We display the username by using Spring Security's integration with
214-
`HttpServletRequest#getRemoteUser()`. The "`Sign Out`" form submits a POST to `/logout`.
204+
We display the username by using Thymeleaf's integration with Spring Security. The "`Sign Out`" form submits a POST to `/logout`.
215205
Upon successfully logging out, it redirects the user to `/login?logout`.
216206

207+
NOTE: Thymeleaf 3.1 no longer provides access to `HttpServletRequest` so `HttpServletRequest#getRemoteUser()` cannot be used to access the currently authenticated user.
208+
209+
217210
[[run_the_app]]
218211
== Run the Application
219212

complete/build.gradle

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
plugins {
2-
id 'org.springframework.boot' version '2.7.1'
3-
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
42
id 'java'
3+
id 'org.springframework.boot' version '3.0.0'
4+
id 'io.spring.dependency-management' version '1.1.0'
55
}
66

77
group = 'com.example'
88
version = '0.0.1-SNAPSHOT'
9-
sourceCompatibility = '1.8'
9+
sourceCompatibility = '17'
1010

1111
repositories {
1212
mavenCentral()
1313
}
1414

1515
dependencies {
16-
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
1716
implementation 'org.springframework.boot:spring-boot-starter-web'
17+
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
18+
// tag::security-dependencies[]
1819
implementation 'org.springframework.boot:spring-boot-starter-security'
20+
// Temporary explicit version to fix Thymeleaf bug
21+
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.1.RELEASE'
1922
implementation 'org.springframework.security:spring-security-test'
20-
testImplementation('org.springframework.boot:spring-boot-starter-test')
23+
// end::security-dependencies[]
24+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2125
}
2226

2327
test {

complete/pom.xml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.7.1</version>
8+
<version>3.0.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
@@ -15,7 +15,7 @@
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
18-
<java.version>1.8</java.version>
18+
<java.version>17</java.version>
1919
</properties>
2020

2121
<dependencies>
@@ -27,15 +27,23 @@
2727
<groupId>org.springframework.boot</groupId>
2828
<artifactId>spring-boot-starter-web</artifactId>
2929
</dependency>
30+
<!-- tag::security-dependencies[] -->
3031
<dependency>
3132
<groupId>org.springframework.boot</groupId>
3233
<artifactId>spring-boot-starter-security</artifactId>
3334
</dependency>
35+
<dependency>
36+
<groupId>org.thymeleaf.extras</groupId>
37+
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
38+
<!-- Temporary explicit version to fix Thymeleaf bug -->
39+
<version>3.1.1.RELEASE</version>
40+
</dependency>
3441
<dependency>
3542
<groupId>org.springframework.security</groupId>
3643
<artifactId>spring-security-test</artifactId>
3744
<scope>test</scope>
3845
</dependency>
46+
<!-- end::security-dependencies[] -->
3947

4048
<dependency>
4149
<groupId>org.springframework.boot</groupId>
@@ -44,13 +52,4 @@
4452
</dependency>
4553
</dependencies>
4654

47-
<build>
48-
<plugins>
49-
<plugin>
50-
<groupId>org.springframework.boot</groupId>
51-
<artifactId>spring-boot-maven-plugin</artifactId>
52-
</plugin>
53-
</plugins>
54-
</build>
55-
5655
</project>

complete/src/main/java/com/example/securingweb/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class WebSecurityConfig {
1818
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1919
http
2020
.authorizeHttpRequests((requests) -> requests
21-
.antMatchers("/", "/home").permitAll()
21+
.requestMatchers("/", "/home").permitAll()
2222
.anyRequest().authenticated()
2323
)
2424
.formLogin((form) -> form

complete/src/main/resources/templates/hello.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<!DOCTYPE html>
22
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
3-
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
3+
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
44
<head>
55
<title>Hello World!</title>
66
</head>
77
<body>
8-
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
8+
<h1 th:inline="text">Hello <span th:remove="tag" sec:authentication="name">thymeleaf</span>!</h1>
99
<form th:action="@{/logout}" method="post">
1010
<input type="submit" value="Sign Out"/>
1111
</form>

complete/src/main/resources/templates/login.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!DOCTYPE html>
2-
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
3-
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
43
<head>
54
<title>Spring Security Example </title>
65
</head>

complete/src/test/java/com/example/securingweb/SecuringWebApplicationTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import org.springframework.security.test.context.support.WithMockUser;
99
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.FormLoginRequestBuilder;
1010
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.MvcResult;
1112

13+
import static org.assertj.core.api.Assertions.assertThat;
1214
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
1315
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
1416
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
@@ -58,7 +60,10 @@ public void accessSecuredResourceUnauthenticatedThenRedirectsToLogin() throws Ex
5860
@Test
5961
@WithMockUser
6062
public void accessSecuredResourceAuthenticatedThenOk() throws Exception {
61-
mockMvc.perform(get("/hello"))
62-
.andExpect(status().isOk());
63+
MvcResult mvcResult = mockMvc.perform(get("/hello"))
64+
.andExpect(status().isOk())
65+
.andReturn();
66+
67+
assertThat(mvcResult.getResponse().getContentAsString()).contains("Hello user!");
6368
}
6469
}

initial/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
plugins {
2-
id 'org.springframework.boot' version '2.7.1'
3-
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
42
id 'java'
3+
id 'org.springframework.boot' version '3.0.0'
4+
id 'io.spring.dependency-management' version '1.1.0'
55
}
66

77
group = 'com.example'
88
version = '0.0.1-SNAPSHOT'
9-
sourceCompatibility = '1.8'
9+
sourceCompatibility = '17'
1010

1111
repositories {
1212
mavenCentral()
@@ -15,7 +15,7 @@ repositories {
1515
dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
1717
implementation 'org.springframework.boot:spring-boot-starter-web'
18-
testImplementation('org.springframework.boot:spring-boot-starter-test')
18+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
1919
}
2020

2121
test {

initial/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.7.1</version>
8+
<version>3.0.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
@@ -15,7 +15,7 @@
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
18-
<java.version>1.8</java.version>
18+
<java.version>17</java.version>
1919
</properties>
2020

2121
<dependencies>

initial/src/main/resources/templates/hello.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!DOCTYPE html>
2-
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
3-
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
43
<head>
54
<title>Hello World!</title>
65
</head>

0 commit comments

Comments
 (0)