2016-03-24 306 views
0

我無法使用@WithMockUser,無論我做什麼,都不會在春季測試中提供授權用戶。也許它是因爲它的資源服務器,但..WIthMockUser does not work

配置:

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

和測試類

@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。

+0

@WithMockUser(username =「ram」,authorities = {「ROLE_XX」,「ROLE_AA」})或username可以省略 – Tiina

回答