登录验证(还不行)

This commit is contained in:
kehaoyuan
2017-08-01 16:43:59 +08:00
parent a15ab62f03
commit 77b660576a
6 changed files with 109 additions and 16 deletions

View File

@ -16,7 +16,6 @@ import com.gh.common.util.DataUtils;
import com.gh.common.util.StringUtils;
import com.gh.common.util.TokenUtils;
import com.gh.gamecenter.BuildConfig;
import com.gh.gamecenter.statistics.AppTrafficUtils;
import com.leon.channel.helper.ChannelReaderUtil;
import com.lightgame.config.CommonDebug;
import com.lightgame.utils.Utils;
@ -216,7 +215,7 @@ public class AppController extends MultiDexApplication {
// 启用EventBus3.0加速功能
EventBus.builder().addIndex(new EventBusIndex()).installDefaultEventBus();
AppTrafficUtils.appTraffic(this); //流量统计
// AppTrafficUtils.appTraffic(this); //流量统计
}
private boolean shouldInit() {

View File

@ -35,6 +35,9 @@ public class Config {
public static final String UMENG_APPKEY = BuildConfig.UMENG_APPKEY;
public static final String UMENG_MESSAGE_SECRET = BuildConfig.UMENG_MESSAGE_SECRET;
public static final String APP_SECRET = "597e9c59dcd026000d782101"; // 登录验证
public static final String APP_ID = "2201146978"; // 登录验证
public static boolean isShow(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getBoolean("isShow", true);

View File

@ -0,0 +1,36 @@
package com.gh.common.util;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HMACUtils {
public static String encrypt(String data, String key) {
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
return byte2hex(mac.doFinal(data.getBytes()));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
private static String byte2hex(byte[] ciphertext) {
StringBuilder builder = new StringBuilder();
String stmp;
for (int i = 0; ciphertext != null && i < ciphertext.length; i++) {
stmp = Integer.toHexString(ciphertext[i] & 0XFF);
if (stmp.length() == 1) {
builder.append('0');
}
builder.append(stmp);
}
return builder.toString().toLowerCase();
}
}

View File

@ -143,6 +143,7 @@ public class LoginUtils {
ResponseBody responseBody = e.response().errorBody();
try {
String string = responseBody.string();
Utils.log("=======" + string);
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
switch (code) {

View File

@ -2,18 +2,29 @@ package com.gh.gamecenter.retrofit;
import android.content.Context;
import com.gh.common.constant.Config;
import com.gh.common.util.GzipUtils;
import com.gh.common.util.HMACUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.StringUtils;
import com.gh.common.util.TimestampUtils;
import com.lightgame.utils.Utils;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
/**
* Created by LGT on 2016/11/8.
@ -52,19 +63,62 @@ class OkHttpCacheInterceptor implements Interceptor {
// Utils.log(String.format("Interceptor Sending request %s on %s%n%s",
// request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
Response response;
if (request.url().toString().contains(Config.USERSEA_HOST)) {
// 去除timestamp拿缓存
url = response.request().url().toString();
if (response.code() == 504 && url.contains("timestamp")) {
byte[] data = OkHttpCache.getCache(mContext, TimestampUtils.removeTimestamp(url));
if (data != null) {
data = GzipUtils.decompressBytes(data);
response = response.newBuilder()
.code(200)
.message("OK")
.body(ResponseBody.create(MediaType.parse("application/json"), data))
.build();
if (url.contains("?")) {
url += "&app_id=" + Config.APP_ID;
} else {
url += "?app_id=" + Config.APP_ID;
}
String body = "";
if (request.body() != null && request.body().contentLength() > 0) {
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
body = buffer.readUtf8();
}
Set<String> paramsSet = HttpUrl.parse(url).queryParameterNames();
StringBuilder params = new StringBuilder();
params.append("#");
if (paramsSet.size() != 0) {
List<String> paramsList = new ArrayList<>();
paramsList.addAll(paramsSet);
Collections.sort(paramsList);
for (String s : paramsList) {
params.append(s);
params.append("=");
params.append(HttpUrl.parse(url).queryParameter(s));
}
}
String str = StringUtils.buildString(request.method(), "#", url.split("//")[1].split("\\?")[0], params.toString(), "#", body);
String encode = URLEncoder.encode(str);
String signature = HMACUtils.encrypt(encode, "&" + Config.APP_SECRET + "#");
request = request.newBuilder()
.addHeader("Authorization", "SIGNATURE-V2 " + signature)
.url(url)
.build();
response = chain.proceed(request);
Utils.log("Authorization::" + str + "\n" + url + "\n" + signature + "\n" + encode + "\n" + Config.APP_SECRET);
} else {
response = chain.proceed(request);
// 去除timestamp拿缓存
url = response.request().url().toString();
if (response.code() == 504 && url.contains("timestamp")) {
byte[] data = OkHttpCache.getCache(mContext, TimestampUtils.removeTimestamp(url));
if (data != null) {
data = GzipUtils.decompressBytes(data);
response = response.newBuilder()
.code(200)
.message("OK")
.body(ResponseBody.create(MediaType.parse("application/json"), data))
.build();
}
}
}