简体   繁体   English

WIthMockUser不起作用

[英]WIthMockUser doesn't work

I can't use @WithMockUser, no matter what I do it doesn't provide authorized user in spring test. 我不能使用@WithMockUser,无论我做什么都不能在春季测试中提供授权用户。 Maybe it's because its resource server but.. 也许是因为它的资源服务器但是

Config: 配置:

@Configuration
@EnableResourceServer
class ResourceServerConfig extends ResourceServerConfigurerAdapter 
...
    @Override
    public void configure(HttpSecurity http) throws Exception {
    http
            .anonymous().and().authorizeRequests().antMatchers("/**").hasRole("USER");
}
...
}

And test 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"));
...
}

I keep getting 401 Access is denied (user is anonymous); redirecting to authentication entry point 我不断收到401 Access is denied (user is anonymous); redirecting to authentication entry point Access is denied (user is anonymous); redirecting to authentication entry point . Access is denied (user is anonymous); redirecting to authentication entry point I've tried with setting usernames, roles in annotation parameters, passing with(user..) to mockMvc, nothing helps. 我尝试设置用户名,注释参数中的角色,将with(user..)传递给嘲笑Mvc,没有任何帮助。 It's spring security 4.0.4.RELEASE. 这是Spring Security 4.0.4.RELEASE。

ok, it works after setting 'stateless' parameter to 'false' as described here: https://github.com/spring-projects/spring-security-oauth/issues/385 好的,如此处所述,在将'stateless'参数设置为'false'之后它可以工作: https : //github.com/spring-projects/spring-security-oauth/issues/385

and using with(user.. 并使用with(user..

ResultActions response = mockMvc.perform(post("/bucket/key")
            .with(user("john@example.com"))
            .content("value"));

WithMockUser is still not working, but this method is good enough for now. WithMockUser仍然无法正常工作,但是这种方法目前已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM