generated from root/miduo_server
237 lines
8.5 KiB
Java
237 lines
8.5 KiB
Java
package com.ljsd.controller;
|
||
|
||
import com.ljsd.redis.RedisKey;
|
||
import com.ljsd.service.*;
|
||
import com.ljsd.util.BaseGlobal;
|
||
import com.ljsd.util.MD5Util;
|
||
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.*;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Random;
|
||
|
||
public class GetUserController extends HttpServlet {
|
||
private final static String _COLLECTION_NAME = "user_info";
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GetUserController.class);
|
||
|
||
private static Map<String, VerifyService> serviceMap = new HashMap<>();
|
||
public static void initHandler(){
|
||
//初始化所有需要的验证方式
|
||
serviceMap.put("MHT", new LdVerifyService("MHT"));
|
||
serviceMap.put("XP", new XPVerifyService("XP"));
|
||
serviceMap.put("CH",new CaohuaVerifyService("CH"));//草花
|
||
serviceMap.put("QUICK", new QuickVerifyService("QUICK"));
|
||
}
|
||
|
||
|
||
public GetUserController() {
|
||
super();
|
||
}
|
||
|
||
public void destroy() {
|
||
super.destroy();
|
||
}
|
||
|
||
|
||
/**
|
||
* openId 账号id
|
||
* version
|
||
* serverId
|
||
* token
|
||
* platform android、ios 3 跳过验证
|
||
* admin
|
||
* gid pid
|
||
*
|
||
*
|
||
*---verty--
|
||
* openid 和token校验
|
||
*
|
||
* ---return--
|
||
*
|
||
* uid(openid + serverid + platid)
|
||
|
||
*
|
||
*/
|
||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||
throws ServletException, 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 version = request.getParameter("version");
|
||
if (version == null) {
|
||
version="未知版本号";
|
||
}
|
||
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 admin = request.getParameter("admin"); //平台类型
|
||
|
||
String gid = request.getParameter("gid"); //gid
|
||
String pid = request.getParameter("pid"); //pid
|
||
String channel = request.getParameter("channel");
|
||
|
||
/*if (StringUtils.checkIsEmpty(admin)) {
|
||
response.sendError(400, "platform is empety");
|
||
return;
|
||
}*/
|
||
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{}",openId,token,platform,admin,channel);
|
||
if(serviceMap == null){
|
||
LOGGER.info("serviceMap is null");
|
||
}
|
||
try {
|
||
//根据前端发过来的种类进行校验
|
||
if(channel==null||channel.equals("")){
|
||
channel="MHT";
|
||
}
|
||
boolean verify = serviceMap.get(channel).verify(response, request, admin, platform, pid, openId, token);
|
||
|
||
if(!verify){
|
||
returnErrorToFront(res,response,"校验失败,请重新登录");
|
||
return;
|
||
}
|
||
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) {
|
||
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);
|
||
}
|
||
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);
|
||
|
||
}
|
||
|
||
// public static boolean loginVerfifyShangwu(String channelName,String openId,String token,String appId){
|
||
// try{
|
||
// String loginUrl = MHTSDKConstans.loginVerifyShangwu;
|
||
// Map<String, String> params = new HashMap<>();
|
||
//
|
||
// params.put("userToken", token);
|
||
// params.put("appId", appId);
|
||
// params.put("channelName", channelName);
|
||
// params.put("uid", openId);
|
||
//// loginUrl+"?"+"sid="+params.get("sid")+"&appid="+
|
||
// String loginResult = HttpUtils.doPost(loginUrl,params);
|
||
// if(loginResult == null || loginResult.isEmpty()){
|
||
// return false;
|
||
// }
|
||
// return parseLoginResult(loginResult);
|
||
// }catch(Exception e){
|
||
// return false;
|
||
// }
|
||
//
|
||
// }
|
||
|
||
|
||
public static void main(String[] args) {
|
||
String paramSign = "d53b3e8ef74bf72d8aafce3a1c8671a0" ;
|
||
String secretKey = "";
|
||
String timeStamp = "0";
|
||
String openId = "1238";
|
||
String verifyStr = openId+"&"+timeStamp+"&"+secretKey;
|
||
String s = MD5Util.encrypByMd5(verifyStr);
|
||
|
||
}
|
||
|
||
}
|