SignUtils.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.fdkankan.sign;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.json.JSONObject;
  4. import java.util.Date;
  5. public class SignUtils {
  6. public static Boolean checkSign(String sign,String appIdValue,String privateKey) {
  7. try {
  8. if(StringUtils.isBlank(sign)){
  9. return false;
  10. }
  11. String deTxt = RsaUtils.decipher(sign, privateKey);
  12. if(StringUtils.isBlank(deTxt)){
  13. return false;
  14. }
  15. JSONObject jsonObject = new JSONObject(deTxt);
  16. String appId = jsonObject.getString("appId");
  17. Long timestamp = jsonObject.getLong("timestamp");
  18. if(StringUtils.isBlank(appId) || timestamp == null){
  19. return false;
  20. }
  21. if(!appId.equals(appIdValue)){
  22. return false;
  23. }
  24. Long time = new Date().getTime();
  25. // if(time < timestamp && timestamp - time > 1000 * 60){
  26. // return false;
  27. // }
  28. //app获取时间存在误差,前后五分钟失败
  29. if( Math.abs(time -timestamp) >1000 * 60 *5){
  30. return false;
  31. }
  32. return true;
  33. }catch (Exception e){
  34. return false;
  35. }
  36. }
  37. }