2017-06-19 70 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的,所以你可以看到什么是从连接传送到客户端 –