2
我有一个弹簧休息mvc控制器,其URL为“/ public/rest/vehicle/get”。在我的安全配置中,我已经定义任何/ 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();
当我使用浏览器或任何其他方式启动我的应用程序并提交请求时,此配置正常工作。 现在,我有一个测试类,它看起来像这样,
@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"));
}}
现在,当我运行这个测试,它说
java.lang.AssertionError: Status
Expected :200
Actual :401
当我打印请求响应,我看到它是在抱怨,因为的安全。 “”错误消息=需要完全验证来访问此资源“ 任何想法,为什么它不与我有的安全配置一起工作,以及有什么办法强制它使用正确的配置?在此先感谢