|
|
@@ -1,28 +1,26 @@
|
|
|
-package com.jzg.ansheng.crawler.component;
|
|
|
+package com.jzg.quotation.ansheng.crawler.component;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.jzg.ansheng.crawler.constant.enums.RequestUrl;
|
|
|
-import com.jzg.ansheng.crawler.entity.request.*;
|
|
|
-import com.jzg.ansheng.crawler.entity.response.*;
|
|
|
-import com.jzg.ansheng.crawler.properties.AnShengCrawlerProperties;
|
|
|
import com.jzg.commons.exception.SystemException;
|
|
|
+import com.jzg.quotation.ansheng.crawler.constant.enums.RequestUrl;
|
|
|
+import com.jzg.quotation.ansheng.crawler.entity.request.*;
|
|
|
+import com.jzg.quotation.ansheng.crawler.entity.response.*;
|
|
|
+import com.jzg.quotation.ansheng.crawler.properties.AnShengCrawlerProperties;
|
|
|
import com.jzg.quotation.commons.component.RequestComponent;
|
|
|
+import com.jzg.quotation.commons.utils.CookieParseUtils;
|
|
|
import com.jzg.quotation.commons.utils.RequestObjectConversion;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @description: 安盛天平 爬虫 请求
|
|
|
@@ -37,13 +35,6 @@ public class AnShengCrawlerRequestComponent {
|
|
|
|
|
|
private final AnShengCrawlerProperties anShengCrawlerProperties;
|
|
|
|
|
|
- public <Response> Response request(RequestUrl requestUrl, Object body, Class<Response> responseClass, HttpHeaders headers) {
|
|
|
- if (ObjectUtils.isEmpty(headers)) {
|
|
|
- headers = new HttpHeaders();
|
|
|
- }
|
|
|
- ResponseEntity<Response> responseResponseEntity = requestComponent.objectPost(requestUrl.getRequestUrl(anShengCrawlerProperties.getUrl()), body, responseClass, headers);
|
|
|
- return responseResponseEntity.getBody();
|
|
|
- }
|
|
|
|
|
|
public <Response> Response post(RequestUrl requestUrl, Object body, Class<Response> responseClass, HttpHeaders headers) {
|
|
|
Map<String, String> message = Map.of("message", JSON.toJSONString(body));
|
|
|
@@ -57,9 +48,12 @@ public class AnShengCrawlerRequestComponent {
|
|
|
* @param request 获取验证码参数
|
|
|
* @return 验证码参数
|
|
|
*/
|
|
|
- public AnShengCrawlerVerifyResponse.DataDTO crawlerVerify(AnShengCrawlerVerifyRequest request) {
|
|
|
- AnShengCrawlerVerifyResponse response = request(RequestUrl.MFA_VERIFY, request, AnShengCrawlerVerifyResponse.class, null);
|
|
|
- return response.getData();
|
|
|
+ public AnShengCrawlerVerifyResponse.DataDTO crawlerVerify(AnShengCrawlerVerifyRequest request, final HttpHeaders headers) {
|
|
|
+ ResponseEntity<AnShengCrawlerVerifyResponse> verifyRequest = requestComponent.objectPost(RequestUrl.MFA_VERIFY.getRequestUrl(anShengCrawlerProperties.getUrl()), request, AnShengCrawlerVerifyResponse.class, headers);
|
|
|
+ List<String> strings = verifyRequest.getHeaders().get(CookieParseUtils.SET_COOKIE);
|
|
|
+ String cookie = CookieParseUtils.listToStrCookie(strings);
|
|
|
+ headers.set(CookieParseUtils.COOKIE, cookie);
|
|
|
+ return verifyRequest.getBody().getData();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -80,10 +74,24 @@ public class AnShengCrawlerRequestComponent {
|
|
|
* @param request 登录请求
|
|
|
* @param headers 请求头
|
|
|
*/
|
|
|
- public HttpHeaders login(AnShengCrawlerCheckLoginRequest request, HttpHeaders headers) {
|
|
|
+ public void login(AnShengCrawlerCheckLoginRequest request, HttpHeaders headers) {
|
|
|
MultiValueMap<String, Object> multiValueMap = RequestObjectConversion.convertMultiValueMap(request);
|
|
|
ResponseEntity<String> response = requestComponent.formData(RequestUrl.LOGIN.getRequestUrl(anShengCrawlerProperties.getUrl()), multiValueMap, String.class, headers);
|
|
|
- return response.getHeaders();
|
|
|
+ conversionCookies(headers, response, CookieParseUtils.JSESSIONID);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void conversionCookies(HttpHeaders headers, ResponseEntity<String> response, String cookieName) {
|
|
|
+ List<String> strings = response.getHeaders().get(CookieParseUtils.SET_COOKIE);
|
|
|
+ Map<String, String> loginCookie = CookieParseUtils.parseCookie(strings);
|
|
|
+ Map<String, String> rawCookie = CookieParseUtils.parseCookie(Objects.requireNonNull(headers.get(CookieParseUtils.COOKIE)).get(0));
|
|
|
+
|
|
|
+ if(CookieParseUtils.AUTHORIZATION.equals(cookieName)){
|
|
|
+ rawCookie.put(cookieName, CookieParseUtils.BEARER + loginCookie.get(cookieName));
|
|
|
+ }else {
|
|
|
+ rawCookie.put(cookieName, loginCookie.get(cookieName));
|
|
|
+ }
|
|
|
+ String cookie = CookieParseUtils.toStringCookie(rawCookie);
|
|
|
+ headers.set(CookieParseUtils.COOKIE, cookie);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -92,9 +100,15 @@ public class AnShengCrawlerRequestComponent {
|
|
|
* @param headers 请求头
|
|
|
*/
|
|
|
public void authorize(HttpHeaders headers) {
|
|
|
- requestComponent.baseGet(RequestUrl.OAUTH_AUTHORIZE.getRequestUrl(anShengCrawlerProperties.getUrl()), new LinkedMultiValueMap<>(), String.class, headers);
|
|
|
+ LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
|
+ map.put("client_id", List.of("ast-prod"));
|
|
|
+ map.put("response_type", List.of("code"));
|
|
|
+
|
|
|
+ ResponseEntity<String> response = requestComponent.baseGet(RequestUrl.OAUTH_AUTHORIZE.getRequestUrl(anShengCrawlerProperties.getUrl()), new LinkedMultiValueMap<>(), String.class, headers);
|
|
|
+ conversionCookies(headers, response, CookieParseUtils.AUTHORIZATION);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 获取归属信息
|
|
|
*
|