Skip to content

Commit 342198c

Browse files
committed
Fix spring-session-sample-boot-redis-json
1 parent c151a97 commit 342198c

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed
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.web;
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 user 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("httpSession")
40+
HttpSession httpSession(HttpSession httpSession) {
41+
return httpSession;
42+
}
43+
44+
}

spring-session-samples/spring-session-sample-boot-redis-json/src/main/resources/templates/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
</tr>
2424
</thead>
2525
<tbody>
26-
<tr th:each="name : ${T(java.util.Collections).list(#httpSession.getAttributeNames())}">
26+
<tr th:each="name : ${T(org.springframework.util.CollectionUtils).toIterator(httpSession?.getAttributeNames())}">
2727
<td th:text="${name}"></td>
28-
<td th:text="${#httpSession.getAttribute(name)}"></td>
28+
<td th:text="${httpSession.getAttribute(name)}"></td>
2929
</tr>
3030
</tbody>
3131
</table>

spring-session-samples/spring-session-sample-boot-redis-json/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>

0 commit comments

Comments
 (0)