研究生登录参数调整为动态获取

old-package
lensfrex 2 years ago
parent 6eaeadb11d
commit e4473af4d7
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 57
      mywust-core/src/main/java/cn/linghang/mywust/core/request/factory/graduate/GraduateRequestFactory.java
  2. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/request/factory/physics/PhysicsSystemRequestFactory.java
  3. 17
      mywust-core/src/main/java/cn/linghang/mywust/core/request/service/auth/GraduateLogin.java
  4. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/request/service/physics/PhysicsScoreApiService.java
  5. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/util/PageFormExtractor.java

@ -3,8 +3,11 @@ package cn.linghang.mywust.core.request.factory.graduate;
import cn.linghang.mywust.captcha.SolvedImageCaptcha; import cn.linghang.mywust.captcha.SolvedImageCaptcha;
import cn.linghang.mywust.core.api.GraduateUrls; import cn.linghang.mywust.core.api.GraduateUrls;
import cn.linghang.mywust.core.request.factory.RequestFactory; import cn.linghang.mywust.core.request.factory.RequestFactory;
import cn.linghang.mywust.core.util.PageFormExtractor;
import cn.linghang.mywust.network.entitys.HttpRequest; import cn.linghang.mywust.network.entitys.HttpRequest;
import cn.linghang.mywust.util.StringUtil; import cn.linghang.mywust.util.StringUtil;
import lombok.Builder;
import lombok.Data;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
@ -15,15 +18,21 @@ public class GraduateRequestFactory extends RequestFactory {
return makeHttpRequest(GraduateUrls.GRADUATE_CAPTCHA_API); return makeHttpRequest(GraduateUrls.GRADUATE_CAPTCHA_API);
} }
private static final Map<String,String> LOGIN_CONST_PARAMS = new HashMap<>(5); public static HttpRequest captchaRequest(String cookie) {
return makeHttpRequest(GraduateUrls.GRADUATE_CAPTCHA_API, null, cookie);
}
private static final Map<String, String> LOGIN_CONST_PARAMS = new HashMap<>(5);
static { static {
LOGIN_CONST_PARAMS.put("__VIEWSTATE", "/wEPDwUENTM4MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFEl9jdGwwOkltYWdlQnV0dG9uMQUSX2N0bDA6SW1hZ2VCdXR0b24yXnZLY54iSWFQ6B2yKH0EisNqU3/eKWEJPibQUElowzU="); LOGIN_CONST_PARAMS.put("__VIEWSTATE", "/wEPDwUENTM4MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFEl9jdGwwOkltYWdlQnV0dG9uMQUSX2N0bDA6SW1hZ2VCdXR0b24yocdtCYu4PMj471MTbP5cXU8bY4gtaO8szHfQ9BzPZdU=");
LOGIN_CONST_PARAMS.put("__EVENTVALIDATION", "/wEdAAYVkBquZFuFxLpraDgB64v+UDagjadrq+xukJizXKfuf485DjYUnSc4B1y8D5WGXeCaN+cQ7B52HzGj0ueO5HRlbdfASR9MjKgO1uRUmJC5kWf476Bpzok4CsBoBh+4Dc7vLkoP0tXUghu7H8qg++pYHeGok+i2xPFtG5oj0qB2dw=="); LOGIN_CONST_PARAMS.put("__EVENTVALIDATION", "/wEdAAZILdedbS+/UZ9vGGceLDGGUDagjadrq+xukJizXKfuf485DjYUnSc4B1y8D5WGXeCaN+cQ7B52HzGj0ueO5HRlbdfASR9MjKgO1uRUmJC5kWf476Bpzok4CsBoBh+4Dc7g7ZIrePIEkbgMpk1NAfZEXHl1KcVqFDpYCaSt9iZO5w==");
LOGIN_CONST_PARAMS.put("__VIEWSTATEGENERATOR", "496CE0B8"); LOGIN_CONST_PARAMS.put("__VIEWSTATEGENERATOR", "496CE0B8");
LOGIN_CONST_PARAMS.put("_ctl0:ImageButton1.x", "39"); LOGIN_CONST_PARAMS.put("_ctl0:ImageButton1.x", "39");
LOGIN_CONST_PARAMS.put("_ctl0:ImageButton1.y", "10"); LOGIN_CONST_PARAMS.put("_ctl0:ImageButton1.y", "10");
} }
@Deprecated
public static HttpRequest loginRequest(String username, String password, SolvedImageCaptcha captcha) { public static HttpRequest loginRequest(String username, String password, SolvedImageCaptcha captcha) {
Map<String, String> params = new HashMap<>(7); Map<String, String> params = new HashMap<>(7);
params.putAll(LOGIN_CONST_PARAMS); params.putAll(LOGIN_CONST_PARAMS);
@ -36,6 +45,48 @@ public class GraduateRequestFactory extends RequestFactory {
return makeHttpRequest(GraduateUrls.GRADUATE_LOGIN_API, requestData, captcha.getBindInfo()); return makeHttpRequest(GraduateUrls.GRADUATE_LOGIN_API, requestData, captcha.getBindInfo());
} }
@Data
@Builder
public static class LoginPageParams {
private final String viewState;
private final String eventValidation;
private final String vewStateGenerator;
}
public static HttpRequest loginRequest(String username, String password, LoginPageParams loginPageParams, SolvedImageCaptcha captcha) {
Map<String, String> params = new HashMap<>(7);
params.putAll(LOGIN_CONST_PARAMS);
params.put("__VIEWSTATE", loginPageParams.viewState);
params.put("__EVENTVALIDATION", loginPageParams.eventValidation);
params.put("__VIEWSTATEGENERATOR", loginPageParams.vewStateGenerator);
params.put("_ctl0:txtusername", username);
params.put("_ctl0:txtpassword", password);
params.put("_ctl0:txtyzm", captcha.getResult());
byte[] requestData = StringUtil.generateQueryString(params).getBytes(StandardCharsets.UTF_8);
return makeHttpRequest(GraduateUrls.GRADUATE_LOGIN_API, requestData, captcha.getBindInfo());
}
public static HttpRequest loginRequest(String username, String password, String loginPage, SolvedImageCaptcha captcha) {
Map<String, String> params = new HashMap<>(7);
params.putAll(LOGIN_CONST_PARAMS);
Map<String, String> form = PageFormExtractor.searchAllParams(loginPage);
params.put("__VIEWSTATE", form.get("__VIEWSTATE"));
params.put("__EVENTVALIDATION", form.get("__EVENTVALIDATION"));
params.put("__VIEWSTATEGENERATOR", form.get("__VIEWSTATEGENERATOR"));
params.put("_ctl0:txtusername", username);
params.put("_ctl0:txtpassword", password);
params.put("_ctl0:txtyzm", captcha.getResult());
byte[] requestData = StringUtil.generateQueryString(params).getBytes(StandardCharsets.UTF_8);
return makeHttpRequest(GraduateUrls.GRADUATE_LOGIN_API, requestData, captcha.getBindInfo());
}
public static HttpRequest studentInfoRequest(String cookie) { public static HttpRequest studentInfoRequest(String cookie) {
return makeHttpRequest(GraduateUrls.GRADUATE_STUDENT_INFO_API, null, cookie); return makeHttpRequest(GraduateUrls.GRADUATE_STUDENT_INFO_API, null, cookie);
} }

@ -15,7 +15,7 @@ public class PhysicsSystemRequestFactory extends RequestFactory {
} }
public static HttpRequest loginCookiesRequest(String username, String password, String loginIndexHtml) { public static HttpRequest loginCookiesRequest(String username, String password, String loginIndexHtml) {
Map<String, String> params = PageFormExtractor.extractAllParams(loginIndexHtml); Map<String, String> params = PageFormExtractor.searchAllParams(loginIndexHtml);
String viewState = params.get("__VIEWSTATE"); String viewState = params.get("__VIEWSTATE");
String queryData = StringUtil.generateQueryString(makeLoginQueryParam(username, password, viewState)); String queryData = StringUtil.generateQueryString(makeLoginQueryParam(username, password, viewState));

@ -30,12 +30,17 @@ public class GraduateLogin {
} }
public String getLoginCookie(String username, String password, RequestClientOption option) throws IOException, ApiException { public String getLoginCookie(String username, String password, RequestClientOption option) throws IOException, ApiException {
// 请求获取验证码 HttpRequest loginIndexRequest = RequestFactory.makeHttpRequest(GraduateUrls.GRADUATE_LOGIN_API);
HttpRequest captchaImageRequest = GraduateRequestFactory.captchaRequest(); HttpResponse loginIndexResponse = requester.get(loginIndexRequest, option);
String loginCookie = loginIndexResponse.getCookies();
// 用得到的Cookie请求获取验证码
HttpRequest captchaImageRequest = GraduateRequestFactory.captchaRequest(loginCookie);
HttpResponse captchaImageResponse = requester.get(captchaImageRequest, option); HttpResponse captchaImageResponse = requester.get(captchaImageRequest, option);
UnsolvedImageCaptcha unsolvedImageCaptcha = new UnsolvedImageCaptcha(); UnsolvedImageCaptcha unsolvedImageCaptcha = new UnsolvedImageCaptcha();
unsolvedImageCaptcha.setBindInfo(captchaImageResponse.getCookies()); unsolvedImageCaptcha.setBindInfo(loginCookie);
byte[] processedImage = ImageUtil.process(captchaImageResponse.getBody()); byte[] processedImage = ImageUtil.process(captchaImageResponse.getBody());
unsolvedImageCaptcha.setImage(processedImage); unsolvedImageCaptcha.setImage(processedImage);
@ -44,7 +49,7 @@ public class GraduateLogin {
SolvedImageCaptcha solvedImageCaptcha = captchaSolver.solve(unsolvedImageCaptcha); SolvedImageCaptcha solvedImageCaptcha = captchaSolver.solve(unsolvedImageCaptcha);
// 进行登录 // 进行登录
HttpRequest loginRequest = GraduateRequestFactory.loginRequest(username, password, solvedImageCaptcha); HttpRequest loginRequest = GraduateRequestFactory.loginRequest(username, password, loginIndexResponse.getStringBody(), solvedImageCaptcha);
HttpResponse loginResponse = requester.post(loginRequest, option); HttpResponse loginResponse = requester.post(loginRequest, option);
// 登陆成功,应该会是302跳转,不是的话多半是认证错误 // 登陆成功,应该会是302跳转,不是的话多半是认证错误
@ -52,8 +57,8 @@ public class GraduateLogin {
throw new ApiException(ApiException.Code.GRADUATE_PASSWORD_WRONG); throw new ApiException(ApiException.Code.GRADUATE_PASSWORD_WRONG);
} }
// 使用当初通过验证码得到的cookie来作为登录cookie,至于是否真正可行待验证 // 使用首页第一次访问得到的cookie来作为登录cookie
return captchaImageResponse.getCookies(); return loginCookie;
} }
public void checkCookies(String cookie, RequestClientOption option) throws ApiException, IOException { public void checkCookies(String cookie, RequestClientOption option) throws ApiException, IOException {

@ -38,7 +38,7 @@ public class PhysicsScoreApiService extends PhysicsApiServiceBase {
PhysicsScoreListPageParser.PhysicsScoreListPageParseResult pageParseResult = scoreListPageParser.parse(scoreListPage); PhysicsScoreListPageParser.PhysicsScoreListPageParseResult pageParseResult = scoreListPageParser.parse(scoreListPage);
Map<String, String> pageParams = PageFormExtractor.extractAllParams(scoreListPage); Map<String, String> pageParams = PageFormExtractor.searchAllParams(scoreListPage);
pageParams.put("ID_PEE63$ddlxq", pageParseResult.getTerm()); pageParams.put("ID_PEE63$ddlxq", pageParseResult.getTerm());
List<String> courseIds = pageParseResult.getCourseIds(); List<String> courseIds = pageParseResult.getCourseIds();

@ -30,7 +30,7 @@ public class PageFormExtractor {
private static final Pattern formPattern = Pattern.compile("name=\"(?<name>.*?)\"(.*?)value=\"(?<value>.*?)\""); private static final Pattern formPattern = Pattern.compile("name=\"(?<name>.*?)\"(.*?)value=\"(?<value>.*?)\"");
public static Map<String, String> extractAllParams(String wholeHtml) { public static Map<String, String> searchAllParams(String wholeHtml) {
Map<String, String> params = new HashMap<>(16); Map<String, String> params = new HashMap<>(16);
Matcher matcher = formPattern.matcher(wholeHtml); Matcher matcher = formPattern.matcher(wholeHtml);
while (matcher.find()) { while (matcher.find()) {

Loading…
Cancel
Save