Authored by qinchao

删除恶意ip时携带header

... ... @@ -12,4 +12,5 @@ public interface HttpRestClientService {
public <T> T get(String path, Map request, Class<T> responseType);
public <T> T getWithHost(String path, Map request, Class<T> responseType);
}
... ...
... ... @@ -110,4 +110,40 @@ public class HttpRestClientServiceImpl implements HttpRestClientService {
}
}
@Override
public <T> T getWithHost(String path, Map request, Class<T> responseType) {
logger.debug("begin to do http get host. path:{}, request params:{}", path, request);
//add accept json
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.set("Host","erp.yoho.yohoops.org");
HttpEntity<?> entity = new HttpEntity<>(headers);
//url
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(path);
//添加参数
if(request!=null){
Map<String, ?> params = (Map<String, ?>) request;
if (params != null && params.size() > 0) {
for (Map.Entry<String, ?> urlParam : params.entrySet()) {
builder.queryParam(urlParam.getKey(), urlParam.getValue());
}
}
}
final URI getURI = builder.build().encode().toUri();
try {
HttpEntity<T> response = restTemplate.exchange(getURI, HttpMethod.GET, entity, responseType);
logger.debug("end to do http get. path:{}, request params:{}. response body: {}", path, request, response.getBody());
return response.getBody();
} catch (Exception e) {
logger.error("doPostStringJson failed!url:"+path, e);
return null;
}
}
}
... ...
... ... @@ -77,13 +77,13 @@ public class MaliciousIpController {
public BaseResponse delMipsFromOpsRedis(String ip) {
if(StringUtils.isNotBlank(ip)){
//获取header信息,
HttpHeaders headers = new HttpHeaders();
headers.add("Host","erp.yoho.yohoops.org");
/* HttpHeaders headers = new HttpHeaders();
headers.add("Host","erp.yoho.yohoops.org");*/
String[] hosts=maliciousRedisHosts.split(",");
String[] ports=maliciousRedisPorts.split(",");
for(int i=0;i<hosts.length;i++){
String url="http://"+hosts[i]+":"+ports[i]+"/malIp?method=del&ips=";
String removeReturnMsg = httpRestClientService.postForObject(url+ip, new HttpEntity<String>(headers), String.class);
String removeReturnMsg = httpRestClientService.getWithHost(url+ip, null, String.class);
if(StringUtils.isBlank(removeReturnMsg)){
return new BaseResponse(201,"删除恶意ip返回结果为空,删除出现异常,主机:"+hosts[i]);
}
... ...