2011-08-18 16 views
11

मुझे एंड्रॉइड देशी ऐप से https कनेक्शन पर कुकीज़ का उपयोग करने की आवश्यकता है। मैं RestTemplate का उपयोग कर रहा हूँ।वसंत एंड्रॉइड: https और कुकीज़ के साथ RestTemplate का उपयोग

जांच की जा रही अन्य थ्रेड (। जैसे Setting Security cookie using RestTemplate) मैं एक http कनेक्शन के भीतर कुकीज़ को संभालने में सक्षम था:

restTemplate.setRequestFactory(new YourClientHttpRequestFactory()); 

जहां YourClientHttpRequestFactory extends SimpleClientHttpRequestFactory

इस http पर लेकिन https पर नहीं ठीक काम करता है।

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient())); 

जहां HttpUtils यहाँ वर्णित है: http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html

मेरे समस्या यह है कि मैं करने की जरूरत है

दूसरी ओर मैं SSL प्रमाणपत्र पर भरोसा एंड्रॉयड के https समस्या को हल करने में सक्षम था ClientHttpRequestFactory के एक ही कार्यान्वयन का उपयोग करें।

1) एक तरह से https संभाल करने SimpleClientHttpRequestFactory

2 एक और दृष्टिकोण

+1

HttpUtils लिंक के लिए धन्यवाद! एसएसएल पर समाधान के लिए बेताब था और अन्य संकेतों ने मदद नहीं की थी। – Solata

उत्तर

8

मैं था का उपयोग कर) का उपयोग कर एक तरह से कुकीज़ को संभालने के लिए HttpComponentsClientHttpRequestFactory

3 लगता है) का उपयोग लगता है: तो मैं 3 विकल्प हैं एक ही समस्या। यहां मेरा समाधान है:

सबसे पहले, मैंने एसएसएल को वैसे ही संभाला जैसा आपने किया था (मैंने बॉब ली की विधि का उपयोग किया था)।

कुकीज एक अलग कहानी है। जिस तरह से मैंने रीस्ट टेम्पलेट के बिना अतीत में कुकीज को संभाला है (यानी सीधे अपाचे की एचटीपी क्लाइंट क्लास का उपयोग करना) एचटीपी कॉन्टेक्स्ट के उदाहरण को एचटीपी क्लाइंट के निष्पादन विधि में पास करना है। के एक कदम पीछे ले चलते हैं ...

HttpClient अतिभारित execute तरीकों की एक संख्या है, जिनमें से एक है:

execute(HttpUriRequest request, HttpContext context) 

HttpContext का उदाहरण एक CookieStore के लिए एक संदर्भ हो सकता है।

private HttpContext createHttpContext() { 

    CookieStore cookieStore = (CookieStore) StaticCacheHelper.retrieveObjectFromCache(COOKIE_STORE); 
    if (cookieStore == null) { 
     Log.d(getClass().getSimpleName(), "Creating new instance of a CookieStore"); 
     // Create a local instance of cookie store 
     cookieStore = new BasicCookieStore(); 
    } 

    // Create local HTTP context 
    HttpContext localContext = new BasicHttpContext(); 
    // Bind custom cookie store to the local context 
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 
    return localContext; 
} 
बेशक

, आपको अनुरोध भेजने से पहले CookieStore के कहने के लिए कुकीज़ जोड़ सकते हैं: जब आप HttpContext का एक उदाहरण बनाने के लिए, एक CookieStore (या तो एक नया एक, या एक आपने पिछले एक अनुरोध से बचाया) प्रदान करते हैं यदि आप चाहते हैं।

HttpResponse response = httpClient.execute(httpRequester, localContext); 

(जहां httpRequester HttpPost, HttpGet, आदि का एक उदाहरण है: अब जब आप विधि पर अमल फोन, HttpContext की कि उदाहरण का उपयोग)

आप अनुवर्ती अनुरोधों पर कुकीज़ को पुन: भेजने के लिए, सुनिश्चित करें कि आप कुकीज़ कहीं स्टोर बनाने की जरूरत है:

StaticCacheHelper.storeObjectInCache(COOKIE_STORE, localContext.getAttribute(ClientContext.COOKIE_STORE), MAX_MILLISECONDS_TO_LIVE_IN_CACHE); 

StaticCacheHelper वर्ग कि इस कोड में प्रयोग किया जाता है सिर्फ एक कस्टम वर्ग में डाटा स्टोर कर सकते हैं एक स्थिर नक्शा:

public class StaticCacheHelper { 

private static final int TIME_TO_LIVE = 43200000; // 12 hours 

private static Map<String, Element> cacheMap = new HashMap<String, Element>(); 

/** 
* Retrieves an item from the cache. If found, the method compares 
* the object's expiration date to the current time and only returns 
* the object if the expiration date has not passed. 
* 
* @param cacheKey 
* @return 
*/ 
public static Object retrieveObjectFromCache(String cacheKey) { 
    Element e = cacheMap.get(cacheKey); 
    Object o = null; 
    if (e != null) { 
     Date now = new Date(); 
     if (e.getExpirationDate().after(now)) { 
      o = e.getObject(); 
     } else { 
      removeCacheItem(cacheKey); 
     } 
    } 
    return o; 
} 

/** 
* Stores an object in the cache, wrapped by an Element object. 
* The Element object has an expiration date, which will be set to 
* now + this class' TIME_TO_LIVE setting. 
* 
* @param cacheKey 
* @param object 
*/ 
public static void storeObjectInCache(String cacheKey, Object object) { 
    Date expirationDate = new Date(System.currentTimeMillis() + TIME_TO_LIVE); 
    Element e = new Element(object, expirationDate); 
    cacheMap.put(cacheKey, e); 
} 

/** 
* Stores an object in the cache, wrapped by an Element object. 
* The Element object has an expiration date, which will be set to 
* now + the timeToLiveInMilliseconds value that is passed into the method. 
* 
* @param cacheKey 
* @param object 
* @param timeToLiveInMilliseconds 
*/ 
public static void storeObjectInCache(String cacheKey, Object object, int timeToLiveInMilliseconds) { 
    Date expirationDate = new Date(System.currentTimeMillis() + timeToLiveInMilliseconds); 
    Element e = new Element(object, expirationDate); 
    cacheMap.put(cacheKey, e); 
} 

public static void removeCacheItem(String cacheKey) { 
    cacheMap.remove(cacheKey); 
} 

public static void clearCache() { 
    cacheMap.clear(); 
} 

static class Element { 

    private Object object; 
    private Date expirationDate; 

    /** 
    * @param object 
    * @param key 
    * @param expirationDate 
    */ 
    private Element(Object object, Date expirationDate) { 
     super(); 
     this.object = object; 
     this.expirationDate = expirationDate; 
    } 
    /** 
    * @return the object 
    */ 
    public Object getObject() { 
     return object; 
    } 
    /** 
    * @param object the object to set 
    */ 
    public void setObject(Object object) { 
     this.object = object; 
    } 
    /** 
    * @return the expirationDate 
    */ 
    public Date getExpirationDate() { 
     return expirationDate; 
    } 
    /** 
    * @param expirationDate the expirationDate to set 
    */ 
    public void setExpirationDate(Date expirationDate) { 
     this.expirationDate = expirationDate; 
    } 
} 
} 

लेकिन !!!! 01/2012 तक स्प्रिंग एंड्रॉइड में रेस्ट टेम्पलेट आपको अनुरोध के निष्पादन के लिए एक HttpContext जोड़ने की सुविधा नहीं देता है !! यह स्प्रिंग फ्रेमवर्क 3.1.0.RELEASE में तय किया जा रहा है और यह फिक्स scheduled to be migrated into Spring Android 1.0.0.RC1 है।

तो, जब हमें स्प्रिंग एंड्रॉइड 1.0.0.आरसी 1 मिलता है, तो हमें उपर्युक्त उदाहरण में वर्णित संदर्भ जोड़ने में सक्षम होना चाहिए। तब तक, हमें ClientHttpRequestInterceptor का उपयोग करके अनुरोध/प्रतिक्रिया शीर्षलेख से कुकीज़ को जोड़ना/खींचना होगा।

public class MyClientHttpRequestInterceptor implements 
    ClientHttpRequestInterceptor { 

private static final String SET_COOKIE = "set-cookie"; 
private static final String COOKIE = "cookie"; 
private static final String COOKIE_STORE = "cookieStore"; 

/* (non-Javadoc) 
* @see org.springframework.http.client.ClientHttpRequestInterceptor#intercept(org.springframework.http.HttpRequest, byte[], org.springframework.http.client.ClientHttpRequestExecution) 
*/ 
@Override 
public ClientHttpResponse intercept(HttpRequest request, byte[] byteArray, 
     ClientHttpRequestExecution execution) throws IOException { 

    Log.d(getClass().getSimpleName(), ">>> entering intercept"); 
    List<String> cookies = request.getHeaders().get(COOKIE); 
    // if the header doesn't exist, add any existing, saved cookies 
    if (cookies == null) { 
     List<String> cookieStore = (List<String>) StaticCacheHelper.retrieveObjectFromCache(COOKIE_STORE); 
     // if we have stored cookies, add them to the headers 
     if (cookieStore != null) { 
      for (String cookie : cookieStore) { 
       request.getHeaders().add(COOKIE, cookie); 
      } 
     } 
    } 
    // execute the request 
    ClientHttpResponse response = execution.execute(request, byteArray); 
    // pull any cookies off and store them 
    cookies = response.getHeaders().get(SET_COOKIE); 
    if (cookies != null) { 
     for (String cookie : cookies) { 
      Log.d(getClass().getSimpleName(), ">>> response cookie = " + cookie); 
     } 
     StaticCacheHelper.storeObjectInCache(COOKIE_STORE, cookies); 
    } 
    Log.d(getClass().getSimpleName(), ">>> leaving intercept"); 
    return response; 
} 

} 

intercepter, अनुरोध को बीच में रोक कैश में लग रहा है, अगर वहाँ अनुरोध में जोड़ने के लिए कोई भी कुकी को देखने के लिए, तो अनुरोध को निष्पादित करता है, तो प्रतिक्रिया और उन्हें स्टोर भावी उपयोग के लिए किसी भी कुकीज़ खींचती है।

अनुरोध टेम्पलेट के लिए इंटरसेप्टर जोड़ें:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClientHelper.createDefaultHttpClient(GET_SERVICE_URL))); 
ClientHttpRequestInterceptor[] interceptors = {new MyClientHttpRequestInterceptor()}; 
restTemplate.setInterceptors(interceptors); 

और वहाँ तुम जाओ! मैंने इसका परीक्षण किया है और यह काम करता है। यह आपको स्प्रिंग एंड्रॉइड 1.0.0.आरसी 1 तक पकड़ लेगा जब हम सीधे HttpContext को RestTemplate के साथ उपयोग कर सकते हैं।

उम्मीद है कि यह दूसरों की मदद करता है !!

संबंधित मुद्दे