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
This commit is contained in:
@@ -53,6 +53,8 @@ class FindByUsernameTests {
|
|||||||
|
|
||||||
private WebDriver driver;
|
private WebDriver driver;
|
||||||
|
|
||||||
|
private WebDriver driver2;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
|
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
|
||||||
@@ -61,6 +63,9 @@ class FindByUsernameTests {
|
|||||||
@AfterEach
|
@AfterEach
|
||||||
void tearDown() {
|
void tearDown() {
|
||||||
this.driver.quit();
|
this.driver.quit();
|
||||||
|
if (this.driver2 != null) {
|
||||||
|
this.driver2.quit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -79,6 +84,25 @@ class FindByUsernameTests {
|
|||||||
home.terminateButtonDisabled();
|
home.terminateButtonDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void terminateOtherSession() throws Exception {
|
||||||
|
HomePage forgotToLogout = home(this.driver);
|
||||||
|
|
||||||
|
this.driver2 = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
|
||||||
|
HomePage terminateFogotSession = home(this.driver2);
|
||||||
|
terminateFogotSession.terminateSession(forgotToLogout.getSessionId()).assertAt();
|
||||||
|
|
||||||
|
LoginPage login = HomePage.go(this.driver);
|
||||||
|
login.assertAt();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HomePage home(WebDriver driver) {
|
||||||
|
LoginPage login = HomePage.go(driver);
|
||||||
|
HomePage home = login.form().login(HomePage.class);
|
||||||
|
home.assertAt();
|
||||||
|
return home;
|
||||||
|
}
|
||||||
|
|
||||||
@TestConfiguration
|
@TestConfiguration
|
||||||
static class Config {
|
static class Config {
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,18 @@ public class HomePage extends BasePage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void terminateButtonDisabled() {
|
public void terminateButtonDisabled() {
|
||||||
|
String sessionId = getSessionId();
|
||||||
|
WebElement element = getDriver().findElement(By.id("terminate-" + sessionId));
|
||||||
|
assertThat(element.isEnabled()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HomePage terminateSession(String sessionId) {
|
||||||
|
WebElement terminate = getDriver().findElement(By.id("terminate-" + sessionId));
|
||||||
|
terminate.click();
|
||||||
|
return new HomePage(getDriver());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSessionId() {
|
||||||
Set<Cookie> cookies = getDriver().manage().getCookies();
|
Set<Cookie> cookies = getDriver().manage().getCookies();
|
||||||
String cookieValue = null;
|
String cookieValue = null;
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie : cookies) {
|
||||||
@@ -63,8 +75,7 @@ public class HomePage extends BasePage {
|
|||||||
cookieValue = new String(Base64.getDecoder().decode(cookie.getValue()));
|
cookieValue = new String(Base64.getDecoder().decode(cookie.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WebElement element = getDriver().findElement(By.id("terminate-" + cookieValue));
|
return cookieValue;
|
||||||
assertThat(element.isEnabled()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HomePage logout() {
|
public HomePage logout() {
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ import org.springframework.session.Session;
|
|||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller for sending the user to the login view.
|
* Controller for sending the user to the login view.
|
||||||
@@ -50,7 +50,7 @@ public class IndexController {
|
|||||||
}
|
}
|
||||||
// end::findbyusername[]
|
// end::findbyusername[]
|
||||||
|
|
||||||
@RequestMapping(value = "/sessions/{sessionIdToDelete}", method = RequestMethod.DELETE)
|
@PostMapping("/sessions/{sessionIdToDelete}")
|
||||||
public String removeSession(Principal principal, @PathVariable String sessionIdToDelete) {
|
public String removeSession(Principal principal, @PathVariable String sessionIdToDelete) {
|
||||||
Set<String> usersSessionIds = this.sessions.findByPrincipalName(principal.getName()).keySet();
|
Set<String> usersSessionIds = this.sessions.findByPrincipalName(principal.getName()).keySet();
|
||||||
if (usersSessionIds.contains(sessionIdToDelete)) {
|
if (usersSessionIds.contains(sessionIdToDelete)) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
<td th:text="${#temporals.format(sessionElement.lastAccessedTime.atZone(T(java.time.ZoneId).systemDefault()),'dd/MMM/yyyy HH:mm:ss')}"></td>
|
<td th:text="${#temporals.format(sessionElement.lastAccessedTime.atZone(T(java.time.ZoneId).systemDefault()),'dd/MMM/yyyy HH:mm:ss')}"></td>
|
||||||
<td th:text="${details?.accessType}"></td>
|
<td th:text="${details?.accessType}"></td>
|
||||||
<td>
|
<td>
|
||||||
<form th:action="@{'/sessions/' + ${sessionElement.id}}" th:method="delete">
|
<form th:action="@{'/sessions/' + ${sessionElement.id}}" th:method="post">
|
||||||
<input th:id="'terminate-' + ${sessionElement.id}" type="submit" value="Terminate" th:disabled="${sessionElement.id == #httpSession.id}"/>
|
<input th:id="'terminate-' + ${sessionElement.id}" type="submit" value="Terminate" th:disabled="${sessionElement.id == #httpSession.id}"/>
|
||||||
</form>
|
</form>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user