2017-07-15 16 views
2

私は "/ public/rest/vehicle/get"というURLを持つspring rest mvcコントローラを持っています。私のセキュリティ設定では、/ public/restのリクエストには認証が必要ではないと定義しました。SpringBoot @WebMvcTestセキュリティの問題

http. 
      csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/home/**", "/**", "/css/**", "/js/**", "/fonts/**", "/images/**", "/public/rest/**","/login*","/signin/**","/signup/**").permitAll() 
      .antMatchers("/property/**").authenticated() 
      .and() 
      .formLogin().loginPage("/login").permitAll() 
      .and().httpBasic().disable(); 

この設定は、アプリケーションを起動してブラウザやその他の手段でリクエストを送信するとうまく動作します。 は今、私はこのテストを実行すると、それは

java.lang.AssertionError: Status 
Expected :200 
Actual :401 

私は要求応答を印刷するとき、私はので、それが不平を言っている参照と言い、今

@RunWith(SpringRunner.class) 
@WebMvcTest(VehicleController.class) 
public class VehicleControllerTest { 

    @Autowired 
    private MockMvc mockMvc; 

    @MockBean 
    private VehicleService vehicleService; 


    @Test 
    public void getVehicle() throws Exception { 
     given(this.vehicleService.get(0)). 
       willReturn(new VehicleEquipmentDTO()); 
     this.mockMvc.perform(get("/public/rest/vehicle/get").param("id","0")) 
       .andDo(print()) 
       .andExpect(status().isOk());//.andExpect(content().string("Honda Civic")); 
    }} 

、次のようになり、テストクラスを持っていますセキュリティの"エラーメッセージ=このリソースにアクセスするには完全な認証が必要です" 私が持っているセキュリティ設定ではうまくいかない理由と正しい設定を使用する回避策はありますか?事前に感謝

答えて

3

最後に理由が見つかりました。 WebMvcTestはテストだけがスライスされているので、セキュリティ設定は適用されません。回避策は、明示的にインポートすることです。

@Import(WebSecurityConfig.class) 
関連する問題