generated from root/miduo_server
331 lines
12 KiB
Java
331 lines
12 KiB
Java
package com.ljsd.controller;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.ljsd.redis.RedisKey;
|
||
import com.ljsd.redis.RedisUtil;
|
||
import com.ljsd.util.*;
|
||
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.*;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
public class GetUserController extends HttpServlet {
|
||
private final static String _COLLECTION_NAME = "user_info";
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GetUserController.class);
|
||
public static int isTestLan=0;
|
||
|
||
public static void initLanState(){
|
||
Properties properties = BaseGlobal.getInstance().properties;
|
||
isTestLan = Integer.parseInt(properties.getProperty("isTestLan"));
|
||
}
|
||
|
||
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
|
||
|
||
/*if (StringUtils.checkIsEmpty(admin)) {
|
||
response.sendError(400, "platform is empety");
|
||
return;
|
||
}*/
|
||
LOGGER.info("the opendId = {},token={},platform={} admin=>{}",openId,token,platform,admin);
|
||
try {
|
||
boolean vertify = vertify(response, request, admin, platform, pid, openId, token);
|
||
if(!vertify){
|
||
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;
|
||
}else{
|
||
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;
|
||
}
|
||
}
|
||
int uid = 0;
|
||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||
if (userInfos.size() == 0) {
|
||
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 boolean vertify(HttpServletResponse response,HttpServletRequest request,String admin,String platform,String pid,String openId,String token){
|
||
try {
|
||
if(!KTSDKConstans.appsecret.equals(admin)) {
|
||
if (isTestLan == 1) { //test
|
||
boolean result = loginVerfifyByTestLan(openId, token);
|
||
if (!result) {
|
||
response.sendError(400, "verify fail");
|
||
LOGGER.error("test verify fail");
|
||
return false;
|
||
}
|
||
} else if (!"3".equals(platform)) {//正式 切不是pc
|
||
String sub_channel = request.getParameter("sub_channel");
|
||
boolean result = false;
|
||
if (sub_channel != null && !sub_channel.equals("")) {
|
||
if (sub_channel.equals("1000")) {
|
||
//测试服appId
|
||
result = loginVerfify("MHT", openId, token, MHTSDKConstans.rhappid);
|
||
} else if(sub_channel.equals("20201222")){
|
||
//商务服
|
||
result = loginVerfifyShangwu("MHT", openId, token, MHTSDKConstans.rhappIdshangwu);
|
||
} else {
|
||
//正式服appId
|
||
result = loginVerfify("MHT", openId, token, MHTSDKConstans.rhappidOnline);
|
||
}
|
||
}
|
||
if (!result) {
|
||
response.sendError(400, "verify fail");
|
||
LOGGER.error("verify fail");
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
// else {
|
||
// boolean result = loginVerfifyByTestLan(openId, token);
|
||
// if (!result) {
|
||
// response.sendError(400, "verify fail");
|
||
// LOGGER.error("test verify fail");
|
||
// return false;
|
||
// }
|
||
// }
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||
throws ServletException, IOException {
|
||
this.doGet(request, response);
|
||
}
|
||
|
||
|
||
public static boolean loginVerfify(String channelName,String openId,String token,String appId){
|
||
try{
|
||
Properties properties = BaseGlobal.getInstance().properties;
|
||
|
||
String login_url = properties.getProperty("login_url");
|
||
LOGGER.info("login_url={}",login_url);
|
||
String loginUrl = MHTSDKConstans.loginVerify;
|
||
Map<String, String> params = new HashMap<>();
|
||
|
||
params.put("sid", token);
|
||
params.put("appid", appId);
|
||
params.put("channellevel1", channelName);
|
||
String sign = getSign(getParamString(params), appId, MHTSDKConstans.secretKey);
|
||
params.put("sign", sign);
|
||
// loginUrl+"?"+"sid="+params.get("sid")+"&appid="+
|
||
String loginResult = HttpUtils.doPost(login_url,params);
|
||
LOGGER.info("loginResult=>{}",loginResult);
|
||
if(loginResult == null || loginResult.isEmpty()){
|
||
return false;
|
||
}
|
||
return parseLoginResult(loginResult);
|
||
}catch(Exception e){
|
||
return false;
|
||
}
|
||
|
||
}
|
||
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 boolean loginVerfifyByTestLan(String openId,String token) throws Exception {
|
||
String tokenInRedis = BaseGlobal.getInstance().redisApp.get(RedisKey.LOGIN_TOKEN, openId, String.class, -1);
|
||
LOGGER.info("loginVerfifyByTestLan tokenInRedis={} client=>{}",tokenInRedis, token);
|
||
return token.equals(tokenInRedis);
|
||
}
|
||
|
||
|
||
private static boolean parseLoginResult(String orderResult){
|
||
try {
|
||
JSONObject jsonObject = JSONObject.parseObject(orderResult);
|
||
int state = jsonObject.getIntValue("code");
|
||
if(state != 200){
|
||
String content = jsonObject.getString("message");
|
||
LOGGER.info("parseLoginResult content={}",content);
|
||
return false;
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 构建签名
|
||
* sign=md5hex(paramString+md5hex(appid)+md5hex(secret))
|
||
* @return
|
||
*/
|
||
private static String getSign(String paramString,String appid,String secret){
|
||
|
||
return MD5Util.encrypByMd5(paramString + MD5Util.encrypByMd5(appid) + MD5Util.encrypByMd5(secret));
|
||
}
|
||
private static String getParamString(Map<String,String> param){
|
||
|
||
StringBuilder builder = new StringBuilder();
|
||
param.forEach((k,v)->{
|
||
if(builder.length()>0){
|
||
builder.append("&");
|
||
}
|
||
builder.append(k).append("=").append(v);
|
||
});
|
||
return builder.toString();
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
// boolean mht = loginVerfify("MHT", "2318137", "be385683efe228aadac0c8b5822a6fba");
|
||
}
|
||
|
||
}
|