generated from root/miduo_server
114 lines
4.1 KiB
Java
114 lines
4.1 KiB
Java
package com.ljsd.controller;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.ljsd.util.HttpUtils;
|
||
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.IOException;
|
||
import java.io.PrintWriter;
|
||
import java.util.*;
|
||
|
||
public class KTGetUserOpenIdController extends HttpServlet {
|
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KTGetUserOpenIdController.class);
|
||
|
||
public static String login_url = "http://api.kt007.com/api/v1/verify/user";
|
||
public static String appkey = "vJnztWhAoOlxbPDdmYTrFLwZI";
|
||
|
||
public KTGetUserOpenIdController() {
|
||
super();
|
||
}
|
||
|
||
public void destroy() {
|
||
super.destroy();
|
||
}
|
||
|
||
|
||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||
DBObject res = new BasicDBObject();
|
||
String token = request.getParameter("access_token");
|
||
if (token == null || token.isEmpty()) {
|
||
returnErrorToFront(res, response, "token is empty");
|
||
return;
|
||
}
|
||
String gid = request.getParameter("gid"); //gid
|
||
String pid = request.getParameter("pid"); //pid
|
||
String tm = Long.toString(System.currentTimeMillis() / 1000);
|
||
LOGGER.info("the token = {},gid={},pid={} tm=>{}", token, gid, pid, tm);
|
||
response.setCharacterEncoding("UTF-8");
|
||
response.setContentType("application/json; charset=utf-8");
|
||
SortedMap<String, String> params = new TreeMap<>();
|
||
params.put("gid", gid);
|
||
params.put("pid", pid);
|
||
params.put("tm", tm);
|
||
params.put("access_token", token);
|
||
StringBuilder sbKey = new StringBuilder();
|
||
Set<Map.Entry<String, String>> entries = params.entrySet();
|
||
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
|
||
while (iterator.hasNext()) {
|
||
Map.Entry<String, String> next = iterator.next();
|
||
String key = next.getKey();
|
||
String value = next.getValue();
|
||
if (value == null || "".equals(value)) {
|
||
continue;
|
||
}
|
||
if ("sign".equals(key) || "sign_type".equals(key)) {
|
||
continue;
|
||
}
|
||
sbKey.append(key).append("=").append(value).append("&");
|
||
}
|
||
sbKey.append(appkey);
|
||
String mySign = MD5Util.encrypByMd5(sbKey.toString());
|
||
params.put("sign", mySign);
|
||
String loginResult = HttpUtils.doPost(login_url, params);
|
||
// LOGGER.info("二次请求 : KTGetUserOpenIdController 加密前={} 加密后={} 请求结果={}", sbKey.toString(), mySign, loginResult);
|
||
if (loginResult == null || loginResult.isEmpty()) {
|
||
returnErrorToFront(res, response, "token is empty");
|
||
return;
|
||
}
|
||
JSONObject jsonObject = JSONObject.parseObject(loginResult);
|
||
int state = jsonObject.getIntValue("code");
|
||
if (state == 200) {
|
||
JSONObject data = (JSONObject) jsonObject.get("data");
|
||
String openid = data.getString("openid");
|
||
int is_certify = data.getIntValue("is_certify");
|
||
int age = data.getIntValue("age");
|
||
res.put("openid", openid);
|
||
res.put("is_certify", is_certify);
|
||
res.put("age", age);
|
||
} else {
|
||
returnErrorToFront(res, response, "SDK 返回错误");
|
||
return;
|
||
}
|
||
PrintWriter out = response.getWriter();
|
||
out.print(res);
|
||
out.flush();
|
||
out.close();
|
||
}
|
||
|
||
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);
|
||
|
||
}
|
||
|
||
}
|