This commit is contained in:
dengqn 2024-07-20 16:53:49 +08:00
parent b021366642
commit 1122f50f9e
7 changed files with 130 additions and 6 deletions

2
.gitignore vendored
View File

@ -32,5 +32,5 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
src/resource/application.properties application.properties

16
pom.xml
View File

@ -40,11 +40,21 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>cn.hutool</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>hutool-all</artifactId>
<scope>test</scope> <version>5.8.27</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -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();
}
}

View File

@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.io.Serializable; import java.io.Serializable;
@ -15,6 +17,8 @@ import java.io.Serializable;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "user.igps")
public class IGPSport implements Serializable { public class IGPSport implements Serializable {
private static final long serialVersionUID = 3004387713475397922L; private static final long serialVersionUID = 3004387713475397922L;

View File

@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.io.Serializable; import java.io.Serializable;
@ -15,6 +17,8 @@ import java.io.Serializable;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "user.xingzhe")
public class XingZhe implements Serializable { public class XingZhe implements Serializable {
private static final long serialVersionUID = 2522264649038579098L; private static final long serialVersionUID = 2522264649038579098L;

View File

@ -1,23 +1,88 @@
package com.dengqn.igps2xingzhe.controller; 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.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; 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 * @author dengqn
* @since 2024/7/20 11:21 * @since 2024/7/20 11:21
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("/api/trigger") @RequestMapping("/api/trigger")
public class Trigger { public class Trigger {
@Autowired
private HttpClient httpClient;
@Autowired
private CookieStore cookieStore;
@Autowired
private XingZhe xingZhe;
@GetMapping("/sync/igps2xingzhe") @GetMapping("/sync/igps2xingzhe")
public ResponseEntity<String> onSyncIGPS2XingZhe() { public ResponseEntity<String> 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<String, Object> 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"); return ResponseEntity.ok("ok");
} }
} }

View File

@ -2,10 +2,12 @@ spring.application.name=igps2xingzhe
server.port=18877 server.port=18877
data: E:\\temp
user.xingzhe.username: xxx user.xingzhe.username: xxx
user.xingzhe.password: xxx user.xingzhe.password: xxx
user.xingzhe.pubKey: xxx # 固定公钥
user.xingzhe.pubKey: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDmuQkBbijudDAJgfffDeeIButqWHZvUwcRuvWdg89393FSdz3IJUHc0rgI/S3WuU8N0VePJLmVAZtCOK4qe4FY/eKmWpJmn7JfXB4HTMWjPVoyRZmSYjW4L8GrWmh51Qj7DwpTADadF3aq04o+s1b8LXJa8r6+TIqqL5WUHtRqmQIDAQAB"
user.igps.username: xxx user.igps.username: xxx
user.igps.password: xxx user.igps.password: xxx