You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.8 KiB
78 lines
2.8 KiB
4 years ago
|
package com.fr.plugin.http.ip;
|
||
|
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.IPTokenPluginDecisionDBAccessProvider;
|
||
|
import com.fr.plugin.dao.UserIpRelationDao;
|
||
|
import com.fr.plugin.entity.UserIpEntity;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.db.accessor.DBAccessor;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.IOException;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
public class AddApi extends BaseHttpHandler {
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return RequestMethod.POST;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return "/ip/add";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isPublic() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
|
||
|
DBAccessor dbAccessor = IPTokenPluginDecisionDBAccessProvider.getDbAccessor();
|
||
|
//通过这两个函数获取的参数可以避免中文乱码和空指针
|
||
|
// String parameter = httpServletRequest.getParameter("");
|
||
|
// String userName = WebUtils.getHTTPRequestParameter(httpServletRequest, "userName");
|
||
|
// String ip = WebUtils.getHTTPRequestParameter(httpServletRequest, "ip");
|
||
|
String body = getBody(httpServletRequest);//{"userName":"admin","ip":"123345566"}
|
||
|
JSONObject entries = new JSONObject(body);
|
||
|
|
||
|
String userName =entries.getString("userName");
|
||
|
String ip =entries.getString("ip");
|
||
|
if (StringUtils.isBlank(userName)||StringUtils.isBlank(ip)) {
|
||
|
JSONObject jsonObject = new JSONObject();
|
||
|
jsonObject.put("msg", "user name or ip is black");
|
||
|
jsonObject.put("code", -1);
|
||
|
WebUtils.printAsJSON(httpServletResponse, jsonObject);
|
||
|
}
|
||
|
UserIpEntity ipEntity = new UserIpEntity();
|
||
|
ipEntity.setUserName(userName);
|
||
|
ipEntity.setIp(ip);
|
||
|
ipEntity.setId(UUID.randomUUID().toString());
|
||
|
//执行新增操作
|
||
|
dbAccessor.runDMLAction(daoContext -> {
|
||
|
daoContext.getDAO(UserIpRelationDao.class).add(ipEntity);
|
||
|
return null;
|
||
|
});
|
||
|
JSONObject jsonObject = new JSONObject();
|
||
|
jsonObject.put("msg", "success");
|
||
|
jsonObject.put("code", 200);
|
||
|
WebUtils.printAsJSON(httpServletResponse, jsonObject);
|
||
|
}
|
||
|
|
||
|
String getBody(HttpServletRequest request) throws IOException {
|
||
|
BufferedReader br = request.getReader();
|
||
|
String str;
|
||
|
StringBuilder wholeStr = new StringBuilder();
|
||
|
while((str = br.readLine()) != null){
|
||
|
wholeStr.append(str);
|
||
|
}
|
||
|
return wholeStr.toString();
|
||
|
}
|
||
|
}
|