Commit 6e01e832596e1769bdd53cb634b3019833e93bf4
1 parent
89a803bf
Exists in
master
1.注册server时不发送请求 2.请求增加seckey
Showing
6 changed files
with
28 additions
and
7 deletions
Show diff stats
build.gradle
src/main/java/com/taover/heartbeat/ClientHolderImpl.java
| ... | ... | @@ -9,6 +9,8 @@ import org.apache.commons.lang3.StringUtils; |
| 9 | 9 | |
| 10 | 10 | import com.taover.heartbeat.bean.ClientInstance; |
| 11 | 11 | import com.taover.heartbeat.bean.ClientRequest; |
| 12 | +import com.taover.heartbeat.bean.Instance; | |
| 13 | +import com.taover.util.UtilEncrypt; | |
| 12 | 14 | |
| 13 | 15 | public class ClientHolderImpl implements ClientHolder{ |
| 14 | 16 | Map<String, ClientInstance> clientMap = new HashMap<String, ClientInstance>(); |
| ... | ... | @@ -42,9 +44,12 @@ public class ClientHolderImpl implements ClientHolder{ |
| 42 | 44 | } |
| 43 | 45 | |
| 44 | 46 | private boolean validClientRequest(ClientRequest clientRequest) { |
| 47 | + long time = clientRequest.getUnixtime(); | |
| 48 | + String seckey = UtilEncrypt.MD5Lower32(clientRequest.getCode()+clientRequest.getFixRateSec()+clientRequest.getMaxWaitSec()+time+Instance.SERVER_ENCRPT_SURFFIX); | |
| 45 | 49 | if(StringUtils.isNotBlank(clientRequest.getCode()) |
| 46 | 50 | && StringUtils.isNotBlank(clientRequest.getIp()) |
| 47 | - && clientRequest.getFixRateSec() > 0) { | |
| 51 | + && clientRequest.getFixRateSec() > 0 | |
| 52 | + && seckey.equals(clientRequest.getSeckey())) { | |
| 48 | 53 | return true; |
| 49 | 54 | }else { |
| 50 | 55 | return false; | ... | ... |
src/main/java/com/taover/heartbeat/ServerHolderImpl.java
src/main/java/com/taover/heartbeat/bean/ClientRequest.java
| ... | ... | @@ -8,6 +8,7 @@ public class ClientRequest { |
| 8 | 8 | private int maxWaitSec = Instance.DEFAULT_MAX_WAIT_SEC; |
| 9 | 9 | private int fixRateSec = Instance.DEFAULT_FIX_RATE_SEC; |
| 10 | 10 | private long unixtime; |
| 11 | + private String seckey = ""; | |
| 11 | 12 | |
| 12 | 13 | public String getCode() { |
| 13 | 14 | return code; |
| ... | ... | @@ -39,13 +40,20 @@ public class ClientRequest { |
| 39 | 40 | public void setUnixtime(long unixtime) { |
| 40 | 41 | this.unixtime = unixtime; |
| 41 | 42 | } |
| 43 | + public String getSeckey() { | |
| 44 | + return seckey; | |
| 45 | + } | |
| 46 | + public void setSeckey(String seckey) { | |
| 47 | + this.seckey = seckey; | |
| 48 | + } | |
| 42 | 49 | |
| 43 | - public ClientRequest(String code, String ip, int maxWaitSec, int fixRateSec, long unixtime) { | |
| 50 | + public ClientRequest(String code, String ip, int maxWaitSec, int fixRateSec, long unixtime, String seckey) { | |
| 44 | 51 | this.code = code; |
| 45 | 52 | this.ip = ip; |
| 46 | 53 | this.maxWaitSec = maxWaitSec; |
| 47 | 54 | this.fixRateSec = fixRateSec; |
| 48 | 55 | this.unixtime = unixtime; |
| 56 | + this.seckey = seckey; | |
| 49 | 57 | } |
| 50 | 58 | |
| 51 | 59 | public ClientRequest(ClientRequest clientRequest) { |
| ... | ... | @@ -54,6 +62,7 @@ public class ClientRequest { |
| 54 | 62 | this.maxWaitSec = clientRequest.getMaxWaitSec(); |
| 55 | 63 | this.fixRateSec = clientRequest.getFixRateSec(); |
| 56 | 64 | this.unixtime = clientRequest.getUnixtime(); |
| 65 | + this.seckey = clientRequest.getSeckey(); | |
| 57 | 66 | } |
| 58 | 67 | |
| 59 | 68 | public static ClientRequest createClientRequest(HttpServletRequest request) { |
| ... | ... | @@ -61,11 +70,13 @@ public class ClientRequest { |
| 61 | 70 | String maxWaitSec = request.getParameter("maxWaitSec"); |
| 62 | 71 | String fixRateSec = request.getParameter("fixRateSec"); |
| 63 | 72 | String unixtime = request.getParameter("unixtime"); |
| 73 | + String seckey = request.getParameter("seckey"); | |
| 64 | 74 | String ip = request.getRemoteHost(); |
| 65 | 75 | return new ClientRequest(code, ip, |
| 66 | 76 | maxWaitSec==null?Instance.DEFAULT_MAX_WAIT_SEC:Integer.valueOf(maxWaitSec), |
| 67 | 77 | fixRateSec==null?Instance.DEFAULT_FIX_RATE_SEC:Integer.valueOf(fixRateSec), |
| 68 | - unixtime==null?System.currentTimeMillis()/1000:Long.valueOf(unixtime)); | |
| 78 | + unixtime==null?System.currentTimeMillis()/1000:Long.valueOf(unixtime), | |
| 79 | + seckey); | |
| 69 | 80 | } |
| 70 | 81 | |
| 71 | 82 | public String getIdentity() { | ... | ... |
src/main/java/com/taover/heartbeat/bean/Instance.java
src/main/java/com/taover/heartbeat/bean/ServerInstance.java
| ... | ... | @@ -2,6 +2,7 @@ package com.taover.heartbeat.bean; |
| 2 | 2 | |
| 3 | 3 | import java.net.SocketTimeoutException; |
| 4 | 4 | |
| 5 | +import com.taover.util.UtilEncrypt; | |
| 5 | 6 | import com.taover.util.UtilHttpByOkHttp; |
| 6 | 7 | |
| 7 | 8 | public class ServerInstance implements Instance { |
| ... | ... | @@ -53,8 +54,10 @@ public class ServerInstance implements Instance { |
| 53 | 54 | } |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | - private String getUrlParams() { | |
| 57 | - return "?code="+this.code+"&fixRateSec="+this.fixRateSec+"&maxWaitSec="+this.maxWaitSec+"&unixtime="+(System.currentTimeMillis()/1000); | |
| 57 | + private String getUrlParams() { | |
| 58 | + long time = (System.currentTimeMillis()/1000L); | |
| 59 | + String seckey = UtilEncrypt.MD5Lower32(this.code+this.fixRateSec+this.maxWaitSec+time+SERVER_ENCRPT_SURFFIX); | |
| 60 | + return "?code="+this.code+"&fixRateSec="+this.fixRateSec+"&maxWaitSec="+this.maxWaitSec+"&unixtime="+time+"&seckey="+seckey; | |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | 63 | private boolean isLatestRequestGreateEqualFixRateSec() { | ... | ... |