我使用下面的代码,我将基本https身份验证的凭据设置为使用Spring Security的服务器。不幸的是我遇到像é,ò等特殊字符的问题...我在服务器上收到问号?
而不是正确的字符(用户名和密码)。有人知道怎么解决吗?
private RestClient(String username, String password) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
public static synchronized RestClient getInstance(String username, String password){
if (instance == null){
instance = new RestClient(username, password);
instance.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
}
return instance;
}
然后我用例如
RestClient restClient = RestClient.getInstance(username, password);
if (queryParams!=null && queryParams.length!=0){
response = restClient.getForObject(addQueryParam(url, queryParams), Response.class);
}
我甚至尝试使用URIEncoding="UTF-8"
进入tomcat的server.xml,但问题仍然存在。
更新 也许我已修复手动设置标题的问题:
private RestClient(String username, String password) {
String credential = Base64.getEncoder().encodeToString((username+":"+password).getBytes());
// create custom http headers for httpclient
List<BasicHeader> defaultHeaders = Arrays.asList(new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic "+credential));
HttpClient httpClient = HttpClients.custom().setDefaultHeaders(defaultHeaders).build();
setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
public static synchronized RestClient getInstance(String username, String password){
if (instance == null){
instance = new RestClient(username, password);
}
return instance;
}
我正在测试,让你知道