File tree Expand file tree Collapse file tree 4 files changed +40
-5
lines changed
spring-session-samples/spring-session-sample-boot-findbyusername/src
integration-test/java/sample Expand file tree Collapse file tree 4 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,8 @@ class FindByUsernameTests {
53
53
54
54
private WebDriver driver ;
55
55
56
+ private WebDriver driver2 ;
57
+
56
58
@ BeforeEach
57
59
void setup () {
58
60
this .driver = MockMvcHtmlUnitDriverBuilder .mockMvcSetup (this .mockMvc ).build ();
@@ -61,6 +63,9 @@ void setup() {
61
63
@ AfterEach
62
64
void tearDown () {
63
65
this .driver .quit ();
66
+ if (this .driver2 != null ) {
67
+ this .driver2 .quit ();
68
+ }
64
69
}
65
70
66
71
@ Test
@@ -79,6 +84,25 @@ void login() {
79
84
home .terminateButtonDisabled ();
80
85
}
81
86
87
+ @ Test
88
+ void terminateOtherSession () throws Exception {
89
+ HomePage forgotToLogout = home (this .driver );
90
+
91
+ this .driver2 = MockMvcHtmlUnitDriverBuilder .mockMvcSetup (this .mockMvc ).build ();
92
+ HomePage terminateFogotSession = home (this .driver2 );
93
+ terminateFogotSession .terminateSession (forgotToLogout .getSessionId ()).assertAt ();
94
+
95
+ LoginPage login = HomePage .go (this .driver );
96
+ login .assertAt ();
97
+ }
98
+
99
+ private static HomePage home (WebDriver driver ) {
100
+ LoginPage login = HomePage .go (driver );
101
+ HomePage home = login .form ().login (HomePage .class );
102
+ home .assertAt ();
103
+ return home ;
104
+ }
105
+
82
106
@ TestConfiguration
83
107
static class Config {
84
108
Original file line number Diff line number Diff line change @@ -56,15 +56,26 @@ public void doesNotContainCookie(String cookieName) {
56
56
}
57
57
58
58
public void terminateButtonDisabled () {
59
+ String sessionId = getSessionId ();
60
+ WebElement element = getDriver ().findElement (By .id ("terminate-" + sessionId ));
61
+ assertThat (element .isEnabled ()).isFalse ();
62
+ }
63
+
64
+ public HomePage terminateSession (String sessionId ) {
65
+ WebElement terminate = getDriver ().findElement (By .id ("terminate-" + sessionId ));
66
+ terminate .click ();
67
+ return new HomePage (getDriver ());
68
+ }
69
+
70
+ public String getSessionId () {
59
71
Set <Cookie > cookies = getDriver ().manage ().getCookies ();
60
72
String cookieValue = null ;
61
73
for (Cookie cookie : cookies ) {
62
74
if ("SESSION" .equals (cookie .getName ())) {
63
75
cookieValue = new String (Base64 .getDecoder ().decode (cookie .getValue ()));
64
76
}
65
77
}
66
- WebElement element = getDriver ().findElement (By .id ("terminate-" + cookieValue ));
67
- assertThat (element .isEnabled ()).isFalse ();
78
+ return cookieValue ;
68
79
}
69
80
70
81
public HomePage logout () {
Original file line number Diff line number Diff line change 26
26
import org .springframework .stereotype .Controller ;
27
27
import org .springframework .ui .Model ;
28
28
import org .springframework .web .bind .annotation .PathVariable ;
29
+ import org .springframework .web .bind .annotation .PostMapping ;
29
30
import org .springframework .web .bind .annotation .RequestMapping ;
30
- import org .springframework .web .bind .annotation .RequestMethod ;
31
31
32
32
/**
33
33
* Controller for sending the user to the login view.
@@ -50,7 +50,7 @@ public String index(Principal principal, Model model) {
50
50
}
51
51
// end::findbyusername[]
52
52
53
- @ RequestMapping ( value = "/sessions/{sessionIdToDelete}" , method = RequestMethod . DELETE )
53
+ @ PostMapping ( "/sessions/{sessionIdToDelete}" )
54
54
public String removeSession (Principal principal , @ PathVariable String sessionIdToDelete ) {
55
55
Set <String > usersSessionIds = this .sessions .findByPrincipalName (principal .getName ()).keySet ();
56
56
if (usersSessionIds .contains (sessionIdToDelete )) {
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ <h1>Secured Page</h1>
25
25
< td th:text ="${#temporals.format(sessionElement.lastAccessedTime.atZone(T(java.time.ZoneId).systemDefault()),'dd/MMM/yyyy HH:mm:ss')} "> </ td >
26
26
< td th:text ="${details?.accessType} "> </ td >
27
27
< td >
28
- < form th:action ="@{'/sessions/' + ${sessionElement.id}} " th:method ="delete ">
28
+ < form th:action ="@{'/sessions/' + ${sessionElement.id}} " th:method ="post ">
29
29
< input th:id ="'terminate-' + ${sessionElement.id} " type ="submit " value ="Terminate " th:disabled ="${sessionElement.id == #httpSession.id} "/>
30
30
</ form >
31
31
</ td >
You can’t perform that action at this time.
0 commit comments