修复本科生登录时Cookie检查条件判断错误的问题;修复由于builder而导致的HttpRequest产生的null问题

old-package
lensfrex 2 years ago
parent ebf06af1a7
commit 0c47043757
Signed by: lensfrex
GPG Key ID: 947ADABD8533C476
  1. 12
      mywust-core/src/main/java/cn/linghang/mywust/core/service/auth/UndergraduateLogin.java
  2. 37
      mywust-network/src/main/java/cn/linghang/mywust/network/RequestClientOption.java
  3. 6
      mywust-network/src/main/java/cn/linghang/mywust/network/entitys/HttpRequest.java

@ -35,11 +35,17 @@ public class UndergraduateLogin {
HttpResponse sessionResponse = requester.get(sessionRequest, requestOption); HttpResponse sessionResponse = requester.get(sessionRequest, requestOption);
String cookies = sessionResponse.getCookies(); String cookies = sessionResponse.getCookies();
if (checkCookies(cookies, requestOption)) { if (!roughCheckCookie(cookies)) {
log.warn("[mywust]: Cookie检查不通过:{}", cookies); log.error("[mywust]: Cookie粗查不通过:{}", cookies);
throw new ApiException(ApiException.Code.UNKNOWN_EXCEPTION, "登录获取的Cookie无效"); throw new ApiException(ApiException.Code.UNKNOWN_EXCEPTION, "登录获取的Cookie无效");
} }
// 检查Cookie是否真正可用,同时请求一次任意接口使后续接口能够正确响应
// 拿到Cookie后调用的第一个接口会产生302/301跳转,需要再次调用才能正确响应
if (!checkCookies(cookies, requestOption)) {
log.warn("[mywust]: Cookie检查不通过:{}", cookies);
}
return cookies; return cookies;
} }
@ -92,7 +98,7 @@ public class UndergraduateLogin {
} }
private boolean roughCheckCookie(String cookies) { private boolean roughCheckCookie(String cookies) {
return cookies == null || !cookies.contains("JSESSIONID") || !cookies.contains("SERVERID"); return cookies != null && cookies.contains("JSESSIONID") && cookies.contains("SERVERID");
} }
private static final int COOKIES_ERROR_RESPONSE_LENGTH = private static final int COOKIES_ERROR_RESPONSE_LENGTH =

@ -4,7 +4,6 @@ import lombok.Builder;
import lombok.Data; import lombok.Data;
@Data @Data
@Builder
public class RequestClientOption { public class RequestClientOption {
private Proxy proxy; private Proxy proxy;
private long timeout; private long timeout;
@ -67,4 +66,40 @@ public class RequestClientOption {
@Override @Override
public void setFallowUrlRedirect(boolean fallowUrlRedirect) {} public void setFallowUrlRedirect(boolean fallowUrlRedirect) {}
} }
public static RequestClientOptionBuilder builder() {
return new RequestClientOptionBuilder();
}
public static final class RequestClientOptionBuilder {
private final RequestClientOption requestClientOption;
private RequestClientOptionBuilder() {
requestClientOption = new RequestClientOption();
}
public RequestClientOptionBuilder proxy(Proxy proxy) {
requestClientOption.setProxy(proxy);
return this;
}
public RequestClientOptionBuilder timeout(long timeout) {
requestClientOption.setTimeout(timeout);
return this;
}
public RequestClientOptionBuilder fallowUrlRedirect(boolean fallowUrlRedirect) {
requestClientOption.setFallowUrlRedirect(fallowUrlRedirect);
return this;
}
public RequestClientOptionBuilder ignoreSSLError(boolean ignoreSSLError) {
requestClientOption.setIgnoreSSLError(ignoreSSLError);
return this;
}
public RequestClientOption build() {
return requestClientOption;
}
}
} }

@ -38,9 +38,13 @@ public class HttpRequest {
public HttpRequest(URL url, Map<String, String> headers, String cookies, byte[] data) { public HttpRequest(URL url, Map<String, String> headers, String cookies, byte[] data) {
this.url = url; this.url = url;
this.headers = headers;
this.cookies = cookies; this.cookies = cookies;
this.data = data; this.data = data;
this.headers = headers;
if (headers == null) {
this.headers = new HashMap<>(DEFAULT_HEADERS);
}
} }
public HttpRequest addHeaders(Map<String, String> headers) { public HttpRequest addHeaders(Map<String, String> headers) {

Loading…
Cancel
Save