SEC-3094: Add @WithAnonymousUser & anonymous() MockMvcRequestPostProcessor
This commit is contained in:
@@ -137,6 +137,37 @@ For example, the following would run every test with a user with the username "a
|
||||
public class WithMockUserTests {
|
||||
----
|
||||
|
||||
|
||||
[[test-method-withanonymoususer]]
|
||||
=== @WithAnonymousUser
|
||||
|
||||
Using `@WithAnonymousUser` allows running as an anonymous user.
|
||||
This is especially convenient when you wish to run most of your tests with a specific user, but want to run a few tests as an anonymous user.
|
||||
For example, the following will run withMockUser1 and withMockUser2 using <<test-method-withmockuser,@WithMockUser>> and anonymous as an anonymous user.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WithMockUser
|
||||
public class WithUserClassLevelAuthenticationTests {
|
||||
|
||||
@Test
|
||||
public void withMockUser1() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withMockUser2() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void anonymous() throws Exception {
|
||||
// override default to run as anonymous user
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
[[test-method-withuserdetails]]
|
||||
=== @WithUserDetails
|
||||
|
||||
@@ -409,6 +440,16 @@ mvc
|
||||
.perform(get("/").with(user(userDetails)))
|
||||
----
|
||||
|
||||
You can run as anonymous user using the following:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
mvc
|
||||
.perform(get("/").with(anonymous()))
|
||||
----
|
||||
|
||||
This is especially useful if you are running with a default user and wish to execute a few requests as an anonymous user.
|
||||
|
||||
If you want a custom `Authentication` (which does not need to exist) you can do so using the following:
|
||||
|
||||
[source,java]
|
||||
|
||||
@@ -373,6 +373,7 @@ This will give you access to the entire project history (including all releases
|
||||
** <<test-method-meta-annotations,Test Meta Annotations>>
|
||||
** <<method-security-meta-annotations,Method Security Meta Annotations>>
|
||||
* <<el-access-web-path-variables,Path Variables in Web Security Expressions>>
|
||||
* <<test-method-withanonymoususer,@WithAnonymousUser>>
|
||||
|
||||
=== What's new in Spring Security 4.0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user