SecurityMockMvcRequestPostProcessors.user
使用 Spring Session with 时是否可以填充测试用户HeaderHttpSessionStrategy
。
我试过类似的东西:
mockMvc.perform(
get(URL)
.with(user("user").password("pwd").roles("USER", "ADMIN")))
.andExpect(status().isOk())
但它返回一个 403。没有with(user(
我得到一个 401,所以有区别。
我有一个失败的简单SecurityConfig
包含:
http
.anonymous()
.and()
.authorizeRequests()
.antMatchers("/api/**").hasAuthority("USER")
.and()
.csrf()
.disable()
.httpBasic()
.and()
.requestCache()
.requestCache(new NullRequestCache());
我与https://github.com/spring-projects/spring-session/tree/1.0.1.RELEASE/samples/rest非常相似,因为我有一个端点,我使用 http basic 进行身份验证。它通过标头返回身份验证令牌,然后在其他 REST 调用中使用该令牌。
所以我希望我可以with(user(
在这种情况下使用 来简化我的测试。