| 20 | private static final SecureRandom RANDOM = new SecureRandom(); |
| 21 | |
| 22 | public static void main(String[] args) throws Exception { |
| 23 | Scanner scanner = new Scanner(System.in); |
| 24 | |
| 25 | System.out.print("请输入 IM 服务地址(如 http://192.168.1.100:18080): "); |
| 26 | String serverUrl = scanner.nextLine().trim(); |
| 27 | |
| 28 | System.out.print("请输入管理员密钥(admin secret): "); |
| 29 | String secret = scanner.nextLine().trim(); |
| 30 | |
| 31 | System.out.print("请输入电话号码: "); |
| 32 | String mobile = scanner.nextLine().trim(); |
| 33 | |
| 34 | scanner.close(); |
| 35 | |
| 36 | if (serverUrl.isEmpty() || secret.isEmpty() || mobile.isEmpty()) { |
| 37 | System.err.println("错误:服务地址、密钥和电话号码均不能为空"); |
| 38 | System.exit(1); |
| 39 | } |
| 40 | |
| 41 | // 构建请求 URL(去掉末尾可能的 /) |
| 42 | if (serverUrl.endsWith("/")) { |
| 43 | serverUrl = serverUrl.substring(0, serverUrl.length() - 1); |
| 44 | } |
| 45 | String urlStr = serverUrl + "/admin/user/get_info"; |
| 46 | |
| 47 | // 构建请求体 |
| 48 | String jsonBody = "{\"mobile\":\"" + mobile + "\",\"includeDeleted\":false}"; |
| 49 | |
| 50 | // 生成认证签名 |
| 51 | int nonce = RANDOM.nextInt(1000000) + 1; |
| 52 | long timestamp = System.currentTimeMillis(); |
| 53 | String signStr = nonce + "|" + secret + "|" + timestamp; |
| 54 | String sign = sha1Hex(signStr); |
| 55 | |
| 56 | // 发送 HTTP POST 请求 |
| 57 | String response = doPost(urlStr, jsonBody, nonce, timestamp, sign); |
| 58 | |
| 59 | // 输出结果 |
| 60 | System.out.println("\n===== 返回结果 ====="); |
| 61 | System.out.println(response); |
| 62 | } |
| 63 | |
| 64 | private static String doPost(String urlStr, String body, int nonce, long timestamp, String sign) throws Exception { |
| 65 | URL url = new URL(urlStr); |