miduo_login/src/main/java/com/ljsd/controller/GetUserController.java

313 lines
11 KiB
Java
Raw Normal View History

2018-12-26 14:42:05 +08:00
package com.ljsd.controller;
2019-07-01 11:27:00 +08:00
import com.alibaba.fastjson.JSONObject;
2018-12-26 14:42:05 +08:00
import com.ljsd.redis.RedisKey;
2020-11-12 17:28:37 +08:00
import com.ljsd.redis.RedisUtil;
2019-09-02 15:13:41 +08:00
import com.ljsd.util.*;
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;
import java.io.IOException;
import java.io.PrintWriter;
2019-08-19 10:06:30 +08:00
import java.util.*;
2020-11-12 17:28:37 +08:00
import java.util.concurrent.TimeUnit;
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
public static int isTestLan=0;
public static void initLanState(){
Properties properties = BaseGlobal.getInstance().properties;
isTestLan = Integer.parseInt(properties.getProperty("isTestLan"));
}
2018-12-26 14:42:05 +08:00
public GetUserController() {
super();
}
public void destroy() {
super.destroy();
}
2020-07-26 18:48:06 +08:00
/**
* openId id
* version
* serverId
* token
* platform androidios 3
* admin
* gid pid
*
*
*---verty--
* openid token
*
* ---return--
*
* uidopenid + serverid + platid
*
*/
2018-12-26 14:42:05 +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()) {
2021-05-23 00:01:22 +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;
}
String platform = request.getParameter("platform"); //平台类型
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;
}
2019-09-02 15:13:41 +08:00
String admin = request.getParameter("admin"); //平台类型
2019-10-09 19:55:23 +08:00
String gid = request.getParameter("gid"); //gid
String pid = request.getParameter("pid"); //pid
2019-09-02 15:13:41 +08:00
/*if (StringUtils.checkIsEmpty(admin)) {
response.sendError(400, "platform is empety");
return;
}*/
2020-11-13 18:02:44 +08:00
LOGGER.info("the opendId = {},token={},platform={} admin=>{}",openId,token,platform,admin);
2018-12-26 14:42:05 +08:00
try {
boolean vertify = vertify(response, request, admin, platform, pid, openId, token);
if(!vertify){
2021-05-23 00:01:22 +08:00
returnErrorToFront(res,response,"校验失败,请重新登录");
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
2019-08-19 10:06:30 +08:00
2018-12-26 14:42:05 +08:00
int uid = 0;
2018-12-27 10:06:44 +08:00
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
2018-12-26 14:42:05 +08:00
if (userInfos.size() == 0) {
2018-12-27 13:31:34 +08:00
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
2018-12-26 14:42:05 +08:00
dbObject.put("uid", uid);
2018-12-27 13:31:34 +08:00
dbObject.put("_id", uid);
2018-12-26 14:42:05 +08:00
userInfos.add(dbObject);
2019-11-29 15:05:48 +08:00
if (gid != null && !gid.isEmpty()) {
dbObject.put("gid", gid);
}
if (pid != null && !pid.isEmpty()) {
dbObject.put("pid", pid);
}
2018-12-27 10:06:44 +08:00
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
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);
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);
2019-11-29 15:05:48 +08:00
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
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")) {
2021-03-16 10:29:34 +08:00
//测试服appId
2021-03-15 16:31:22 +08:00
result = loginVerfify("MHT", openId, token, MHTSDKConstans.rhappid);
2021-03-22 15:40:11 +08:00
} else if(sub_channel.equals("20201222")){
//商务服
result = loginVerfifyShangwu("MHT", openId, token, MHTSDKConstans.rhappIdshangwu);
2020-08-12 14:33:18 +08:00
} else {
2021-03-16 10:29:34 +08:00
//正式服appId
2021-03-15 16:31:22 +08:00
result = loginVerfify("MHT", openId, token, MHTSDKConstans.rhappidOnline);
2020-08-12 14:33:18 +08:00
}
}
if (!result) {
response.sendError(400, "verify fail");
LOGGER.error("verify fail");
return false;
}
}
}
2021-02-25 01:12:48 +08:00
// else {
// boolean result = loginVerfifyByTestLan(openId, token);
// if (!result) {
// response.sendError(400, "verify fail");
// LOGGER.error("test verify fail");
// return false;
// }
// }
2020-08-12 14:33:18 +08:00
}catch (Exception e){
e.printStackTrace();
}
return true;
}
2018-12-26 14:42:05 +08:00
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
2019-07-01 11:27:00 +08:00
2020-08-18 01:16:49 +08:00
public static boolean loginVerfify(String channelName,String openId,String token,String appId){
2019-07-01 11:27:00 +08:00
try{
2020-07-26 18:48:06 +08:00
String loginUrl = MHTSDKConstans.loginVerify;
2021-03-15 16:31:22 +08:00
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="+
2019-07-01 11:27:00 +08:00
String loginResult = HttpUtils.doPost(loginUrl,params);
if(loginResult == null || loginResult.isEmpty()){
2019-07-01 16:57:23 +08:00
return false;
2019-07-01 11:27:00 +08:00
}
return parseLoginResult(loginResult);
}catch(Exception e){
2019-07-01 16:57:23 +08:00
return false;
2019-07-01 11:27:00 +08:00
}
2021-03-22 15:40:11 +08:00
}
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;
}
2019-07-01 11:27:00 +08:00
}
2019-08-19 10:06:30 +08:00
public static boolean loginVerfifyByTestLan(String openId,String token) throws Exception {
String tokenInRedis = BaseGlobal.getInstance().redisApp.get(RedisKey.LOGIN_TOKEN, openId, String.class, -1);
2020-11-13 15:23:03 +08:00
LOGGER.info("loginVerfifyByTestLan tokenInRedis={} client=>{}",tokenInRedis, token);
2019-08-19 10:06:30 +08:00
return token.equals(tokenInRedis);
}
2019-07-01 11:27:00 +08:00
2019-07-01 16:57:23 +08:00
private static boolean parseLoginResult(String orderResult){
2019-07-01 11:27:00 +08:00
try {
2019-07-01 16:57:23 +08:00
JSONObject jsonObject = JSONObject.parseObject(orderResult);
2020-07-26 18:48:06 +08:00
int state = jsonObject.getIntValue("code");
if(state != 200){
String content = jsonObject.getString("message");
LOGGER.info("parseLoginResult content={}",content);
2019-07-01 16:57:23 +08:00
return false;
2019-07-01 11:27:00 +08:00
}
} catch (Exception e) {
e.printStackTrace();
}
2019-07-01 16:57:23 +08:00
return true;
2019-07-01 11:27:00 +08:00
}
2021-03-15 16:31:22 +08:00
/**
*
* 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();
}
2020-07-26 18:48:06 +08:00
public static void main(String[] args) {
2020-08-18 01:16:49 +08:00
// boolean mht = loginVerfify("MHT", "2318137", "be385683efe228aadac0c8b5822a6fba");
2020-07-26 18:48:06 +08:00
}
2018-12-26 14:42:05 +08:00
}