Skip to content

Commit 0cb6e0e

Browse files
committed
Fix spring-session-sample-boot-redis-simple
1 parent b4c3cef commit 0cb6e0e

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 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.
@@ -14,18 +14,22 @@
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.GetMapping;
2221

23-
@Configuration
24-
public class WebMvcConfig implements WebMvcConfigurer {
22+
/**
23+
* An index controller.
24+
*
25+
* @author Rob Winch
26+
*/
27+
@Controller
28+
public class IndexController {
2529

26-
@Override
27-
public void addViewControllers(ViewControllerRegistry registry) {
28-
registry.addViewController("/").setViewName("index");
30+
@GetMapping("/")
31+
String index() {
32+
return "index";
2933
}
3034

3135
}
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 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+
}

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

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

89-
<div class="nav-collapse collapse"
90-
th:with="currentUser=${#httpServletRequest.userPrincipal?.principal}">
91-
<div th:if="${currentUser != null}">
89+
<div class="nav-collapse collapse">
90+
<div th:if="${currentUserName != null}">
9291
<form class="navbar-form pull-right" th:action="@{/logout}" method="post">
9392
<input type="submit" value="Log out"/>
9493
</form>
95-
<p id="un" class="navbar-text pull-right" th:text="${currentUser.username}">
94+
<p id="un" class="navbar-text pull-right" th:text="${currentUserName}">
9695
sample_user
9796
</p>
9897
</div>

0 commit comments

Comments
 (0)