12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package com.nokia.finance.tasks.requestlogs;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.nokia.finance.tasks.utils.AESUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.csv.CSVFormat;
- import org.apache.commons.csv.CSVPrinter;
- import org.apache.commons.csv.QuoteMode;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.test.context.ActiveProfiles;
- import org.springframework.util.MultiValueMap;
- import org.springframework.util.StringUtils;
- import org.springframework.web.util.UriComponents;
- import org.springframework.web.util.UriComponentsBuilder;
- import java.io.OutputStreamWriter;
- import java.net.URLDecoder;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @SpringBootTest
- @ActiveProfiles("prod")
- class PhpRequestLogTest {
- @Autowired
- JdbcTemplate jdbcTemplate;
- @Test
- void test() throws Exception {
- Gson gson = new Gson();
- Files.createDirectories(Paths.get("test/output"));
- String sql = """
- 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
- """;
- List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
- try (OutputStreamWriter osw = new OutputStreamWriter(
- Files.newOutputStream(Paths.get("test/output/php-request-log.csv")), StandardCharsets.UTF_8);
- CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL).build())
- ) {
- // 添加bom头避免excel乱码
- osw.write('\ufeff');
- printer.printRecord("request_time", "login_id", "page_url", "api", "request_parameters", "headers", "app_id", "time_stamp", "token");
- for (Map<String, Object> t : list) {
- String requestTime = (String) t.get("request_time");
- String queryString = (String) t.get("query_string");
- if (!StringUtils.hasText(requestTime) || !StringUtils.hasText(queryString)) {
- continue;
- }
- UriComponents uriComponents = UriComponentsBuilder.fromUriString("?" + queryString).build();
- MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
- Map<String, String> singleValueMap = queryParams.toSingleValueMap();
- String data = singleValueMap.get("data");
- if (!StringUtils.hasText(data)) {
- continue;
- }
- String urlDecodeToken = URLDecoder.decode(data, StandardCharsets.UTF_8);
- String decodeToken;
- try {
- decodeToken = AESUtil.decrypt(urlDecodeToken);
- } catch (Exception e) {
- log.error("token解密失败: {} -> {} -> {}", urlDecodeToken, queryString, e.toString(), e);
- continue;
- }
- Map<String, String> tokenMap = gson.fromJson(decodeToken, new TypeToken<Map<String, String>>() {
- }.getType());
- String loginId = tokenMap.get("LOGIN_ID");
- String pageUrl = tokenMap.get("REQUEST_URL");
- String api = StringUtils.hasText(singleValueMap.get("type")) ? pageUrl + "?type=" + singleValueMap.get("type") : pageUrl;
- Map<String, String> requestParameters = new HashMap<>();
- requestParameters.put("request_parameters", gson.toJson(singleValueMap));
- String appId = tokenMap.get("APP_ID");
- String timeStamp = tokenMap.get("TIME_STAMP");
- printer.printRecord(
- requestTime,
- loginId,
- pageUrl,
- api,
- gson.toJson(requestParameters),
- null,
- appId,
- timeStamp,
- urlDecodeToken
- );
- }
- } catch (Exception e) {
- log.error(e.toString(), e);
- }
- }
- }
|