我在RestTemplate中是全新的,基本上也在REST API中。我想通过Jira REST API在我的应用程序中检索一些数据,但是要取回401 Unauthorized。在jira rest api documentation上找到并发表文章,但实际上并不知道如何将其重写为java,因为该示例使用curl命令行方式。我将不胜感激如何重写任何建议或建议:
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
使用spring rest模板进入java。其中ZnJlZDpmcmVk是base64编码的username:password字符串。非常感谢你。
答案 0 :(得分:110)
取自example on this site,我认为这是最自然的做法,方法是填写标题值并将标题传递给模板。
这是填写标题Authorization
:
String plainCreds = "willie:p@ssword";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
这是将标头传递给REST模板:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url, HttpMethod.GET, request, Account.class);
Account account = response.getBody();
答案 1 :(得分:56)
您可以使用spring-boot RestTemplateBuilder
Private Sub LogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoutToolStripMenuItem.Click
If MsgBox("Are You Sure?", vbYesNo, MsgBoxStyle.Question, "Logout") = MsgBoxResult.Yes Then
Form6.ShowDialog()
Me.Close()
End If
End Sub
(在SB 2.1.0之前是@Bean
RestOperations rest(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.basicAuthentication("user", "password").build();
}
)
答案 2 :(得分:20)
(也许)没有导入spring-boot的最简单方法。
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user", "password"));
答案 3 :(得分:15)
参考Spring Boot的TestRestTemplate
实现如下:
特别是,请参阅addAuthentication()方法,如下所示:
private void addAuthentication(String username, String password) {
if (username == null) {
return;
}
List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor(
username, password));
setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(),
interceptors));
}
同样,您可以轻松制作自己的RestTemplate
继承如TestRestTemplate
,如下:
答案 4 :(得分:7)
从Spring 5.1开始,您可以使用HttpHeaders.setBasicAuth
创建基本授权标头:
String username = "willie";
String password = ":p@ssword";
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
...other headers goes here...
将标头传递到RestTemplate:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url, HttpMethod.GET, request, Account.class);
Account account = response.getBody();
答案 5 :(得分:6)
而不是实例化如下:
TestRestTemplate restTemplate = new TestRestTemplate();
就这样做:
TestRestTemplate restTemplate = new TestRestTemplate(user, password);
它对我有用,我希望它有所帮助!
答案 6 :(得分:0)
有多种方法可以将基本的HTTP身份验证添加到RestTemplate
。
try {
// request url
String url = "https://jsonplaceholder.typicode.com/posts";
// create auth credentials
String authStr = "username:password";
String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
// create headers
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
// create request
HttpEntity request = new HttpEntity(headers);
// make a request
ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, request, String.class);
// get JSON response
String json = response.getBody();
} catch (Exception ex) {
ex.printStackTrace();
}
如果您使用的是Spring 5.1
或更高版本,则不再需要手动设置授权标头。请改用headers.setBasicAuth()
方法:
// create headers
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("username", "password");
@Service
public class RestService {
private final RestTemplate restTemplate;
public RestService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.basicAuthentication("username", "password")
.build();
}
// use `restTemplate` instance here
}
@Bean
RestOperations restTemplateBuilder(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.basicAuthentication("username", "password").build();
}
希望对您有帮助!
答案 7 :(得分:0)
使用setBasicAuth
定义凭据
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("myUsername", myPassword);
然后根据您的喜好创建请求。
示例:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
request, String.class);
String body = response.getBody();