如何从WebMvcTest测试中排除Web安全?

时间:2019-05-28 08:02:58

标签: spring-boot spring-mvc spring-security spring-test-mvc

我的测试始终失败并显示401错误:

@DisplayName("My controller")
@WebMvcTest(controllers = MyController.class,
        excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.keycloak.adapters.*")})
public class MyControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void arrayObjectLinks() throws Exception {
        mvc.perform(get(Application.VERSION_URL + MyController.URL))
            .andExpect(status().isOk());
   }
}

安全配置:

@KeycloakConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableConfigurationProperties(KeycloakSpringBootProperties.class)
public class WebSecurityConfiguration extends KeycloakWebSecurityConfigurerAdapter {

    ...

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers(apiURL).hasAnyRole(RoleType.EXTERNAL_SYSTEM.getId())
                .anyRequest().permitAll();
    }
}

如何完全禁用网络安全?我使用Spring Boot 2.1

0 个答案:

没有答案