On this page
80. 使用 Spring Security 进行测试
Spring Security 提供了对以特定用户身份运行测试的支持。例如,下面的代码段中的测试将与具有ADMIN
角色的经过身份验证的用户一起运行。
@Test
@WithMockUser(roles="ADMIN")
public void requestProtectedUrlWithUser() throws Exception {
mvc
.perform(get("/"))
...
}
Spring Security 提供了与 Spring MVC Test 的全面集成,并且在使用@WebMvcTest
slice 和MockMvc
测试控制器时也可以使用它。
有关 Spring Security 的测试支持的更多详细信息,请参阅 Spring Security 的reference documentation。