|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Security 7.0.4! |
运行 Spring MVC 测试中的用户测试
经常希望以特定用户的身份运行测试。 有两类简单的方法来填充用户:
使用 Spring MVC Test 的 RequestPostProcessor 以用户身份运行
您有多种方式将用户与当前HttpServletRequest关联起来。
以下示例将以用户名为user、密码为password且角色为ROLE_USER的用户运行(该用户不一定需要存在):
-
Java
-
Kotlin
mvc
.perform(get("/").with(user("user")))
mvc.get("/") {
with(user("user"))
}
|
The support works by associating the user to the
|
您可以轻松进行自定义。 例如,以下内容将以用户名为 "admin"、密码为 "pass" 并具有 "ROLE_USER" 和 "ROLE_ADMIN" 角色的用户身份运行(该用户无需实际存在)。
-
Java
-
Kotlin
mvc
.perform(get("/admin").with(user("admin").password("pass").roles("USER","ADMIN")))
mvc.get("/admin") {
with(user("admin").password("pass").roles("USER","ADMIN"))
}
如果您希望使用自定义的UserDetails,可以轻松地将其指定为认证信息。例如,以下示例将使用指定的UserDetails(该UsernamePasswordAuthenticationToken不需要存在)来运行带有指定UserDetails作为主体的4。
-
Java
-
Kotlin
mvc
.perform(get("/").with(user(userDetails)))
mvc.get("/") {
with(user(userDetails))
}
您可以通过以下方式以匿名用户运行:
-
Java
-
Kotlin
mvc
.perform(get("/").with(anonymous()))
mvc.get("/") {
with(anonymous())
}
这在您使用默认用户运行并希望处理几个匿名用户的请求时特别有用。
如果您想要一个自定义的Authentication(不需要实际存在),可以使用以下方式实现:
-
Java
-
Kotlin
mvc
.perform(get("/").with(authentication(authentication)))
mvc.get("/") {
with(authentication(authentication))
}
您可以使用以下方式自定义SecurityContext:
-
Java
-
Kotlin
mvc
.perform(get("/").with(securityContext(securityContext)))
mvc.get("/") {
with(securityContext(securityContext))
}
我们还可以通过使用MockMvcBuilders的默认请求来确保每次请求都以特定用户运行。
例如,以下代码将以用户名为"admin"、密码为"password"且角色为"ROLE_ADMIN"的用户进行运行(该用户不一定真实存在):
-
Java
-
Kotlin
mvc = MockMvcBuilders
.webAppContextSetup(context)
.defaultRequest(get("/").with(user("user").roles("ADMIN")))
.apply(springSecurity())
.build();
mvc = MockMvcBuilders
.webAppContextSetup(context)
.defaultRequest<DefaultMockMvcBuilder>(get("/").with(user("user").roles("ADMIN")))
.apply<DefaultMockMvcBuilder>(springSecurity())
.build()
如果在多个测试中频繁使用同一个用户,建议将该用户信息移动到一个方法中。
例如,您可以在名为CustomSecurityMockMvcRequestPostProcessors的类中指定以下内容:
-
Java
-
Kotlin
public static RequestPostProcessor rob() {
return user("rob").roles("ADMIN");
}
fun rob(): RequestPostProcessor {
return user("rob").roles("ADMIN")
}
现在您可以在测试中使用静态导入 CustomSecurityMockMvcRequestPostProcessors:
-
Java
-
Kotlin
import static sample.CustomSecurityMockMvcRequestPostProcessors.*;
...
mvc
.perform(get("/").with(rob()))
import sample.CustomSecurityMockMvcRequestPostProcessors.*
//...
mvc.get("/") {
with(rob())
}
使用注解在Spring MVC测试中以用户身份运行
除了使用RequestPostProcessor来创建用户外,你还可以使用测试方法安全性中描述的注解。
例如,以下代码将在用户名为'user'、密码为'password'且角色为'ROLE_USER'的用户下运行测试:
-
Java
-
Kotlin
@Test
@WithMockUser
public void requestProtectedUrlWithUser() throws Exception {
mvc
.perform(get("/"))
...
}
@Test
@WithMockUser
fun requestProtectedUrlWithUser() {
mvc
.get("/")
// ...
}
以下内容将以用户名 "user",密码 "password",以及角色 "ROLE_ADMIN" 的用户运行测试:
-
Java
-
Kotlin
@Test
@WithMockUser(roles="ADMIN")
public void requestProtectedUrlWithUser() throws Exception {
mvc
.perform(get("/"))
...
}
@Test
@WithMockUser(roles = ["ADMIN"])
fun requestProtectedUrlWithUser() {
mvc
.get("/")
// ...
}