From 1122f50f9e25ab04bb644ce2e54723581ea61c32 Mon Sep 17 00:00:00 2001 From: dengqn <434500374@qq.com> Date: Sat, 20 Jul 2024 16:53:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- pom.xml | 16 ++++- .../igps2xingzhe/config/HttpClientConfig.java | 39 +++++++++++ .../dengqn/igps2xingzhe/config/IGPSport.java | 4 ++ .../dengqn/igps2xingzhe/config/XingZhe.java | 4 ++ .../igps2xingzhe/controller/Trigger.java | 67 ++++++++++++++++++- .../resources/application.properties.example | 4 +- 7 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/dengqn/igps2xingzhe/config/HttpClientConfig.java diff --git a/.gitignore b/.gitignore index ce69305..83d6433 100644 --- a/.gitignore +++ b/.gitignore @@ -32,5 +32,5 @@ build/ ### VS Code ### .vscode/ -src/resource/application.properties +application.properties diff --git a/pom.xml b/pom.xml index 1689f67..34be9dd 100644 --- a/pom.xml +++ b/pom.xml @@ -40,11 +40,21 @@ lombok true + - org.springframework.boot - spring-boot-starter-test - test + cn.hutool + hutool-all + 5.8.27 + + + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + + + + diff --git a/src/main/java/com/dengqn/igps2xingzhe/config/HttpClientConfig.java b/src/main/java/com/dengqn/igps2xingzhe/config/HttpClientConfig.java new file mode 100644 index 0000000..5ac14bd --- /dev/null +++ b/src/main/java/com/dengqn/igps2xingzhe/config/HttpClientConfig.java @@ -0,0 +1,39 @@ +package com.dengqn.igps2xingzhe.config; + +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.cookie.BasicCookieStore; +import org.apache.hc.client5.http.cookie.CookieStore; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.MinimalHttpClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author dengqn + * @since 2024/7/20 15:22 + */ +@Component +public class HttpClientConfig { + @Autowired + private IGPSport iGPSport; + @Autowired + private XingZhe xingZhe; + + @Bean + public CookieStore cookieStore() { + return new BasicCookieStore(); + } + + + @Bean + public HttpClient httpClient(CookieStore cookieStore) { + return HttpClientBuilder.create() + .setDefaultCookieStore(cookieStore) + .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36") + .build(); + } + + +} diff --git a/src/main/java/com/dengqn/igps2xingzhe/config/IGPSport.java b/src/main/java/com/dengqn/igps2xingzhe/config/IGPSport.java index 0b1421e..b61a859 100644 --- a/src/main/java/com/dengqn/igps2xingzhe/config/IGPSport.java +++ b/src/main/java/com/dengqn/igps2xingzhe/config/IGPSport.java @@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; import java.io.Serializable; @@ -15,6 +17,8 @@ import java.io.Serializable; @Builder @NoArgsConstructor @AllArgsConstructor +@Configuration +@ConfigurationProperties(prefix = "user.igps") public class IGPSport implements Serializable { private static final long serialVersionUID = 3004387713475397922L; diff --git a/src/main/java/com/dengqn/igps2xingzhe/config/XingZhe.java b/src/main/java/com/dengqn/igps2xingzhe/config/XingZhe.java index 16f0bd9..b338086 100644 --- a/src/main/java/com/dengqn/igps2xingzhe/config/XingZhe.java +++ b/src/main/java/com/dengqn/igps2xingzhe/config/XingZhe.java @@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; import java.io.Serializable; @@ -15,6 +17,8 @@ import java.io.Serializable; @Builder @NoArgsConstructor @AllArgsConstructor +@Configuration +@ConfigurationProperties(prefix = "user.xingzhe") public class XingZhe implements Serializable { private static final long serialVersionUID = 2522264649038579098L; diff --git a/src/main/java/com/dengqn/igps2xingzhe/controller/Trigger.java b/src/main/java/com/dengqn/igps2xingzhe/controller/Trigger.java index 8a66cd9..596d32a 100644 --- a/src/main/java/com/dengqn/igps2xingzhe/controller/Trigger.java +++ b/src/main/java/com/dengqn/igps2xingzhe/controller/Trigger.java @@ -1,23 +1,88 @@ package com.dengqn.igps2xingzhe.controller; +import cn.hutool.crypto.KeyUtil; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm; +import cn.hutool.crypto.asymmetric.KeyType; +import cn.hutool.crypto.asymmetric.RSA; +import cn.hutool.json.JSONUtil; +import com.dengqn.igps2xingzhe.config.XingZhe; +import lombok.extern.slf4j.Slf4j; +import org.apache.catalina.security.SecurityUtil; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.cookie.Cookie; +import org.apache.hc.client5.http.cookie.CookieStore; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.entity.BasicHttpEntity; +import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; +import org.apache.hc.core5.http.message.BasicHttpResponse; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPublicKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.HashMap; +import java.util.Map; + /** * 同步 * * @author dengqn * @since 2024/7/20 11:21 */ +@Slf4j @RestController @RequestMapping("/api/trigger") public class Trigger { + @Autowired + private HttpClient httpClient; + @Autowired + private CookieStore cookieStore; + + + @Autowired + private XingZhe xingZhe; + @GetMapping("/sync/igps2xingzhe") - public ResponseEntity onSyncIGPS2XingZhe() { + public ResponseEntity onSyncIGPS2XingZhe() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { + + // 1、获取行者平台的cookie + HttpResponse executed = httpClient.execute(ClassicRequestBuilder.get("https://www.imxingzhe.com/user/login").build()); + System.out.println(executed.getCode()); + // 2、计算加密后的密码参数 + + RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), null, xingZhe.getPubKey()); + String rd = cookieStore.getCookies() + .stream().filter(a -> a.getName().equals("rd")).findFirst() + .map(Cookie::getValue).orElse(""); + String encryptBase64 = rsa.encryptBase64(xingZhe.getPassword() + ";" + rd, KeyType.PublicKey); + + Map loginData = new HashMap<>(); + loginData.put("account", xingZhe.getUsername()); + loginData.put("password", encryptBase64); + loginData.put("source", "web"); + + BasicHttpResponse response = (BasicHttpResponse) httpClient.execute(ClassicRequestBuilder + .post("https://www.imxingzhe.com/api/v4/account/login") + .setEntity(JSONUtil.toJsonPrettyStr(loginData), ContentType.APPLICATION_JSON) + .build()); + log.info("xingzhe login: {}", response); + return ResponseEntity.ok("ok"); } + + } diff --git a/src/main/resources/application.properties.example b/src/main/resources/application.properties.example index c0f49a8..a9a7247 100644 --- a/src/main/resources/application.properties.example +++ b/src/main/resources/application.properties.example @@ -2,10 +2,12 @@ spring.application.name=igps2xingzhe server.port=18877 +data: E:\\temp user.xingzhe.username: xxx user.xingzhe.password: xxx -user.xingzhe.pubKey: xxx +# 固定公钥 +user.xingzhe.pubKey: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDmuQkBbijudDAJgfffDeeIButqWHZvUwcRuvWdg89393FSdz3IJUHc0rgI/S3WuU8N0VePJLmVAZtCOK4qe4FY/eKmWpJmn7JfXB4HTMWjPVoyRZmSYjW4L8GrWmh51Qj7DwpTADadF3aq04o+s1b8LXJa8r6+TIqqL5WUHtRqmQIDAQAB" user.igps.username: xxx user.igps.password: xxx