PhpRequestLogTest.java 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.nokia.finance.tasks.requestlogs;
  2. import com.google.gson.Gson;
  3. import com.google.gson.reflect.TypeToken;
  4. import com.nokia.finance.tasks.utils.AESUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.csv.CSVFormat;
  7. import org.apache.commons.csv.CSVPrinter;
  8. import org.apache.commons.csv.QuoteMode;
  9. import org.junit.jupiter.api.Test;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.test.context.ActiveProfiles;
  14. import org.springframework.util.MultiValueMap;
  15. import org.springframework.util.StringUtils;
  16. import org.springframework.web.util.UriComponents;
  17. import org.springframework.web.util.UriComponentsBuilder;
  18. import java.io.OutputStreamWriter;
  19. import java.net.URLDecoder;
  20. import java.nio.charset.StandardCharsets;
  21. import java.nio.file.Files;
  22. import java.nio.file.Paths;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. @Slf4j
  27. @SpringBootTest
  28. @ActiveProfiles("prod")
  29. class PhpRequestLogTest {
  30. @Autowired
  31. JdbcTemplate jdbcTemplate;
  32. @Test
  33. void test() throws Exception {
  34. Gson gson = new Gson();
  35. Files.createDirectories(Paths.get("test/output"));
  36. String sql = """
  37. select to_char(to_timestamp(create_time), 'YYYY-MM-DD HH24:MI:SS') as request_time, query_string from car_theme.wz_adminlog order by create_time
  38. """;
  39. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  40. try (OutputStreamWriter osw = new OutputStreamWriter(
  41. Files.newOutputStream(Paths.get("test/output/php-request-log.csv")), StandardCharsets.UTF_8);
  42. CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL).build())
  43. ) {
  44. // 添加bom头避免excel乱码
  45. osw.write('\ufeff');
  46. printer.printRecord("request_time", "login_id", "page_url", "api", "request_parameters", "headers", "app_id", "time_stamp", "token");
  47. for (Map<String, Object> t : list) {
  48. String requestTime = (String) t.get("request_time");
  49. String queryString = (String) t.get("query_string");
  50. if (!StringUtils.hasText(requestTime) || !StringUtils.hasText(queryString)) {
  51. continue;
  52. }
  53. UriComponents uriComponents = UriComponentsBuilder.fromUriString("?" + queryString).build();
  54. MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
  55. Map<String, String> singleValueMap = queryParams.toSingleValueMap();
  56. String data = singleValueMap.get("data");
  57. if (!StringUtils.hasText(data)) {
  58. continue;
  59. }
  60. String urlDecodeToken = URLDecoder.decode(data, StandardCharsets.UTF_8);
  61. String decodeToken;
  62. try {
  63. decodeToken = AESUtil.decrypt(urlDecodeToken);
  64. } catch (Exception e) {
  65. log.error("token解密失败: {} -> {} -> {}", urlDecodeToken, queryString, e.toString(), e);
  66. continue;
  67. }
  68. Map<String, String> tokenMap = gson.fromJson(decodeToken, new TypeToken<Map<String, String>>() {
  69. }.getType());
  70. String loginId = tokenMap.get("LOGIN_ID");
  71. String pageUrl = tokenMap.get("REQUEST_URL");
  72. String api = StringUtils.hasText(singleValueMap.get("type")) ? pageUrl + "?type=" + singleValueMap.get("type") : pageUrl;
  73. Map<String, String> requestParameters = new HashMap<>();
  74. requestParameters.put("request_parameters", gson.toJson(singleValueMap));
  75. String appId = tokenMap.get("APP_ID");
  76. String timeStamp = tokenMap.get("TIME_STAMP");
  77. printer.printRecord(
  78. requestTime,
  79. loginId,
  80. pageUrl,
  81. api,
  82. gson.toJson(requestParameters),
  83. null,
  84. appId,
  85. timeStamp,
  86. urlDecodeToken
  87. );
  88. }
  89. } catch (Exception e) {
  90. log.error(e.toString(), e);
  91. }
  92. }
  93. }