【问题标题】:How to disable securty on controller Unit Test?如何禁用控制器单元测试的安全性?
【发布时间】:2019-01-20 03:42:46
【问题描述】:

我尝试禁用控制器单元测试的安全性,但总是出现错误 403。

我的单元测试:

@RunWith(SpringRunner.class)
@WebMvcTest(value = MeasureController.class, secure = false)
@AutoConfigureMockMvc(secure = false)
public class MeasureControllerTest {

    @Autowired
    private MockMvc mvc;
    @Autowired
    private ObjectMapper objectMapper;
    @MockBean
    private ObjectService objectService;
    @Autowired
    private MeasureController measureController;

    /**
     * Test of sayHello method, of class MeasureController.
     *
     * @throws java.lang.Exception
     */
    @Test
    public void OnPostShouldReturnCreatedStatusIfEmptyMeasure() throws Exception {
        final String url = "/object/" + uuidKey + "/measures/";
        this.mvc.perform(post(url)
                .content("[]")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isCreated());

        verifyZeroInteractions(objectService);
    }
}

安全配置:

@Configuration
@EnableResourceServer
public class SecurityResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests()
                .antMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**").permitAll()
                .anyRequest().authenticated()
                ;
    }

    @Primary
    @Bean
    public RemoteTokenServices tokenServices() {
        final RemoteTokenServices tokenService = new RemoteTokenServices();
        tokenService.setCheckTokenEndpointUrl("https://..../oauth/check_token");
        tokenService.setClientId(".....");
        tokenService.setClientSecret(".....");
        return tokenService;
    }
}

spring 文档说要将 AutoConfigureMockMvc.secure 设置为 false 或将 WebMvcTest.secure 设置为 false。但两者都不会禁用安全性。我错过了什么?

我使用 Spring Boot 2.0.4。和 spring-security-oauth2 2.3.3.RELEASE

【问题讨论】:

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


    【解决方案1】:

    “WebMvcTest.secure”已被弃用。您必须对控制器进行测试:

    @RunWith(SpringRunner.class)
    @WebMvcTest(value = PatientDeviceController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
    @AutoConfigureMockMvc(secure = false)
    

    【讨论】:

    【解决方案2】:

    对于其他来这里寻找答案的人(以及我未来的自己),在 Spring Boot 2.2 中进行了测试:

    @WebMvcTest(controllers = YourController.class,
            excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = WebSecurityConfigurer.class) },
            excludeAutoConfiguration = { SecurityAutoConfiguration.class})
    

    【讨论】:

      猜你喜欢
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2019-03-30
      • 2014-10-25
      • 2017-06-08
      • 1970-01-01
      相关资源
      最近更新 更多