generated from root/miduo_server
修改027登录校验
parent
af4c89c131
commit
3d9e98ecd5
|
|
@ -107,6 +107,13 @@ public class GetUserController extends HttpServlet {
|
|||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
DBObject res = new BasicDBObject();
|
||||
String channel = request.getParameter("channel");
|
||||
if(channel!= null){
|
||||
if("TCX027".equals(channel) || "TCX02702".equals(channel)) {
|
||||
this.do027Get(request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
String openId = request.getParameter("openId");
|
||||
if (openId == null || openId.isEmpty()) {
|
||||
returnErrorToFront(res,response, "openId is empty");
|
||||
|
|
@ -150,7 +157,6 @@ public class GetUserController extends HttpServlet {
|
|||
String gid = request.getParameter("gid");
|
||||
//pid
|
||||
String pid = request.getParameter("pid");
|
||||
String channel = request.getParameter("channel");
|
||||
String sub_channel = request.getParameter("sub_channel");
|
||||
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",openId,token,platform,admin,channel,sub_channel);
|
||||
try {
|
||||
|
|
@ -236,6 +242,139 @@ public class GetUserController extends HttpServlet {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 027渠道要服务器获取openId
|
||||
* @param request
|
||||
* @param response
|
||||
* @throws ServletException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void do027Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
DBObject res = new BasicDBObject();
|
||||
String openId = request.getParameter("openId");
|
||||
if (openId == null) {
|
||||
returnErrorToFront(res,response, "openId is empty");
|
||||
return;
|
||||
}
|
||||
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");
|
||||
//gid
|
||||
String gid = request.getParameter("gid");
|
||||
//pid
|
||||
String pid = request.getParameter("pid");
|
||||
String channel = request.getParameter("channel");
|
||||
String sub_channel = request.getParameter("sub_channel");
|
||||
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",openId,token,platform,admin,channel,sub_channel);
|
||||
try {
|
||||
//根据前端发过来的种类进行校验
|
||||
if(channel==null|| channel.isEmpty()){
|
||||
channel="MHT";
|
||||
}
|
||||
// 内网或者 游心 开天,不需要验证
|
||||
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
||||
|
||||
} else {
|
||||
openId = serviceMap.get(channel).verifyGetOpenId(response, request, admin, platform, pid, openId, token);
|
||||
}
|
||||
if(openId == null){
|
||||
returnErrorToFront(res,response,"校验失败,请重新登录");
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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.isEmpty()){
|
||||
returnErrorToFront(res,response,"服务器信息获取错误");
|
||||
return;
|
||||
}
|
||||
int uid;
|
||||
//加分布式锁
|
||||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
if (userInfos.isEmpty()) {
|
||||
try{
|
||||
boolean getLock=false;
|
||||
while(!getLock){
|
||||
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||||
if(getLock){
|
||||
userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||
//双重校验
|
||||
if (userInfos.isEmpty()) {
|
||||
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) {
|
||||
LOGGER.error("登陆报错:{}",e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
private void returnErrorToFront(DBObject res,HttpServletResponse response,String errorReason) throws IOException {
|
||||
res.put("errorCode", -1);
|
||||
res.put("reason",errorReason);
|
||||
|
|
|
|||
|
|
@ -116,4 +116,8 @@ public abstract class AbstractVerifyService implements VerifyService {
|
|||
protected abstract VerifyParams getTestParam(VerifyParams params,HttpServletRequest request,String openId,String token);
|
||||
|
||||
protected abstract VerifyParams getFormatParam(VerifyParams params,HttpServletRequest request,String openId,String token);
|
||||
|
||||
public String verifyGetOpenId(HttpServletResponse response, HttpServletRequest request, String admin, String platform, String pid, String openId,String token){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,7 +80,8 @@ public class Tcx027VerifyService extends AbstractVerifyService {
|
|||
return doVerify(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
|
||||
boolean doVerify(VerifyParams params) {
|
||||
String url = "http://sdk.user.i.027gm.com/game/verify/login";
|
||||
LOGGER.info("027登录验证-->{}",params.getOpenId());
|
||||
|
|
@ -112,6 +113,42 @@ public class Tcx027VerifyService extends AbstractVerifyService {
|
|||
return result;
|
||||
}
|
||||
|
||||
String doVerifyGetOpenId(VerifyParams params) {
|
||||
String url = "http://sdk.user.i.027gm.com/game/verify/login";
|
||||
LOGGER.info("027登录验证-->{}",params.getOpenId());
|
||||
Map<String,String> parms = new HashMap<>();
|
||||
String token = params.getToken();
|
||||
String appId = params.getAppId();
|
||||
parms.put("token",token);
|
||||
parms.put("appId",appId);
|
||||
String sign = MD5Util.encrypByMd5(appId + serverKey + token);
|
||||
parms.put("sign",sign);
|
||||
try {
|
||||
String r = HttpUtils.doPost(url, parms);
|
||||
LOGGER.info("027登录结果:{}",r);
|
||||
JSONObject jsonObject = JSON.parseObject(r);
|
||||
int status = jsonObject.getIntValue("status");
|
||||
if(status == 1 ){
|
||||
String userId = jsonObject.getString("userId");
|
||||
if (userId == null || userId.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
//验证成功返回openId
|
||||
LOGGER.info("027 openId : ",userId);
|
||||
return userId;
|
||||
}
|
||||
else{
|
||||
if(status == 1001){
|
||||
LOGGER.info("sign error status : ",status);
|
||||
}
|
||||
}
|
||||
}catch (IOException e){
|
||||
LOGGER.info("027验证失败,{}",e.getMessage());
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VerifyParams getTestParam(VerifyParams params, HttpServletRequest request, String openId, String token) {
|
||||
params.setToken(token);
|
||||
|
|
@ -125,4 +162,52 @@ public class Tcx027VerifyService extends AbstractVerifyService {
|
|||
params.setOpenId(openId);
|
||||
return params;
|
||||
}
|
||||
|
||||
public String verifyGetOpenId(HttpServletResponse response, HttpServletRequest request, String admin, String platform, String pid, String openId,String token){
|
||||
try {
|
||||
//如果是自己人不需要验证了
|
||||
if(AppConstans.appsecret.equals(admin)) {
|
||||
return null;
|
||||
}
|
||||
if (isTestLan == 1) { //test
|
||||
boolean result = loginVerfifyByTestLan(openId, token);
|
||||
if (!result) {
|
||||
response.sendError(400, "verify fail");
|
||||
LOGGER.error("test verify fail");
|
||||
return null;
|
||||
}
|
||||
return openId;
|
||||
}
|
||||
|
||||
if (!"1".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")) {
|
||||
//测试服构建参数
|
||||
VerifyParams params = new VerifyParams();
|
||||
VerifyParams testParam = getTestParam(params,request, openId, token);
|
||||
result = verifyTest(testParam);
|
||||
if(result){
|
||||
return openId;
|
||||
}
|
||||
}else {
|
||||
//正式服appId
|
||||
VerifyParams params = new VerifyParams();
|
||||
VerifyParams formatParam = getFormatParam(params,request, openId, token);
|
||||
return doVerifyGetOpenId(formatParam);
|
||||
}
|
||||
}
|
||||
if (!result) {
|
||||
response.sendError(400, "verify fail");
|
||||
LOGGER.error("verify fail");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ public interface VerifyService {
|
|||
|
||||
boolean verifyFormat(VerifyParams params);
|
||||
|
||||
//通过服务器校验获取openId
|
||||
String verifyGetOpenId(HttpServletResponse response, HttpServletRequest request, String admin, String platform, String pid,String openId,String token);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue