我試圖使用Spring實現一個RESTful Web服務。我已經設置了Spring Security來處理適用於REST服務的鏈接。我從Android應用程序調用此Web服務。我現在所做的是使用基本身份驗證連接到它。我正在努力尋找關於這是多麼安全的體面信息。我想我至少應該通過SSL或者其他什麼來撥打這些電話?保護Spring Rest服務
我的Android客戶端調用REST客戶
public MyClass callRest() {
final String url = "http://10.0.2.2:8080/myservice/rest/getSomething";
HttpAuthentication authHeader = new HttpBasicAuthentication(username,
password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
requestHeaders.setAccept(Collections
.singletonList(MediaType.APPLICATION_JSON));
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(
new MappingJacksonHttpMessageConverter());
try {
ResponseEntity<MyClass> response = restTemplate.exchange(url,
HttpMethod.GET, new HttpEntity<Object>(requestHeaders),
MyClass.class);
return response.getBody();
} catch (HttpClientErrorException e) {
return new MyClass();
}
}
所以,我已經把我的春季安全配置,現在的代碼:
<http auto-config='true'>
<intercept-url pattern="/rest/**" access="ROLE_USER"
requires-channel="https" />
</http>
我想不通的地方從那裏去,因爲現在連接不再工作了,當然是因爲https。我似乎無法找到如何使用Resttemplate解決這個問題的體面的例子。
任何幫助?
你是什麼意思它不起作用 - 你是否得到任何錯誤,或者它只是沒有返回響應?您是否嘗試請求「https」,因爲您的請求仍設置爲「http」。 –
我知道它仍然設置爲http,但我是否改變:如果我嘗試連接到Web服務,現在我收到一個SSLException。 – Eels
什麼是SSLException?將它添加到你的問題,這將真的幫助。 –