generated from root/miduo_server
接入登录验证
parent
d5b3253e8d
commit
a177fc157a
|
@ -22,4 +22,7 @@ dependencies {
|
|||
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'
|
||||
compile group: 'jcifs', name: 'jcifs', version: '1.3.17'
|
||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
|
||||
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
|
||||
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
package com.ljsd.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ljsd.pojo.SuJieAccount;
|
||||
import com.ljsd.redis.RedisKey;
|
||||
import com.ljsd.util.BaseGlobal;
|
||||
import com.ljsd.util.EncryptUtils;
|
||||
import com.ljsd.util.HttpUtils;
|
||||
import com.ljsd.util.KTSDKConstans;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
@ -11,7 +16,9 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class GetUserController extends HttpServlet {
|
||||
|
@ -38,9 +45,28 @@ public class GetUserController extends HttpServlet {
|
|||
response.sendError(400, "serverId is empety");
|
||||
return;
|
||||
}
|
||||
String token = request.getParameter("token");
|
||||
if (token == null || serverId.isEmpty()) {
|
||||
response.sendError(400, "token is empety");
|
||||
return;
|
||||
}
|
||||
String platform = request.getParameter("platform"); //平台类型
|
||||
if (platform == null || platform.isEmpty()) {
|
||||
response.sendError(400, "token is empety");
|
||||
return;
|
||||
}
|
||||
if(!"3".equals(platform)){
|
||||
SuJieAccount suJieAccount = loginVerfify(platform, openId, token);
|
||||
if(suJieAccount == null){
|
||||
response.sendError(400, "verify fail");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
DBObject dbObject = new BasicDBObject();
|
||||
dbObject.put("openId", openId);
|
||||
dbObject.put("serverId", serverId);
|
||||
dbObject.put("platform", platform);
|
||||
try {
|
||||
int uid = 0;
|
||||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
|
@ -53,10 +79,10 @@ public class GetUserController extends HttpServlet {
|
|||
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||||
}
|
||||
Random random = new Random();
|
||||
int token = Math.abs(random.nextInt());
|
||||
int utoken = Math.abs(random.nextInt());
|
||||
uid = (int) userInfos.get(0).get("uid");
|
||||
res.put("uid", uid);
|
||||
res.put("token", token);
|
||||
res.put("token", utoken);
|
||||
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), token, -1, false);
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(res);
|
||||
|
@ -65,8 +91,6 @@ public class GetUserController extends HttpServlet {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
|
@ -74,4 +98,50 @@ public class GetUserController extends HttpServlet {
|
|||
this.doGet(request, response);
|
||||
}
|
||||
|
||||
|
||||
public static SuJieAccount loginVerfify(String platForm,String openId,String token){
|
||||
try{
|
||||
String loginUrl = "";
|
||||
if("1".equals(platForm)){
|
||||
loginUrl = KTSDKConstans.androidVerify;
|
||||
}else if("2".equals(platForm)){
|
||||
loginUrl = KTSDKConstans.iosVerify;
|
||||
}
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("userID", openId);
|
||||
params.put("token", token);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("userID=").append(openId)
|
||||
.append("token=").append(token)
|
||||
.append(KTSDKConstans.appid);
|
||||
|
||||
String sign = EncryptUtils.createMD5String(sb.toString());
|
||||
params.put("sign", sign);
|
||||
String loginResult = HttpUtils.doPost(loginUrl,params);
|
||||
if(loginResult == null || loginResult.isEmpty()){
|
||||
return null;
|
||||
}
|
||||
return parseLoginResult(loginResult);
|
||||
}catch(Exception e){
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static SuJieAccount parseLoginResult(String orderResult){
|
||||
try {
|
||||
SuJieAccount suJieAccount = (SuJieAccount)JSONObject.parse(orderResult);
|
||||
int state = suJieAccount.getState();
|
||||
if(state != 0){
|
||||
return null;
|
||||
}
|
||||
return suJieAccount;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ public class RegistController extends HttpServlet {
|
|||
String plat = request.getParameter("plat");
|
||||
String state = request.getParameter("state");
|
||||
String open_time = request.getParameter("open_time");
|
||||
String token = request.getParameter("token");
|
||||
DBObject doc = new BasicDBObject();
|
||||
doc.put("_id", serverId);
|
||||
doc.put("name", name);
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.ljsd.pojo;
|
||||
|
||||
public class SuJieAccount {
|
||||
private String accountID;
|
||||
private String accountName;
|
||||
private int state;
|
||||
|
||||
public SuJieAccount() {
|
||||
}
|
||||
|
||||
public SuJieAccount(String accountID, String accountName){
|
||||
this.accountID = accountID;
|
||||
this.accountName = accountName;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public String getAccountID() {
|
||||
return accountID;
|
||||
}
|
||||
public void setAccountID(String accountID) {
|
||||
this.accountID = accountID;
|
||||
}
|
||||
public String getAccountName() {
|
||||
return accountName;
|
||||
}
|
||||
public void setAccountName(String accountName) {
|
||||
this.accountName = accountName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.ljsd.util;
|
||||
|
||||
import sun.security.provider.MD5;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class EncryptUtils {
|
||||
|
||||
/**
|
||||
* 将输入的字符串进行MD5加密(编码)
|
||||
*
|
||||
* @param inputString
|
||||
* @return
|
||||
*/
|
||||
public static String createMD5String(String inputString) {
|
||||
return encodeByMD5(inputString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对字符串进行MD5编码
|
||||
*
|
||||
* @param originStr
|
||||
* @return
|
||||
*/
|
||||
public static String encodeByMD5(String originStr) {
|
||||
if (originStr != null) {
|
||||
try {
|
||||
// 创建具有指定算法名称的信息摘要
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
// 使用指定的字节数组对摘要进行最后的更新,然后完成摘要计算
|
||||
char[] _charStr = originStr.toCharArray();
|
||||
byte[] _byteStr = new byte[_charStr.length];
|
||||
for (int i = 0; i < _charStr.length; i++) {
|
||||
_byteStr[i] = (byte)_charStr[i];
|
||||
}
|
||||
byte[] _results = md.digest(_byteStr);
|
||||
StringBuffer _hexValue = new StringBuffer();
|
||||
for (int i = 0; i < _results.length; i++) {
|
||||
int _val = ((int)_results[i]) & 0xff;
|
||||
if(_val < 16){
|
||||
_hexValue.append("0");
|
||||
}
|
||||
_hexValue.append(Integer.toHexString(_val));
|
||||
}
|
||||
return _hexValue.toString();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.ljsd.util;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
public static String doPost(String url, Map<String,String> parms) throws IOException {
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
List<NameValuePair> nvps =getFromMap(parms);
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
|
||||
CloseableHttpResponse response2 = httpclient.execute(httpPost);
|
||||
|
||||
try {
|
||||
HttpEntity entity = response2.getEntity();
|
||||
return EntityUtils.toString(entity);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
response2.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<NameValuePair> getFromMap(Map<String,String> parms){
|
||||
List<NameValuePair> nvps = new ArrayList<NameValuePair>(parms.size());
|
||||
for(Map.Entry<String,String> item : parms.entrySet()){
|
||||
nvps.add(new BasicNameValuePair(item.getKey(), item.getValue()));
|
||||
}
|
||||
return nvps;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.ljsd.util;
|
||||
|
||||
public class KTSDKConstans {
|
||||
public static String appid = "129213";
|
||||
public static String appkey = "c0fb59eb68559b6c9c1463e4d5d0c806";
|
||||
public static String appsecret = "d53b3e8ef74bf72d8aafce3a1c8671a0";
|
||||
|
||||
public static String androidVerify = "http://sujie.passport.ktsdk.com/user/verifyAccount";
|
||||
public static String iosVerify = "http://sujie.passport.ktsdk.com/userios/verifyAccount";
|
||||
|
||||
}
|
Loading…
Reference in New Issue