spring - spring testRestTemplate 基本身份验证不适用于 POST 请求 Httpstatus 302
问题描述
我正在使用 Springorg.springframework.boot.test.web.client.TestRestTemplate
来测试控制器代码。
我可以简单地使用 来测试 GET API testRestTemplate.withBasicAuth("test", "test").exchange(...)
,但同样的方法不适用于同一控制器中的 POST 端点。
它返回302 found
带有以下 ResponseEntity 的 HttpStatus:
<302,[Set-Cookie:"JSESSIONID=332C559B7CABE5682EE9910A6FF834DA; Path=/; HttpOnly", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Location:"http://localhost:61598/login", Content-Length:"0", Date:"Thu, 09 Jul 2020 14:24:18 GMT", Server:"Application Server"]>
控制器代码:
@GetMapping(value = "/filterable_columns", produces = "application/json")
public List<FilterableField> filterableFieldList() {
log.info("Received request for list of filterable fields");
return metaDataService.filterableFieldList(referenceDataService.getSchemaUri());
}
@PostMapping(value = "/search", produces = "application/json")
public List<Map<String, Object>> filteredSearch(@RequestBody FilteredSearchRequest filteredSearchRequest) throws IllegalAccessException {
log.info("Received request for filtered search");
return referenceDataService.filteredSearch(filteredSearchRequest);
}
控制器测试:
@Test // This works as expected
void filterableFieldList() {
val reply = testRestTemplate.withBasicAuth("test", "test")
.exchange("/reference_data/filterable_columns",
HttpMethod.GET, null,
new ParameterizedTypeReference<List<FilterableField>>() {
});
assertEquals(HttpStatus.OK, reply.getStatusCode());
assertFalse(Objects.requireNonNull(reply.getBody()).isEmpty());
}
@Test // This does not work
void filteredSearch() {
val reply = testRestTemplate.withBasicAuth("test", "test")
.exchange("/reference_data/search",
HttpMethod.POST,
new HttpEntity<>(new FilteredSearchRequest()),
new ParameterizedTypeReference<List<Map<String, Object>>>() {
}
);
System.out.println(reply);
assertEquals(HttpStatus.OK, reply.getStatusCode());
}
AdfsSecurityConfiguration.java:
@Configuration
@ConditionalOnProperty(prefix = "moneta.security.adfs", name = "enabled", matchIfMissing = true)
public class AdfsSecurityConfiguration extends WebSecurityConfigurerAdapter {
private final Environment environment;
private final AdfsConfigurer<HttpSecurity> adfsConfigurer;
public AdfsSecurityConfiguration(final Environment environment, final AdfsConfigurer<HttpSecurity> adfsConfigurer) {
this.environment = environment;
this.adfsConfigurer = adfsConfigurer;
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
if (!isRunningLocally()) {
http.requiresChannel().anyRequest().requiresSecure();
}
http.apply(adfsConfigurer).and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().authorizeRequests()
.requestMatchers(EndpointRequest.to("keepalive", "info", "health", "env"), EndpointRequest.toLinks())
.permitAll().anyRequest().authenticated().and().csrf().disable();
}
private boolean isRunningLocally() {
return environment.acceptsProfiles(Profiles.of("default"));
}
}
应用程序-test.yml:
spring:
security:
user:
name: test
password: test
解决方案
推荐阅读
- python - 在测试子类时使用python的模拟补丁来模拟父类属性的值(通过调用导入的模块设置)
- c - 如何在 C 中重复 20 次后停止 for 循环
- python - Hbase shell 得到这个错误:fstat unimplemented unsupported or native support failed to load
- firebase - Gcloud auth 干扰 Firebase 凭据
- java - HashSet 与 Linked Hashset 的迭代
- java - 具有奇怪行为的左移运算符
- python - Pandas 数据透视表返回 MemoryError
- python - 有没有办法在 for 循环中执行字符串?
- javascript - 事件处理程序中的 React Hook 不是函数错误?
- terraform - terraform env: : 没有这样的文件或目录