Cần giúp đỡ về bảo vệ dữ liệu trên api


Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

Mình thấy để truy cập vào 1 api sử dụng http get  thì nó sẽ trả về 1 đoạn mã html ( vd : http://xemphim.somee.com/Json/Thread )

bây giờ mình muốn chặn người khác truy cập vào api trên . Nhưng lại muốn từ những chiếc smartphone có thể truy cập và lấy thông tin bình thường thì giải pháp là ntn . các bạn giúp mình với .

Mình không muốn sử dụng querystring dạng abc.com/user=xyz&pass=qwe để lấy dữ liệu nhé . vì như vậy sẽ bị sniff dễ dàng 

Help meeeeeeeeeeeeeeeeeeeeeeeeeee

Label
Từ: 15:48 01/06/2012
Bài: 125
Cảm ơn: 147
Thích: 25

Module server thì encrypt dữ liệu trước khi trả dữ liệu bằng 1 thuật toán nào đó

Module client thì decrypt rồi mới sử dụng

Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

em nghi Cách thứ 2 là sử dụng HTTPPost , user sẽ tiến hành post string lên sv để verify

/**
* HTTP Post , chuyển 1 chuỗi string lên server
* @param url
*            : đường dẫn
* @param jsonData
*            : chuỗi string
* @param callback
*            : callback
*/
public static void postJsonDataToUrl(final String url,
final String jsonData, final IOnTransportCallbackData callback,
long timeOut, final Handler handler) {
final AsyncTask<Void, Void, String> asyn = new AsyncTask<Void, Void, String>() {
 
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonData, HTTP.UTF_8);
httppost.setEntity(entity);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entityResponse = response.getEntity();
if (entity != null) {
InputStream instream = entityResponse.getContent();
 
String result = convertStreamToString(instream);
return result;
}
} catch (Exception ex) {
NTDLogUtils.e(ex.getMessage());
}
return null;
}
 
@Override
protected void onPostExecute(String result) {
if (callback != null)
callback.onResult(result);
handler.removeCallbacksAndMessages(null);
}
};
asyn.execute();
handler.postDelayed(new Runnable() {
 
@Override
public void run() {
asyn.cancel(true);
handler.removeCallbacksAndMessages(null);
callback.onResult(null);
}
}, timeOut);
}
 
public static interface IOnTransportCallbackData {
public void onResult(String object);
}
 
server kiểm tra json hợp lệ sẽ response lại html
Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

Cách thứ 3 là post theo param 

/**

* HTTPPost
* @param url
*            : đường dẫn
* @param nameValuePairs
*            : key,value
* @param callback
*            : callback
*/
public static void postDataToURL(final String url,
final ArrayList<NameValuePair> nameValuePairs,
final IOnTransportCallbackData callback, long timeOut,
final Handler handler) {
final AsyncTask<Void, Void, String> asyn = new AsyncTask<Void, Void, String>() {
 
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
 
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
 
String result = convertStreamToString(instream);
return result;
}
} catch (Exception ex) {
NTDLogUtils.e(ex.getMessage());
}
return null;
}
 
@Override
protected void onPostExecute(String result) {
handler.removeCallbacksAndMessages(null);
if (callback != null)
callback.onResult(result);
}
 
}.execute();
handler.postDelayed(new Runnable() {
 
@Override
public void run() {
asyn.cancel(true);
handler.removeCallbacksAndMessages(null);
callback.onResult(null);
}
}, timeOut);
}
 
public static interface IOnTransportCallbackData {
public void onResult(String object);
}
 
Server nhận dữ liệu theo param và response lại html
Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

Cách 1 Sử dụng encrypt và decrypt với http get

public static void getSourceFromUrl(final String url,

final IOnTransportCallbackData callback, long timeOut,
final Handler handler) {
final AsyncTask<Void, Void, String> asyn = new AsyncTask<Void, Void, String>() {
 
@Override
protected String doInBackground(Void... params) {
URL u;
try {
u = new URL(url);
 
BufferedReader in = new BufferedReader(
new InputStreamReader(u.openStream()));
String str;
String html = StringEmpty;
while ((str = in.readLine()) != null) {
html += str;
}
in.close();
return html;
} catch (Exception ex) {
NTDLogUtils.e(ex.getMessage());
}
return null;
}
 
@Override
protected void onPostExecute(String result) {
handler.removeCallbacksAndMessages(null);
if (callback != null)
callback.onResult(result);
}
 
}.execute();
handler.postDelayed(new Runnable() {
 
@Override
public void run() {
asyn.cancel(true);
handler.removeCallbacksAndMessages(null);
callback.onResult(null);
}
}, timeOut);
}
 
public static interface IOnTransportCallbackData {
public void onResult(String object);
}
 
decrypt  voi key ban lua chon , tuy` theo thuật toán sử dụng
 
Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

cách 4 : sử dụng soap để post username và password ( dùng ksoap2)

Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

Server sẽ pải config để chặn tất cả các request với http get 

và nếu cẩn thận hơn thì encrypt và decrypt với http post . 

Còn rất nhiều giải pháp cho vấn đề này , các bạn đóng góp nhé :D

Label
Từ: 10:32 14/06/2012
Bài: 112
Cảm ơn: 124
Thích: 18

Cách 5 : sử dụng websocket , mỗi tội là cực kỳ tốn pin :D