File tree Expand file tree Collapse file tree 3 files changed +49
-6
lines changed
spring-session-samples/spring-session-sample-boot-findbyusername/src/main Expand file tree Collapse file tree 3 files changed +49
-6
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 7
7
< h1 > Secured Page</ h1 >
8
8
< p > This page is secured using Spring Boot, Spring Session, and Spring Security.</ p >
9
9
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 >
11
11
12
12
< table class ="table table-stripped ">
13
13
< tr >
@@ -26,7 +26,7 @@ <h1>Secured Page</h1>
26
26
< td th:text ="${details?.accessType} "> </ td >
27
27
< td >
28
28
< 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 } "/>
30
30
</ form >
31
31
</ td >
32
32
</ tr >
Original file line number Diff line number Diff line change 81
81
< div class ="container ">
82
82
< a class ="brand " th:href ="@{/} "> < img th:src ="@{/images/logo.png} " alt ="Spring Security Sample "/> </ a >
83
83
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} ">
87
86
< form class ="navbar-form pull-right " th:action ="@{/logout} " method ="post ">
88
87
< input type ="submit " value ="Log out " />
89
88
</ 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 } ">
91
90
sample_user
92
91
</ p >
93
92
</ div >
You can’t perform that action at this time.
0 commit comments