Skip to content

Commit b7d94d1

Browse files
jgayosoDave Syer
authored and
Dave Syer
committed
Optimized login form - delegated CSRF token creation to thymeleaf
Also added additional test to verify behaviour. Fixes spring-projectsgh-1039
1 parent aa30fdb commit b7d94d1

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

spring-boot-samples/spring-boot-sample-web-method-security/src/main/resources/templates/login.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
<p th:if="${param.logout}" class="alert">You have been logged out</p>
2121
<p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p>
2222
<h2>Login with Username and Password</h2>
23-
<form name="form" action="/login" method="POST">
23+
<form name="form" th:action="@{/login}" action="/login" method="POST">
2424
<fieldset>
2525
<input type="text" name="username" value="" placeholder="Username" />
2626
<input type="password" name="password" placeholder="Password" />
2727
</fieldset>
2828
<input type="submit" id="login" value="Login"
29-
class="btn btn-primary" /> <input type="hidden"
30-
th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
29+
class="btn btn-primary" />
3130
</form>
3231
</div>
3332
</div>

spring-boot-samples/spring-boot-sample-web-secure/src/main/resources/templates/login.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
<p th:if="${param.logout}" class="alert">You have been logged out</p>
2121
<p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p>
2222
<h2>Login with Username and Password</h2>
23-
<form name="form" action="/login" method="POST">
23+
<form name="form" th:action="@{/login}" action="/login" method="POST">
2424
<fieldset>
2525
<input type="text" name="username" value="" placeholder="Username" />
2626
<input type="password" name="password" placeholder="Password" />
2727
</fieldset>
2828
<input type="submit" id="login" value="Login"
29-
class="btn btn-primary" /> <input type="hidden"
30-
th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
29+
class="btn btn-primary" />
3130
</form>
3231
</div>
3332
</div>

spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ public void testHome() throws Exception {
6969
entity.getHeaders().getLocation().toString().endsWith(port + "/login"));
7070
}
7171

72+
@Test
73+
public void testLoginPage() throws Exception {
74+
HttpHeaders headers = new HttpHeaders();
75+
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
76+
ResponseEntity<String> entity = new TestRestTemplate().exchange(
77+
"http://localhost:" + this.port + "/login", HttpMethod.GET, new HttpEntity<Void>(
78+
headers), String.class);
79+
assertEquals(HttpStatus.OK, entity.getStatusCode());
80+
assertTrue("Wrong content:\n" + entity.getBody(),
81+
entity.getBody().contains("_csrf"));
82+
}
83+
7284
@Test
7385
public void testLogin() throws Exception {
7486
HttpHeaders headers = getHeaders();

0 commit comments

Comments
 (0)