Skip to content

Commit 0dcdf5f

Browse files
committed
Fix Thymeleaf Samples
Thymeleaf removed support for accessing the HttpServletRequest and HttpSession automatically, so we need to add any properties we want to access as ModelAttributes Closes gh-2076
2 parents 2a6a9cf + a0bf6a0 commit 0dcdf5f

File tree

22 files changed

+382
-92
lines changed

22 files changed

+382
-92
lines changed

settings.gradle

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,10 @@ include 'spring-session-docs'
1919
include 'spring-session-hazelcast'
2020
include 'spring-session-jdbc'
2121

22-
include 'spring-session-sample-javaconfig-custom-cookie'
23-
project(':spring-session-sample-javaconfig-custom-cookie').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-custom-cookie')
24-
include 'spring-session-sample-javaconfig-jdbc'
25-
project(':spring-session-sample-javaconfig-jdbc').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-jdbc')
26-
include 'spring-session-sample-javaconfig-redis'
27-
project(':spring-session-sample-javaconfig-redis').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-redis')
28-
include 'spring-session-sample-javaconfig-rest'
29-
project(':spring-session-sample-javaconfig-rest').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-rest')
30-
include 'spring-session-sample-javaconfig-hazelcast'
31-
project(':spring-session-sample-javaconfig-hazelcast').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-hazelcast')
32-
include 'spring-session-sample-javaconfig-security'
33-
project(':spring-session-sample-javaconfig-security').projectDir = file('spring-session-samples/spring-session-sample-javaconfig-security')
34-
include 'spring-session-sample-misc-hazelcast'
35-
project(':spring-session-sample-misc-hazelcast').projectDir = file('spring-session-samples/spring-session-sample-misc-hazelcast')
36-
include 'spring-session-sample-xml-redis'
37-
project(':spring-session-sample-xml-redis').projectDir = file('spring-session-samples/spring-session-sample-xml-redis')
38-
include 'spring-session-sample-xml-jdbc'
39-
project(':spring-session-sample-xml-jdbc').projectDir = file('spring-session-samples/spring-session-sample-xml-jdbc')
40-
include 'spring-session-sample-boot-webflux'
41-
project(':spring-session-sample-boot-webflux').projectDir = file('spring-session-samples/spring-session-sample-boot-webflux')
42-
include 'spring-session-sample-boot-webflux-custom-cookie'
43-
project(':spring-session-sample-boot-webflux-custom-cookie').projectDir = file('spring-session-samples/spring-session-sample-boot-webflux-custom-cookie')
44-
include 'spring-session-sample-boot-mongodb-reactive'
45-
project(':spring-session-sample-boot-mongodb-reactive').projectDir = file('spring-session-samples/spring-session-sample-boot-mongodb-reactive')
46-
47-
//file('spring-session-samples').eachDirMatch(~/spring-session-sample-.*/) { dir ->
48-
// include dir.name
49-
// project(":$dir.name").projectDir = dir
50-
//}
22+
file('spring-session-samples').eachDirMatch(~/spring-session-sample-.*/) { dir ->
23+
include dir.name
24+
project(":$dir.name").projectDir = dir
25+
}
5126

5227
rootProject.children.each { project ->
5328
project.buildFileName = "${project.name}.gradle"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2014-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.mvc;
18+
19+
import java.security.Principal;
20+
21+
import jakarta.servlet.http.HttpSession;
22+
23+
import org.springframework.web.bind.annotation.ControllerAdvice;
24+
import org.springframework.web.bind.annotation.ModelAttribute;
25+
26+
/**
27+
* {@link ControllerAdvice} to expose security related attributes.
28+
*
29+
* @author Rob Winch
30+
*/
31+
@ControllerAdvice
32+
public class SecurityControllerAdvise {
33+
34+
@ModelAttribute("currentUserName")
35+
String currentUser(Principal principal) {
36+
return (principal != null) ? principal.getName() : null;
37+
}
38+
39+
@ModelAttribute("httpSessionId")
40+
String sessionId(HttpSession httpSession) {
41+
return httpSession.getId();
42+
}
43+
44+
}

spring-session-samples/spring-session-sample-boot-findbyusername/src/main/resources/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h1>Secured Page</h1>
88
<p>This page is secured using Spring Boot, Spring Session, and Spring Security.</p>
99

10-
<p>Your current session id is <span id="session-id" th:text="${#httpSession.id}"></span></p>
10+
<p>Your current session id is <span id="session-id" th:text="${httpSessionId}"></span></p>
1111

1212
<table class="table table-stripped">
1313
<tr>
@@ -26,7 +26,7 @@ <h1>Secured Page</h1>
2626
<td th:text="${details?.accessType}"></td>
2727
<td>
2828
<form th:action="@{'/sessions/' + ${sessionElement.id}}" th:method="post">
29-
<input th:id="'terminate-' + ${sessionElement.id}" type="submit" value="Terminate" th:disabled="${sessionElement.id == #httpSession.id}"/>
29+
<input th:id="'terminate-' + ${sessionElement.id}" type="submit" value="Terminate" th:disabled="${sessionElement.id == httpSessionId}"/>
3030
</form>
3131
</td>
3232
</tr>

spring-session-samples/spring-session-sample-boot-findbyusername/src/main/resources/templates/layout.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@
8181
<div class="container">
8282
<a class="brand" th:href="@{/}"><img th:src="@{/images/logo.png}" alt="Spring Security Sample"/></a>
8383

84-
<div class="nav-collapse collapse"
85-
th:with="currentUser=${#httpServletRequest.userPrincipal?.principal}">
86-
<div th:if="${currentUser != null}">
84+
<div class="nav-collapse collapse">
85+
<div th:if="${currentUserName != null}">
8786
<form class="navbar-form pull-right" th:action="@{/logout}" method="post">
8887
<input type="submit" value="Log out" />
8988
</form>
90-
<p id="un" class="navbar-text pull-right" th:text="${currentUser.username}">
89+
<p id="un" class="navbar-text pull-right" th:text="${currentUserName}">
9190
sample_user
9291
</p>
9392
</div>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2017 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,22 @@
1616

1717
package sample.config;
1818

19-
import org.springframework.context.annotation.Configuration;
20-
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
21-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19+
import java.security.Principal;
2220

23-
@Configuration
24-
public class WebMvcConfig implements WebMvcConfigurer {
21+
import org.springframework.web.bind.annotation.ControllerAdvice;
22+
import org.springframework.web.bind.annotation.ModelAttribute;
2523

26-
@Override
27-
public void addViewControllers(ViewControllerRegistry registry) {
28-
registry.addViewController("/").setViewName("index");
24+
/**
25+
* {@link ControllerAdvice} to expose user related attributes.
26+
*
27+
* @author Rob Winch
28+
*/
29+
@ControllerAdvice
30+
public class UserControllerAdvise {
31+
32+
@ModelAttribute("currentUserName")
33+
String currentUser(Principal principal) {
34+
return (principal != null) ? principal.getName() : null;
2935
}
3036

3137
}

spring-session-samples/spring-session-sample-boot-hazelcast/src/main/resources/templates/layout.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@
8181
<div class="container">
8282
<a class="brand" th:href="@{/}"><img th:src="@{/images/logo.png}" alt="Spring Security Sample"/></a>
8383

84-
<div class="nav-collapse collapse"
85-
th:with="currentUser=${#httpServletRequest.userPrincipal?.principal}">
86-
<div th:if="${currentUser != null}">
84+
<div class="nav-collapse collapse">
85+
<div th:if="${currentUserName != null}">
8786
<form class="navbar-form pull-right" th:action="@{/logout}" method="post">
8887
<input type="submit" value="Log out" />
8988
</form>
90-
<p id="un" class="navbar-text pull-right" th:text="${currentUser.username}">
89+
<p id="un" class="navbar-text pull-right" th:text="${currentUserName}">
9190
sample_user
9291
</p>
9392
</div>
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.config;
17+
package sample;
1818

19-
import org.springframework.context.annotation.Configuration;
20-
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
21-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19+
import org.springframework.stereotype.Controller;
20+
import org.springframework.web.bind.annotation.RequestMapping;
2221

23-
@Configuration
24-
public class WebMvcConfig implements WebMvcConfigurer {
22+
/**
23+
* Controller for sending the user to the login view.
24+
*
25+
* @author Rob Winch
26+
*
27+
*/
28+
@Controller
29+
public class IndexController {
2530

26-
@Override
27-
public void addViewControllers(ViewControllerRegistry registry) {
28-
registry.addViewController("/").setViewName("index");
31+
@RequestMapping("/")
32+
public String index() {
33+
return "index";
2934
}
3035

3136
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2014-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import java.security.Principal;
20+
21+
import org.springframework.web.bind.annotation.ControllerAdvice;
22+
import org.springframework.web.bind.annotation.ModelAttribute;
23+
24+
/**
25+
* {@link ControllerAdvice} to expose security related attributes.
26+
*
27+
* @author Rob Winch
28+
*/
29+
@ControllerAdvice
30+
public class UserControllerAdvise {
31+
32+
@ModelAttribute("currentUserName")
33+
String currentUser(Principal principal) {
34+
return (principal != null) ? principal.getName() : null;
35+
}
36+
37+
}

spring-session-samples/spring-session-sample-boot-jdbc/src/main/resources/templates/layout.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@
8181
<div class="container">
8282
<a class="brand" th:href="@{/}"><img th:src="@{/images/logo.png}" alt="Spring Security Sample"/></a>
8383

84-
<div class="nav-collapse collapse"
85-
th:with="currentUser=${#httpServletRequest.userPrincipal?.principal}">
86-
<div th:if="${currentUser != null}">
84+
<div class="nav-collapse collapse">
85+
<div th:if="${currentUserName != null}">
8786
<form class="navbar-form pull-right" th:action="@{/logout}" method="post">
8887
<input type="submit" value="Log out" />
8988
</form>
90-
<p id="un" class="navbar-text pull-right" th:text="${currentUser.username}">
89+
<p id="un" class="navbar-text pull-right" th:text="${currentUserName}">
9190
sample_user
9291
</p>
9392
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2014-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.mongodb.examples.mvc;
18+
19+
import org.springframework.web.bind.annotation.ControllerAdvice;
20+
import org.springframework.web.bind.annotation.ModelAttribute;
21+
22+
import java.security.Principal;
23+
24+
/**
25+
* {@link ControllerAdvice} to expose user related attributes.
26+
*
27+
* @author Rob Winch
28+
*/
29+
@ControllerAdvice
30+
public class UserControllerAdvise {
31+
32+
@ModelAttribute("currentUserName")
33+
String currentUser(Principal principal) {
34+
return (principal != null) ? principal.getName() : null;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)