generated from root/miduo_server
代码整理
parent
8325f4b0c2
commit
1d6605e8cf
|
|
@ -71,16 +71,10 @@ public class GetUserController extends HttpServlet {
|
|||
* platform android、ios 3 跳过验证
|
||||
* admin
|
||||
* gid pid
|
||||
*
|
||||
*
|
||||
*---verty--
|
||||
* openid 和token校验
|
||||
*
|
||||
* ---return--
|
||||
*
|
||||
* uid(openid + serverid + platid)
|
||||
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
|
@ -104,16 +98,11 @@ public class GetUserController extends HttpServlet {
|
|||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String version = request.getParameter("version");
|
||||
if (version == null) {
|
||||
version="未知版本号";
|
||||
LOGGER.error("登陆报错:{}",e.getMessage(),e);
|
||||
}
|
||||
String serverId = request.getParameter("serverId");
|
||||
if (serverId == null || serverId.isEmpty()) {
|
||||
returnErrorToFront(res,response,"serverId is empty");
|
||||
|
||||
return;
|
||||
}
|
||||
String token = request.getParameter("token");
|
||||
|
|
@ -135,45 +124,21 @@ public class GetUserController extends HttpServlet {
|
|||
String pid = request.getParameter("pid");
|
||||
String channel = request.getParameter("channel");
|
||||
String sub_channel = request.getParameter("sub_channel");
|
||||
/*if (StringUtils.checkIsEmpty(admin)) {
|
||||
response.sendError(400, "platform is empety");
|
||||
return;
|
||||
}*/
|
||||
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",openId,token,platform,admin,channel,sub_channel);
|
||||
if(serviceMap == null){
|
||||
LOGGER.info("serviceMap is null");
|
||||
}
|
||||
try {
|
||||
//根据前端发过来的种类进行校验
|
||||
if(channel==null||channel.equals("")){
|
||||
if(channel==null|| channel.isEmpty()){
|
||||
channel="MHT";
|
||||
}
|
||||
boolean verify = false;
|
||||
if (!AppConstans.appsecret.equals(admin) && "MHT".equals(channel)
|
||||
&& !"3".equals(platform) && !"1000".equals(sub_channel) && !"20201222".equals(sub_channel)) {
|
||||
LdVerifyServiceStr instance = LdVerifyServiceStr.getInstance();
|
||||
VerifyParams params = new VerifyParams();
|
||||
params.setAppId(instance.properties.getProperty("appId_online"));
|
||||
params.setSecretKey(instance.properties.getProperty("secretKey_online"));
|
||||
VerifyParams formatParam = instance.getFormatParam(params,request, openId, token);
|
||||
String result = instance.verifyFormat(formatParam);
|
||||
if (result != null && !result.isEmpty()) {
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
int state = jsonObject.getIntValue("code");
|
||||
if(state != 200){
|
||||
String msg = jsonObject.getString("message");
|
||||
returnErrorToFront(res, response, msg);
|
||||
return;
|
||||
}
|
||||
verify = true;
|
||||
}
|
||||
boolean verify;
|
||||
// 内网或者 游心 开天,不需要验证
|
||||
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
||||
verify = true;
|
||||
} else {
|
||||
// 内网或者 游心 开天,不需要验证
|
||||
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
||||
verify = true;
|
||||
} else {
|
||||
verify = serviceMap.get(channel).verify(response, request, admin, platform, pid, openId, token);
|
||||
}
|
||||
verify = serviceMap.get(channel).verify(response, request, admin, platform, pid, openId, token);
|
||||
}
|
||||
if(!verify){
|
||||
returnErrorToFront(res,response,"校验失败,请重新登录");
|
||||
|
|
@ -189,14 +154,14 @@ public class GetUserController extends HttpServlet {
|
|||
DBObject serverDBObject = new BasicDBObject();
|
||||
serverDBObject.put("server_id", serverId);
|
||||
List<DBObject> server_info = BaseGlobal.getInstance().mongoDBPool.find("server_info", serverDBObject);
|
||||
if(server_info.size()==0){
|
||||
if(server_info.isEmpty()){
|
||||
returnErrorToFront(res,response,"服务器信息获取错误");
|
||||
return;
|
||||
}
|
||||
int uid = 0;
|
||||
int uid;
|
||||
//加分布式锁
|
||||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
if (userInfos.size() == 0) {
|
||||
if (userInfos.isEmpty()) {
|
||||
try{
|
||||
boolean getLock=false;
|
||||
while(!getLock){
|
||||
|
|
@ -204,7 +169,7 @@ public class GetUserController extends HttpServlet {
|
|||
if(getLock){
|
||||
userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
//双重校验
|
||||
if (userInfos.size() == 0) {
|
||||
if (userInfos.isEmpty()) {
|
||||
DBObject next = server_info.iterator().next();
|
||||
Object register_state = next.get("register_state");
|
||||
if(register_state!=null&& register_state.equals("0")){
|
||||
|
|
|
|||
|
|
@ -1,164 +1,165 @@
|
|||
package com.ljsd.controller;
|
||||
|
||||
import com.ljsd.redis.RedisKey;
|
||||
import com.ljsd.util.BaseGlobal;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 好游戏SDK
|
||||
*/
|
||||
public class HaoGameGetUserController extends HttpServlet {
|
||||
private final static String _COLLECTION_NAME = "user_info";
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HaoGameGetUserController.class);
|
||||
|
||||
public HaoGameGetUserController() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 好游戏登录
|
||||
*/
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
DBObject res = new BasicDBObject();
|
||||
String openId = request.getParameter("openId");
|
||||
if (openId == null || openId.isEmpty()) {
|
||||
returnErrorToFront(res, response, "openId is empty");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String banInfo = BaseGlobal.getInstance().redisApp.get(RedisKey.Ban_Open_Id, openId, String.class, -1);
|
||||
if (banInfo != null) {
|
||||
String[] split = banInfo.split("\\|");
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (Long.parseLong(split[0]) > currentTimeMillis) {
|
||||
returnErrorToFront(res, response, banInfo);
|
||||
LOGGER.info("doGet can not login Ban_Open_Id=>{} openId=>{} banInfo==null ? {}", RedisKey.Ban_Open_Id, openId, banInfo);
|
||||
return;
|
||||
} else {
|
||||
BaseGlobal.getInstance().redisApp.del(RedisKey.Ban_Open_Id, openId);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String serverId = request.getParameter("serverId");
|
||||
if (serverId == null || serverId.isEmpty()) {
|
||||
returnErrorToFront(res, response, "serverId is empty");
|
||||
return;
|
||||
}
|
||||
String token = request.getParameter("token");
|
||||
if (token == null || token.isEmpty()) {
|
||||
returnErrorToFront(res, response, "token is empty");
|
||||
return;
|
||||
}
|
||||
String platform = request.getParameter("platform"); //平台类型
|
||||
if (platform == null || platform.isEmpty()) {
|
||||
returnErrorToFront(res, response, "platform is empty");
|
||||
return;
|
||||
}
|
||||
String gid = request.getParameter("gid"); //gid
|
||||
String pid = request.getParameter("pid"); //pid
|
||||
|
||||
|
||||
LOGGER.info("the opendId = {},token={},platform={}", openId, token, platform);
|
||||
try {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
DBObject dbObject = new BasicDBObject();
|
||||
dbObject.put("openId", openId);
|
||||
dbObject.put("serverId", serverId);
|
||||
dbObject.put("platform", platform);
|
||||
DBObject serverDBObject = new BasicDBObject();
|
||||
serverDBObject.put("server_id", serverId);
|
||||
List<DBObject> server_info = BaseGlobal.getInstance().mongoDBPool.find("server_info", serverDBObject);
|
||||
if (server_info.size() == 0) {
|
||||
returnErrorToFront(res, response, "服务器信息获取错误");
|
||||
return;
|
||||
}
|
||||
int uid = 0;
|
||||
//加分布式锁
|
||||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
if (userInfos.size() == 0) {
|
||||
try {
|
||||
boolean getLock = false;
|
||||
while (!getLock) {
|
||||
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||||
if (!getLock) {
|
||||
continue;
|
||||
}
|
||||
userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
//双重校验
|
||||
if (userInfos.size() == 0) {
|
||||
DBObject next = server_info.iterator().next();
|
||||
Object register_state = next.get("register_state");
|
||||
if (register_state != null && register_state.equals("0")) {
|
||||
returnErrorToFront(res, response, "该服务器已停止注册,请选择其他服务器游戏");
|
||||
return;
|
||||
}
|
||||
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
||||
dbObject.put("uid", uid);
|
||||
dbObject.put("_id", uid);
|
||||
userInfos.add(dbObject);
|
||||
if (gid != null && !gid.isEmpty()) {
|
||||
dbObject.put("gid", gid);
|
||||
}
|
||||
if (pid != null && !pid.isEmpty()) {
|
||||
dbObject.put("pid", pid);
|
||||
}
|
||||
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK, "", "get");
|
||||
}
|
||||
}
|
||||
Random random = new Random();
|
||||
int utoken = Math.abs(random.nextInt());
|
||||
uid = (int) userInfos.get(0).get("uid");
|
||||
res.put("uid", uid);
|
||||
res.put("token", utoken);
|
||||
res.put("errorCode", 0);
|
||||
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
|
||||
BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid + "#" + gid, -1, false);
|
||||
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(res);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void returnErrorToFront(DBObject res, HttpServletResponse response, String errorReason) throws IOException {
|
||||
res.put("errorCode", -1);
|
||||
res.put("reason", errorReason);
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(res);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
this.doGet(request, response);
|
||||
|
||||
}
|
||||
}
|
||||
//package com.ljsd.controller;
|
||||
//
|
||||
//import com.ljsd.redis.RedisKey;
|
||||
//import com.ljsd.util.BaseGlobal;
|
||||
//import com.mongodb.BasicDBObject;
|
||||
//import com.mongodb.DBObject;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//
|
||||
//import javax.servlet.ServletException;
|
||||
//import javax.servlet.http.HttpServlet;
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import javax.servlet.http.HttpServletResponse;
|
||||
//import java.io.IOException;
|
||||
//import java.io.PrintWriter;
|
||||
//import java.util.List;
|
||||
//import java.util.Random;
|
||||
//
|
||||
///**
|
||||
// * 好游戏SDK
|
||||
// */
|
||||
//@Deprecated
|
||||
//public class HaoGameGetUserController extends HttpServlet {
|
||||
// private final static String _COLLECTION_NAME = "user_info";
|
||||
// private static final Logger LOGGER = LoggerFactory.getLogger(HaoGameGetUserController.class);
|
||||
//
|
||||
// public HaoGameGetUserController() {
|
||||
// super();
|
||||
// }
|
||||
//
|
||||
// public void destroy() {
|
||||
// super.destroy();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 好游戏登录
|
||||
// */
|
||||
// public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
// DBObject res = new BasicDBObject();
|
||||
// String openId = request.getParameter("openId");
|
||||
// if (openId == null || openId.isEmpty()) {
|
||||
// returnErrorToFront(res, response, "openId is empty");
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// String banInfo = BaseGlobal.getInstance().redisApp.get(RedisKey.Ban_Open_Id, openId, String.class, -1);
|
||||
// if (banInfo != null) {
|
||||
// String[] split = banInfo.split("\\|");
|
||||
// long currentTimeMillis = System.currentTimeMillis();
|
||||
// if (Long.parseLong(split[0]) > currentTimeMillis) {
|
||||
// returnErrorToFront(res, response, banInfo);
|
||||
// LOGGER.info("doGet can not login Ban_Open_Id=>{} openId=>{} banInfo==null ? {}", RedisKey.Ban_Open_Id, openId, banInfo);
|
||||
// return;
|
||||
// } else {
|
||||
// BaseGlobal.getInstance().redisApp.del(RedisKey.Ban_Open_Id, openId);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// String serverId = request.getParameter("serverId");
|
||||
// if (serverId == null || serverId.isEmpty()) {
|
||||
// returnErrorToFront(res, response, "serverId is empty");
|
||||
// return;
|
||||
// }
|
||||
// String token = request.getParameter("token");
|
||||
// if (token == null || token.isEmpty()) {
|
||||
// returnErrorToFront(res, response, "token is empty");
|
||||
// return;
|
||||
// }
|
||||
// String platform = request.getParameter("platform"); //平台类型
|
||||
// if (platform == null || platform.isEmpty()) {
|
||||
// returnErrorToFront(res, response, "platform is empty");
|
||||
// return;
|
||||
// }
|
||||
// String gid = request.getParameter("gid"); //gid
|
||||
// String pid = request.getParameter("pid"); //pid
|
||||
//
|
||||
//
|
||||
// LOGGER.info("the opendId = {},token={},platform={}", openId, token, platform);
|
||||
// try {
|
||||
// response.setCharacterEncoding("UTF-8");
|
||||
// response.setContentType("application/json; charset=utf-8");
|
||||
// DBObject dbObject = new BasicDBObject();
|
||||
// dbObject.put("openId", openId);
|
||||
// dbObject.put("serverId", serverId);
|
||||
// dbObject.put("platform", platform);
|
||||
// DBObject serverDBObject = new BasicDBObject();
|
||||
// serverDBObject.put("server_id", serverId);
|
||||
// List<DBObject> server_info = BaseGlobal.getInstance().mongoDBPool.find("server_info", serverDBObject);
|
||||
// if (server_info.size() == 0) {
|
||||
// returnErrorToFront(res, response, "服务器信息获取错误");
|
||||
// return;
|
||||
// }
|
||||
// int uid = 0;
|
||||
// //加分布式锁
|
||||
// List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
// if (userInfos.size() == 0) {
|
||||
// try {
|
||||
// boolean getLock = false;
|
||||
// while (!getLock) {
|
||||
// getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||||
// if (!getLock) {
|
||||
// continue;
|
||||
// }
|
||||
// userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
// //双重校验
|
||||
// if (userInfos.size() == 0) {
|
||||
// DBObject next = server_info.iterator().next();
|
||||
// Object register_state = next.get("register_state");
|
||||
// if (register_state != null && register_state.equals("0")) {
|
||||
// returnErrorToFront(res, response, "该服务器已停止注册,请选择其他服务器游戏");
|
||||
// return;
|
||||
// }
|
||||
// uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
||||
// dbObject.put("uid", uid);
|
||||
// dbObject.put("_id", uid);
|
||||
// userInfos.add(dbObject);
|
||||
// if (gid != null && !gid.isEmpty()) {
|
||||
// dbObject.put("gid", gid);
|
||||
// }
|
||||
// if (pid != null && !pid.isEmpty()) {
|
||||
// dbObject.put("pid", pid);
|
||||
// }
|
||||
// BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||||
// }
|
||||
// }
|
||||
// } finally {
|
||||
// BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK, "", "get");
|
||||
// }
|
||||
// }
|
||||
// Random random = new Random();
|
||||
// int utoken = Math.abs(random.nextInt());
|
||||
// uid = (int) userInfos.get(0).get("uid");
|
||||
// res.put("uid", uid);
|
||||
// res.put("token", utoken);
|
||||
// res.put("errorCode", 0);
|
||||
// BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
|
||||
// BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid + "#" + gid, -1, false);
|
||||
//
|
||||
// PrintWriter out = response.getWriter();
|
||||
// out.print(res);
|
||||
// out.flush();
|
||||
// out.close();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void returnErrorToFront(DBObject res, HttpServletResponse response, String errorReason) throws IOException {
|
||||
// res.put("errorCode", -1);
|
||||
// res.put("reason", errorReason);
|
||||
// PrintWriter out = response.getWriter();
|
||||
// out.print(res);
|
||||
// out.flush();
|
||||
// out.close();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
// this.doGet(request, response);
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
|
|
|||
Loading…
Reference in New Issue