2017-06-19 71 views
0

我使用下面的代碼將基本http身份驗證的憑據設置爲使用Spring Security的服務器。 不幸的是我有像é,ò等特殊字符的問題...我在服務器上收到問號而不是正確的字符 有人知道如何解決它?我一直在尋找設置編碼沒有成功使用Spring身份驗證的特殊字符

import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.UsernamePasswordCredentials; 
import org.apache.http.client.CredentialsProvider; 
import org.apache.http.client.HttpClient; 
import org.apache.http.impl.client.BasicCredentialsProvider; 
import org.apache.http.impl.client.HttpClients; 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
import org.springframework.web.client.RestTemplate; 

public class RestClient extends RestTemplate { 
    public 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)); 
     } 
    } 

然後我調用Web服務與彈簧類:

RestClient restClient = new RestClient(username, password); 
response = restClient.getForObject(addQueryParam(url, queryParams), Response.class); 

UPDATE:

使用此代碼我有同樣的錯誤(問號而不是特殊字符)。一些想法?

public class RestClient extends RestTemplate { 
    private static RestClient instance; 

    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; 

    } 
} 
+0

「不幸的是我有特殊字符,如E,O等問題。」好的,問題到底是什麼? – rmlan

+0

我收到服務器問號而不是正確字符 – luca

回答

0

轉到你的web.xml文件和設置的URIEncoding爲UTF-8

<Connector port="8080" protocol="HTTP/1.1" 
      connectionTimeout="20000" 
      redirectPort="8443" 
     URIEncoding="UTF-8"/> 


<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" /> 

Source

+0

在服務器上還是在客戶端上?因爲從表單頁面登錄工作 – luca

+0

更改web.xml(服務端) – Elmehdi93

+0

但是您是否引用Tomcat配置文件?在春天,我使用java註釋 – luca

0

您應該添加合適的消息變換 在我來說,當我有一個類似的問題,我用這種配置:

RestTemplate rt = new RestTemplate(); 
rt.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); 

在此之後所有的工作還不錯

安傑洛

+0

我通常做的是:定義一個spring bean(單例作用域,或者,如果需要的話,另一種類型的spring作用域;在我的場景中,singleton就足夠了),然後注入這個bean。但我建議你的第一步是測試這個配置是否適合你:) –

+0

你可以發佈你如何更新課程以及你有什麼迴應? –

+0

所以例如,而不是'你'得到'?'我對嗎?如果是這樣,這似乎並沒有與春天有關。嘗試設置也追查日誌記錄HttpClient的,所以你可以看到什麼是從連接傳送到客戶端 –