Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
1d7caf8e
Commit
1d7caf8e
authored
May 30, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Man up and deal with CSRF in integration test
Fixes gh-979
parent
ed0cfea6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
10 deletions
+27
-10
pom.xml
spring-boot-samples/spring-boot-sample-web-secure/pom.xml
+5
-0
SampleWebSecureApplication.java
...ain/java/sample/ui/secure/SampleWebSecureApplication.java
+0
-4
login.html
...sample-web-secure/src/main/resources/templates/login.html
+1
-1
SampleSecureApplicationTests.java
...t/java/sample/ui/secure/SampleSecureApplicationTests.java
+21
-5
No files found.
spring-boot-samples/spring-boot-sample-web-secure/pom.xml
View file @
1d7caf8e
...
@@ -27,6 +27,11 @@
...
@@ -27,6 +27,11 @@
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-thymeleaf
</artifactId>
<artifactId>
spring-boot-starter-thymeleaf
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
spring-boot-samples/spring-boot-sample-web-secure/src/main/java/sample/ui/secure/SampleWebSecureApplication.java
View file @
1d7caf8e
...
@@ -73,10 +73,6 @@ public class SampleWebSecureApplication extends WebMvcConfigurerAdapter {
...
@@ -73,10 +73,6 @@ public class SampleWebSecureApplication extends WebMvcConfigurerAdapter {
@Override
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
if
(!
security
.
isEnableCsrf
())
{
// For testing
http
.
csrf
().
disable
();
}
http
.
authorizeRequests
().
anyRequest
().
fullyAuthenticated
().
and
().
formLogin
()
http
.
authorizeRequests
().
anyRequest
().
fullyAuthenticated
().
and
().
formLogin
()
.
loginPage
(
"/login"
).
failureUrl
(
"/login?error"
).
permitAll
();
.
loginPage
(
"/login"
).
failureUrl
(
"/login?error"
).
permitAll
();
}
}
...
...
spring-boot-samples/spring-boot-sample-web-secure/src/main/resources/templates/login.html
View file @
1d7caf8e
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
</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"
/>
<input
type=
"hidden"
th:name=
"${_csrf.parameterName}"
th:value=
"${_csrf.token}"
th:if=
"${_csrf}"
/>
th:name=
"${_csrf.parameterName}"
th:value=
"${_csrf.token}"
/>
</form>
</form>
</div>
</div>
</div>
</div>
...
...
spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java
View file @
1d7caf8e
...
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertNotNull;
...
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertNotNull;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
...
@@ -48,7 +50,7 @@ import org.springframework.util.MultiValueMap;
...
@@ -48,7 +50,7 @@ import org.springframework.util.MultiValueMap;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleWebSecureApplication
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleWebSecureApplication
.
class
)
@WebAppConfiguration
@WebAppConfiguration
@IntegrationTest
(
{
"server.port:0"
,
"security.enable_csrf:false"
}
)
@IntegrationTest
(
"server.port:0"
)
@DirtiesContext
@DirtiesContext
public
class
SampleSecureApplicationTests
{
public
class
SampleSecureApplicationTests
{
...
@@ -62,14 +64,14 @@ public class SampleSecureApplicationTests {
...
@@ -62,14 +64,14 @@ public class SampleSecureApplicationTests {
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
headers
),
String
.
class
);
assertEquals
(
HttpStatus
.
OK
,
entity
.
getStatusCode
());
assertEquals
(
HttpStatus
.
FOUND
,
entity
.
getStatusCode
());
assertTrue
(
"Wrong
body (title doesn't match):\n"
+
entity
.
getBody
(),
assertTrue
(
"Wrong
location:\n"
+
entity
.
getHeaders
(),
entity
.
get
Body
().
contains
(
"<title>L
ogin"
));
entity
.
get
Headers
().
getLocation
().
toString
().
endsWith
(
port
+
"/l
ogin"
));
}
}
@Test
@Test
public
void
testLogin
()
throws
Exception
{
public
void
testLogin
()
throws
Exception
{
HttpHeaders
headers
=
new
Http
Headers
();
HttpHeaders
headers
=
get
Headers
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
headers
.
setContentType
(
MediaType
.
APPLICATION_FORM_URLENCODED
);
headers
.
setContentType
(
MediaType
.
APPLICATION_FORM_URLENCODED
);
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<
String
,
String
>();
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<
String
,
String
>();
...
@@ -86,6 +88,20 @@ public class SampleSecureApplicationTests {
...
@@ -86,6 +88,20 @@ public class SampleSecureApplicationTests {
entity
.
getHeaders
().
get
(
"Set-Cookie"
));
entity
.
getHeaders
().
get
(
"Set-Cookie"
));
}
}
private
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
String
.
class
);
assertEquals
(
HttpStatus
.
OK
,
page
.
getStatusCode
());
String
cookie
=
page
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
Matcher
matcher
=
Pattern
.
compile
(
"(?s).*name=\"_csrf\".*?value=\"([^\"]+).*"
).
matcher
(
page
.
getBody
());
assertTrue
(
"No csrf token: "
+
page
.
getBody
(),
matcher
.
matches
());
headers
.
set
(
"X-CSRF-TOKEN"
,
matcher
.
group
(
1
));
return
headers
;
}
@Test
@Test
public
void
testCss
()
throws
Exception
{
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment