WIthMockUser 不起作用
WIthMockUser doesn't work
我不能使用@WithMockUser,无论我做什么它都不会在spring 测试中提供授权用户。也许是因为它的资源服务器但是..
配置:
@Configuration
@EnableResourceServer
class ResourceServerConfig extends ResourceServerConfigurerAdapter
...
@Override
public void configure(HttpSecurity http) throws Exception {
http
.anonymous().and().authorizeRequests().antMatchers("/**").hasRole("USER");
}
...
}
并测试class
@BeforeClass
public void setup(){
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.apply(springSecurity())
.build();
}
@Test
@WithMockUser()
public void shouldAddValueToStore() throws Exception {
ResultActions response = mockMvc.perform(post("/bucket/key")
.content("value"));
...
}
我一直收到 401 Access is denied (user is anonymous); redirecting to authentication entry point
。我试过在注释参数中设置用户名、角色,将 with(user..)
传递给 mockMvc,没有任何帮助。这是 spring 安全 4.0.4.RELEASE.
好的,在将 'stateless' 参数设置为 'false' 后它可以工作,如下所述:https://github.com/spring-projects/spring-security-oauth/issues/385
并使用 with(user..
ResultActions response = mockMvc.perform(post("/bucket/key")
.with(user("john@example.com"))
.content("value"));
WithMockUser 仍然无法正常工作,但这种方法现在已经足够好了。
我不能使用@WithMockUser,无论我做什么它都不会在spring 测试中提供授权用户。也许是因为它的资源服务器但是..
配置:
@Configuration
@EnableResourceServer
class ResourceServerConfig extends ResourceServerConfigurerAdapter
...
@Override
public void configure(HttpSecurity http) throws Exception {
http
.anonymous().and().authorizeRequests().antMatchers("/**").hasRole("USER");
}
...
}
并测试class
@BeforeClass
public void setup(){
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.apply(springSecurity())
.build();
}
@Test
@WithMockUser()
public void shouldAddValueToStore() throws Exception {
ResultActions response = mockMvc.perform(post("/bucket/key")
.content("value"));
...
}
我一直收到 401 Access is denied (user is anonymous); redirecting to authentication entry point
。我试过在注释参数中设置用户名、角色,将 with(user..)
传递给 mockMvc,没有任何帮助。这是 spring 安全 4.0.4.RELEASE.
好的,在将 'stateless' 参数设置为 'false' 后它可以工作,如下所述:https://github.com/spring-projects/spring-security-oauth/issues/385
并使用 with(user..
ResultActions response = mockMvc.perform(post("/bucket/key")
.with(user("john@example.com"))
.content("value"));
WithMockUser 仍然无法正常工作,但这种方法现在已经足够好了。