[英]Spring RestTemplate gives 401 Unauthorized error when sending with Authorization Header
我正在尝试使用以下代码在我的 Spring Rest 应用程序中点击一个 Microsoft Flow POST URL,但它给了我 401 错误。 我的代码:
@RequestMapping(value = "/hookslistner", method = RequestMethod.POST)
public ResponseEntity<Void> recieveWebhook(@RequestBody InventorySystemModel inventory,
@RequestHeader("event") String event,
@RequestHeader("Authorization") String authorization) {
// authorization = "Basic <Base64 encoded value of username:pwd>"
RestTemplate restTemplate = new RestTemplate();
org.springframework.http.HttpHeaders httpHeaders = new org.springframework.http.HttpHeaders();
String url = "https://prod-01.centralindia.logic.azure.com/workflows/835348<hiding rest of part>";
String headerName = "Authorization";
httpHeaders.add(headerName, authorization);
httpHeaders.add("Content-Type", "application/json");
HttpEntity<String> requestEntity = new HttpEntity<>("Headers", httpHeaders);
System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody());
}
错误:
SEVERE: Servlet.service() for servlet [webhooks] in context with path [/inventoryhooks] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Unauthorized] with root cause
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
.
.
.
是不是因为我的目标 url 是 https 而我的本地主机是 http?
谁能指出我出了什么问题?
有两点供您检查此问题:
authorization
值是base64
编码的。更新:
由于目标服务器是 https 服务器,问题是您尚未将ssl
信息配置到RestTempalte
。 可以参考以下代码片段获取一个ssl restTemplate:
@Configuration
public class RestClientConfig {
@Bean
public RestOperations restOperations(ClientHttpRequestFactory clientHttpRequestFactory) throws Exception {
return new RestTemplate(clientHttpRequestFactory);
}
@Bean
public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Bean
public HttpClient httpClient(@Value("${keystore.file}") String file, @Value("${keystore.pass}") String password) throws Exception {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream inStream = new FileInputStream(file);
try {
trustStore.load(inStream, password.toCharArray());
} finally {
inStream.close();
}
SSLContext sslcontext =
SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1.2"}, null,
null);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
测试用例:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = RestClientConfig.class)
public class RestClientTest {
@Autowired
private RestOperations rest;
private HttpHeaders getHeaders(){
String plainCredentials="admin:admin";
String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Credentials);
return headers;
}
@Test
public void test() {
HttpEntity<String> request = new HttpEntity<String>(getHeaders());
ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, request, String.class);
System.out.println(response.getBody());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.