2018-12-26 14:42:05 +08:00
|
|
|
|
package com.ljsd.controller;
|
|
|
|
|
|
|
2021-09-02 18:31:23 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import com.ljsd.pojo.VerifyParams;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
import com.ljsd.redis.RedisKey;
|
2021-06-25 16:03:02 +08:00
|
|
|
|
import com.ljsd.service.*;
|
2021-09-02 19:05:46 +08:00
|
|
|
|
import com.ljsd.util.AppConstans;
|
2021-06-08 03:17:45 +08:00
|
|
|
|
import com.ljsd.util.BaseGlobal;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
import com.mongodb.BasicDBObject;
|
|
|
|
|
|
import com.mongodb.DBObject;
|
2019-08-16 12:03:41 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2021-08-10 10:28:04 +08:00
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.io.PrintWriter;
|
2021-06-08 03:17:45 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.Random;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
|
|
|
|
|
public class GetUserController extends HttpServlet {
|
|
|
|
|
|
private final static String _COLLECTION_NAME = "user_info";
|
2019-08-16 12:03:41 +08:00
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(GetUserController.class);
|
2019-08-19 10:06:30 +08:00
|
|
|
|
|
2022-08-29 20:15:34 +08:00
|
|
|
|
public static Map<String, VerifyService> serviceMap = new HashMap<>();
|
2021-06-08 03:17:45 +08:00
|
|
|
|
public static void initHandler(){
|
|
|
|
|
|
//初始化所有需要的验证方式
|
|
|
|
|
|
serviceMap.put("MHT", new LdVerifyService("MHT"));
|
|
|
|
|
|
serviceMap.put("XP", new XPVerifyService("XP"));
|
2022-04-07 14:30:51 +08:00
|
|
|
|
serviceMap.put("CH",new CaohuaVerifyService("CH"));
|
2021-06-25 16:03:02 +08:00
|
|
|
|
serviceMap.put("QUICK", new QuickVerifyService("QUICK"));
|
2022-04-15 11:53:46 +08:00
|
|
|
|
serviceMap.put("QIANYOU", new QiYouVerifyService("huangjiayl&qypay"));
|
2021-07-15 14:56:43 +08:00
|
|
|
|
serviceMap.put("YX", new YxylVerifyService("YX"));
|
2021-07-21 18:31:02 +08:00
|
|
|
|
serviceMap.put("DY", new V3367VerifyService("DY"));
|
2021-12-01 17:19:08 +08:00
|
|
|
|
serviceMap.put("QUICK2", new QuickVerifyService2("QUICK2"));
|
2022-04-12 17:52:43 +08:00
|
|
|
|
serviceMap.put("QUICK3", new QuickVerifyService3("QUICK3"));
|
2022-03-02 14:04:34 +08:00
|
|
|
|
serviceMap.put("YOUGU", new YouGuVerifyService("YG"));
|
2022-12-28 14:25:01 +08:00
|
|
|
|
serviceMap.put("YGXQ", new YouGuVerifyService("YGXQ"));
|
2022-03-14 17:00:10 +08:00
|
|
|
|
serviceMap.put("DUOYOU", new DuoYouVerifyService("DUOYOU"));
|
2022-04-07 14:30:51 +08:00
|
|
|
|
serviceMap.put("AIWAN", new AiWanVerifyService("AIWAN"));
|
2022-05-10 11:03:02 +08:00
|
|
|
|
serviceMap.put("HF", new HFVerifyService("HF"));
|
2023-05-08 11:49:25 +08:00
|
|
|
|
// serviceMap.put("ANJIU", new AnJiuVerifyService("ANJIU"));
|
|
|
|
|
|
serviceMap.put("FENGTI", new FengTiVerifyService("FENGTI"));
|
2019-08-19 10:06:30 +08:00
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
2021-06-08 03:17:45 +08:00
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
|
public GetUserController() {
|
|
|
|
|
|
super();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-07 14:30:51 +08:00
|
|
|
|
@Override
|
2018-12-26 14:42:05 +08:00
|
|
|
|
public void destroy() {
|
|
|
|
|
|
super.destroy();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 18:48:06 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* openId 账号id
|
|
|
|
|
|
* version
|
|
|
|
|
|
* serverId
|
|
|
|
|
|
* token
|
|
|
|
|
|
* platform android、ios 3 跳过验证
|
|
|
|
|
|
* admin
|
|
|
|
|
|
* gid pid
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*---verty--
|
|
|
|
|
|
* openid 和token校验
|
|
|
|
|
|
*
|
|
|
|
|
|
* ---return--
|
|
|
|
|
|
*
|
|
|
|
|
|
* uid(openid + serverid + platid)
|
|
|
|
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2022-04-07 14:30:51 +08:00
|
|
|
|
@Override
|
2021-10-25 18:01:04 +08:00
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2021-05-23 00:01:22 +08:00
|
|
|
|
DBObject res = new BasicDBObject();
|
2018-12-26 14:42:05 +08:00
|
|
|
|
String openId = request.getParameter("openId");
|
2018-12-27 13:31:34 +08:00
|
|
|
|
if (openId == null || openId.isEmpty()) {
|
2022-03-14 17:00:10 +08:00
|
|
|
|
returnErrorToFront(res,response, "openId is empty");
|
2018-12-27 13:31:34 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-11-12 17:28:37 +08:00
|
|
|
|
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) {
|
2021-05-23 00:01:22 +08:00
|
|
|
|
returnErrorToFront(res,response,banInfo);
|
|
|
|
|
|
LOGGER.info("doGet can not login Ban_Open_Id=>{} openId=>{} banInfo==null ? {}", RedisKey.Ban_Open_Id, openId, banInfo);
|
2020-11-12 17:28:37 +08:00
|
|
|
|
return;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
BaseGlobal.getInstance().redisApp.del(RedisKey.Ban_Open_Id, openId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
2020-04-26 17:22:52 +08:00
|
|
|
|
String version = request.getParameter("version");
|
|
|
|
|
|
if (version == null) {
|
|
|
|
|
|
version="未知版本号";
|
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
|
String serverId = request.getParameter("serverId");
|
2018-12-27 13:31:34 +08:00
|
|
|
|
if (serverId == null || serverId.isEmpty()) {
|
2021-05-23 00:01:22 +08:00
|
|
|
|
returnErrorToFront(res,response,"serverId is empty");
|
|
|
|
|
|
|
2018-12-27 13:31:34 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-07-01 11:27:00 +08:00
|
|
|
|
String token = request.getParameter("token");
|
2020-11-12 17:28:37 +08:00
|
|
|
|
if (token == null || token.isEmpty()) {
|
2021-05-23 00:01:22 +08:00
|
|
|
|
returnErrorToFront(res,response,"token is empty");
|
2019-07-01 11:27:00 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-04-07 14:30:51 +08:00
|
|
|
|
//平台类型
|
|
|
|
|
|
String platform = request.getParameter("platform");
|
2019-07-01 11:27:00 +08:00
|
|
|
|
if (platform == null || platform.isEmpty()) {
|
2021-05-23 00:01:22 +08:00
|
|
|
|
returnErrorToFront(res,response,"platform is empty");
|
2019-07-01 11:27:00 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-04-07 14:30:51 +08:00
|
|
|
|
//平台类型
|
|
|
|
|
|
String admin = request.getParameter("admin");
|
|
|
|
|
|
//gid
|
|
|
|
|
|
String gid = request.getParameter("gid");
|
|
|
|
|
|
//pid
|
|
|
|
|
|
String pid = request.getParameter("pid");
|
2021-06-08 03:17:45 +08:00
|
|
|
|
String channel = request.getParameter("channel");
|
2021-09-02 19:18:50 +08:00
|
|
|
|
String sub_channel = request.getParameter("sub_channel");
|
2019-09-02 15:13:41 +08:00
|
|
|
|
/*if (StringUtils.checkIsEmpty(admin)) {
|
|
|
|
|
|
response.sendError(400, "platform is empety");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}*/
|
2021-09-02 19:18:50 +08:00
|
|
|
|
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",openId,token,platform,admin,channel,sub_channel);
|
2021-06-07 18:15:55 +08:00
|
|
|
|
if(serviceMap == null){
|
|
|
|
|
|
LOGGER.info("serviceMap is null");
|
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
|
try {
|
2021-06-08 03:17:45 +08:00
|
|
|
|
//根据前端发过来的种类进行校验
|
2021-07-23 21:27:07 +08:00
|
|
|
|
if(channel==null||channel.equals("")){
|
|
|
|
|
|
channel="MHT";
|
|
|
|
|
|
}
|
2021-09-02 18:31:23 +08:00
|
|
|
|
boolean verify = false;
|
2021-09-02 19:05:46 +08:00
|
|
|
|
if (!AppConstans.appsecret.equals(admin) && "MHT".equals(channel)
|
|
|
|
|
|
&& !"3".equals(platform) && !"1000".equals(sub_channel) && !"20201222".equals(sub_channel)) {
|
2021-09-02 18:31:23 +08:00
|
|
|
|
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){
|
2021-09-02 18:53:58 +08:00
|
|
|
|
String msg = jsonObject.getString("message");
|
|
|
|
|
|
returnErrorToFront(res, response, msg);
|
|
|
|
|
|
return;
|
2021-09-02 18:31:23 +08:00
|
|
|
|
}
|
2021-09-02 19:18:50 +08:00
|
|
|
|
verify = true;
|
2021-09-02 18:31:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2021-11-16 14:33:13 +08:00
|
|
|
|
// 内网或者 游心 开天,不需要验证
|
|
|
|
|
|
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
2021-10-25 18:34:38 +08:00
|
|
|
|
verify = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
verify = serviceMap.get(channel).verify(response, request, admin, platform, pid, openId, token);
|
|
|
|
|
|
}
|
2021-09-02 18:31:23 +08:00
|
|
|
|
}
|
2021-06-08 03:17:45 +08:00
|
|
|
|
if(!verify){
|
2021-05-23 00:01:22 +08:00
|
|
|
|
returnErrorToFront(res,response,"校验失败,请重新登录");
|
2020-11-10 19:34:50 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-08-19 10:06:30 +08:00
|
|
|
|
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);
|
2019-10-09 19:55:23 +08:00
|
|
|
|
|
2021-04-12 11:08:17 +08:00
|
|
|
|
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;
|
2021-06-22 17:52:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
int uid = 0;
|
2021-08-09 14:14:07 +08:00
|
|
|
|
//加分布式锁
|
2021-06-22 17:52:13 +08:00
|
|
|
|
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
2021-08-10 10:28:04 +08:00
|
|
|
|
if (userInfos.size() == 0) {
|
|
|
|
|
|
try{
|
2021-08-09 14:14:07 +08:00
|
|
|
|
boolean getLock=false;
|
2021-08-10 10:28:04 +08:00
|
|
|
|
while(!getLock){
|
|
|
|
|
|
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
|
|
|
|
|
if(getLock){
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2021-08-09 14:14:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-10 10:28:04 +08:00
|
|
|
|
}finally {
|
|
|
|
|
|
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK,"","get");
|
2019-11-29 15:05:48 +08:00
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
Random random = new Random();
|
2019-07-01 11:27:00 +08:00
|
|
|
|
int utoken = Math.abs(random.nextInt());
|
2018-12-26 14:42:05 +08:00
|
|
|
|
uid = (int) userInfos.get(0).get("uid");
|
|
|
|
|
|
res.put("uid", uid);
|
2019-07-01 11:27:00 +08:00
|
|
|
|
res.put("token", utoken);
|
2020-11-13 14:32:56 +08:00
|
|
|
|
res.put("errorCode", 0);
|
2019-09-09 13:15:23 +08:00
|
|
|
|
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
|
2019-11-29 15:35:10 +08:00
|
|
|
|
BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid+"#"+gid, -1, false);
|
2018-12-26 14:42:05 +08:00
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
|
out.print(res);
|
|
|
|
|
|
out.flush();
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 00:01:22 +08:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-12 14:33:18 +08:00
|
|
|
|
|
2022-04-07 14:30:51 +08:00
|
|
|
|
@Override
|
2018-12-26 14:42:05 +08:00
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
this.doGet(request, response);
|
2021-06-08 18:07:48 +08:00
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|