Skip to content

Commit d8ae336

Browse files
committed
Find by Username Sample switch from DELETE to POST
Spring Boot 2.2 no longer adds HiddenHttpMethodFilter by default See https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes#httphiddenmethodfilter-disabled-by-default This means that trying to map DELETE requests using _method variable does not work. This changes the mapping to use a POST which doesn't require the HiddenHttpMethodFilter which might expose the application to unnecessary security risk by allowing the HTTP method to be overridden. Closes gh-1613
1 parent 315112f commit d8ae336

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

spring-session-samples/spring-session-sample-boot-findbyusername/src/integration-test/java/sample/FindByUsernameTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class FindByUsernameTests {
5353

5454
private WebDriver driver;
5555

56+
private WebDriver driver2;
57+
5658
@BeforeEach
5759
void setup() {
5860
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
@@ -61,6 +63,9 @@ void setup() {
6163
@AfterEach
6264
void tearDown() {
6365
this.driver.quit();
66+
if (this.driver2 != null) {
67+
this.driver2.quit();
68+
}
6469
}
6570

6671
@Test
@@ -79,6 +84,25 @@ void login() {
7984
home.terminateButtonDisabled();
8085
}
8186

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+
82106
@TestConfiguration
83107
static class Config {
84108

spring-session-samples/spring-session-sample-boot-findbyusername/src/integration-test/java/sample/pages/HomePage.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,26 @@ public void doesNotContainCookie(String cookieName) {
5656
}
5757

5858
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() {
5971
Set<Cookie> cookies = getDriver().manage().getCookies();
6072
String cookieValue = null;
6173
for (Cookie cookie : cookies) {
6274
if ("SESSION".equals(cookie.getName())) {
6375
cookieValue = new String(Base64.getDecoder().decode(cookie.getValue()));
6476
}
6577
}
66-
WebElement element = getDriver().findElement(By.id("terminate-" + cookieValue));
67-
assertThat(element.isEnabled()).isFalse();
78+
return cookieValue;
6879
}
6980

7081
public HomePage logout() {

spring-session-samples/spring-session-sample-boot-findbyusername/src/main/java/sample/mvc/IndexController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.springframework.stereotype.Controller;
2727
import org.springframework.ui.Model;
2828
import org.springframework.web.bind.annotation.PathVariable;
29+
import org.springframework.web.bind.annotation.PostMapping;
2930
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RequestMethod;
3131

3232
/**
3333
* Controller for sending the user to the login view.
@@ -50,7 +50,7 @@ public String index(Principal principal, Model model) {
5050
}
5151
// end::findbyusername[]
5252

53-
@RequestMapping(value = "/sessions/{sessionIdToDelete}", method = RequestMethod.DELETE)
53+
@PostMapping("/sessions/{sessionIdToDelete}")
5454
public String removeSession(Principal principal, @PathVariable String sessionIdToDelete) {
5555
Set<String> usersSessionIds = this.sessions.findByPrincipalName(principal.getName()).keySet();
5656
if (usersSessionIds.contains(sessionIdToDelete)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ <h1>Secured Page</h1>
2525
<td th:text="${#temporals.format(sessionElement.lastAccessedTime.atZone(T(java.time.ZoneId).systemDefault()),'dd/MMM/yyyy HH:mm:ss')}"></td>
2626
<td th:text="${details?.accessType}"></td>
2727
<td>
28-
<form th:action="@{'/sessions/' + ${sessionElement.id}}" th:method="delete">
28+
<form th:action="@{'/sessions/' + ${sessionElement.id}}" th:method="post">
2929
<input th:id="'terminate-' + ${sessionElement.id}" type="submit" value="Terminate" th:disabled="${sessionElement.id == #httpSession.id}"/>
3030
</form>
3131
</td>

0 commit comments

Comments
 (0)