Browse Source

Optimized login form - delegated CSRF token creation to thymeleaf

Also added additional test to verify behaviour.

Fixes gh-1039
pull/1052/head
Javier Gayoso 12 years ago committed by Dave Syer
parent
commit
b7d94d1364
  1. 5
      spring-boot-samples/spring-boot-sample-web-method-security/src/main/resources/templates/login.html
  2. 5
      spring-boot-samples/spring-boot-sample-web-secure/src/main/resources/templates/login.html
  3. 12
      spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java

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

@ -20,14 +20,13 @@
<p th:if="${param.logout}" class="alert">You have been logged out</p> <p th:if="${param.logout}" class="alert">You have been logged out</p>
<p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p> <p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p>
<h2>Login with Username and Password</h2> <h2>Login with Username and Password</h2>
<form name="form" action="/login" method="POST"> <form name="form" th:action="@{/login}" action="/login" method="POST">
<fieldset> <fieldset>
<input type="text" name="username" value="" placeholder="Username" /> <input type="text" name="username" value="" placeholder="Username" />
<input type="password" name="password" placeholder="Password" /> <input type="password" name="password" placeholder="Password" />
</fieldset> </fieldset>
<input type="submit" id="login" value="Login" <input type="submit" id="login" value="Login"
class="btn btn-primary" /> <input type="hidden" class="btn btn-primary" />
th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
</form> </form>
</div> </div>
</div> </div>

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

@ -20,14 +20,13 @@
<p th:if="${param.logout}" class="alert">You have been logged out</p> <p th:if="${param.logout}" class="alert">You have been logged out</p>
<p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p> <p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p>
<h2>Login with Username and Password</h2> <h2>Login with Username and Password</h2>
<form name="form" action="/login" method="POST"> <form name="form" th:action="@{/login}" action="/login" method="POST">
<fieldset> <fieldset>
<input type="text" name="username" value="" placeholder="Username" /> <input type="text" name="username" value="" placeholder="Username" />
<input type="password" name="password" placeholder="Password" /> <input type="password" name="password" placeholder="Password" />
</fieldset> </fieldset>
<input type="submit" id="login" value="Login" <input type="submit" id="login" value="Login"
class="btn btn-primary" /> <input type="hidden" class="btn btn-primary" />
th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
</form> </form>
</div> </div>
</div> </div>

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

@ -69,6 +69,18 @@ public class SampleSecureApplicationTests {
entity.getHeaders().getLocation().toString().endsWith(port + "/login")); entity.getHeaders().getLocation().toString().endsWith(port + "/login"));
} }
@Test
public void testLoginPage() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/login", HttpMethod.GET, new HttpEntity<Void>(
headers), String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong content:\n" + entity.getBody(),
entity.getBody().contains("_csrf"));
}
@Test @Test
public void testLogin() throws Exception { public void testLogin() throws Exception {
HttpHeaders headers = getHeaders(); HttpHeaders headers = getHeaders();

Loading…
Cancel
Save