在春天引導我試圖創建一個使用春季啓動RestTemplate基本身份驗證使用RestTemplateBuilder
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
builder.basicAuthorization("username", "password");
RestTemplate template = builder.build();
return template;
}
然後我在服務類注入RestTemplate一個RestTemplate將使用基本身份驗證
@Autowired
private RestTemplate restTemplate;
但是,我的請求失敗,出現401未經授權的異常:
Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
使用另一個REST客戶端(Postman),對相同URL的請求成功,因此我假設基本身份驗證無法正常工作。從調試輸出看起來好像認證頭沒有被設置。什麼會使這項工作?
去調試,看看它是否正確設置頭,我的猜測是你有更多的resttemplate在你的春天的上下文。 – Jaiwo99
春天裏只有一個RestTemplate。 –
分享你的客戶端代碼..如何發送請求 –