parent
6f35b17acc
commit
c0d1d11693
@ -0,0 +1,9 @@ |
||||
package cn.linghang.mywust.core.api; |
||||
|
||||
public class Library { |
||||
public static final String LIBRARY_SESSION_COOKIE_API = "https://libsys.wust.edu.cn/meta-local/opac/cas/rosetta?ticket=%s"; |
||||
|
||||
public static final String LIBRARY_INDEX_URL = "https://libsys.wust.edu.cn/meta-local/opac/cas/rosetta"; |
||||
|
||||
public static final String LIBRARY_COOKIE_TEST_URL = "https://libsys.wust.edu.cn/meta-local/opac/users/info"; |
||||
} |
@ -0,0 +1,37 @@ |
||||
package cn.linghang.mywust.core.service.auth; |
||||
|
||||
import cn.linghang.mywust.network.HttpRequest; |
||||
import cn.linghang.mywust.util.StringUtil; |
||||
|
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
public class AuthRequestFactory { |
||||
public static HttpRequest unionLoginTGTRequest(String username, String password, String service) { |
||||
Map<String, String> requestForm = new HashMap<>(4); |
||||
requestForm.put("username", username); |
||||
requestForm.put("password", password); |
||||
requestForm.put("service", service); |
||||
requestForm.put("loginType", ""); |
||||
|
||||
String queryString = StringUtil.generateQueryString(requestForm); |
||||
|
||||
HttpRequest request = new HttpRequest(); |
||||
request.setData(queryString.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
return request; |
||||
} |
||||
|
||||
public static HttpRequest loginTicketRequest(String service) { |
||||
Map<String, String> requestForm = new HashMap<>(1); |
||||
requestForm.put("service", service); |
||||
|
||||
String queryString = StringUtil.generateQueryString(requestForm); |
||||
|
||||
HttpRequest request = new HttpRequest(); |
||||
request.setData(queryString.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
return request; |
||||
} |
||||
} |
@ -0,0 +1,14 @@ |
||||
package cn.linghang.mywust.core.service.library; |
||||
|
||||
import cn.linghang.mywust.network.HttpRequest; |
||||
import cn.linghang.mywust.network.RequestFactory; |
||||
|
||||
public class LibraryAuthRequestFactory extends RequestFactory { |
||||
public static HttpRequest sessionCookieRequest() { |
||||
return DEFAULT_HTTP_REQUEST; |
||||
} |
||||
|
||||
public static HttpRequest indexRequest() { |
||||
return DEFAULT_HTTP_REQUEST; |
||||
} |
||||
} |
@ -0,0 +1,57 @@ |
||||
package cn.linghang.mywust.core.service.library; |
||||
|
||||
import cn.linghang.mywust.core.api.Bkjx; |
||||
import cn.linghang.mywust.core.api.Library; |
||||
import cn.linghang.mywust.core.api.UnionAuth; |
||||
import cn.linghang.mywust.core.exception.BasicException; |
||||
import cn.linghang.mywust.core.service.auth.UnionLogin; |
||||
import cn.linghang.mywust.core.service.undergraduate.BkjxAuthRequestFactory; |
||||
import cn.linghang.mywust.network.HttpRequest; |
||||
import cn.linghang.mywust.network.HttpResponse; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
|
||||
public class LibraryLogin { |
||||
private final Requester requester; |
||||
|
||||
private final UnionLogin unionLogin; |
||||
|
||||
public LibraryLogin(Requester requester, UnionLogin unionLogin) { |
||||
this.requester = requester; |
||||
this.unionLogin = unionLogin; |
||||
} |
||||
|
||||
public String getLibraryLoginCookie(String username, String password, RequestClientOption requestOption) throws BasicException, IOException { |
||||
String serviceTicket = unionLogin.getServiceTicket(username, password, UnionAuth.LIBRARY_SSO_SERVICE, requestOption); |
||||
|
||||
// 获取登录cookie(session)
|
||||
HttpRequest sessionRequest = LibraryAuthRequestFactory.sessionCookieRequest(); |
||||
HttpResponse sessionResponse = requester.get(new URL(String.format(Library.LIBRARY_SESSION_COOKIE_API, serviceTicket)), sessionRequest, requestOption); |
||||
String cookies = sessionResponse.getCookies(); |
||||
|
||||
// 请求一次首页
|
||||
HttpRequest indexRequest = LibraryAuthRequestFactory.indexRequest(); |
||||
indexRequest.setCookies(cookies); |
||||
requester.get(new URL(Library.LIBRARY_INDEX_URL), indexRequest, requestOption); |
||||
|
||||
return cookies; |
||||
} |
||||
|
||||
public boolean checkCookie(String cookies) throws IOException { |
||||
RequestClientOption option = RequestClientOption.DEFAULT_OPTION; |
||||
|
||||
HttpRequest testRequest = LibraryAuthRequestFactory.getDefaultHttpRequest(); |
||||
testRequest.setCookies(cookies); |
||||
HttpResponse testResponse = requester.get(new URL(Library.LIBRARY_COOKIE_TEST_URL), testRequest, option); |
||||
|
||||
// 响应居然是JSON,好评!
|
||||
// 但是我们只要看有没有Unauthorized关键字就行了
|
||||
// 未认证的话是Unauthorized(如果人家没有抽风改掉的话)
|
||||
byte[] responseData = testResponse.getBody(); |
||||
return responseData != null && !new String(responseData).equalsIgnoreCase("Unauthorized"); |
||||
} |
||||
} |
@ -1,45 +1,21 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.network.HttpRequest; |
||||
import cn.linghang.mywust.network.RequestFactory; |
||||
import cn.linghang.mywust.util.StringUtil; |
||||
|
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
public class AuthRequestFactory { |
||||
public static HttpRequest unionLoginTGTRequest(String username, String password, String service) { |
||||
Map<String, String> requestForm = new HashMap<>(4); |
||||
requestForm.put("username", username); |
||||
requestForm.put("password", password); |
||||
requestForm.put("service", service); |
||||
requestForm.put("loginType", ""); |
||||
|
||||
String queryString = StringUtil.generateQueryString(requestForm); |
||||
|
||||
HttpRequest request = new HttpRequest(); |
||||
request.setData(queryString.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
return request; |
||||
} |
||||
|
||||
public static HttpRequest loginTicketRequest(String service) { |
||||
Map<String, String> requestForm = new HashMap<>(1); |
||||
requestForm.put("service", service); |
||||
|
||||
String queryString = StringUtil.generateQueryString(requestForm); |
||||
|
||||
HttpRequest request = new HttpRequest(); |
||||
request.setData(queryString.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
return request; |
||||
} |
||||
|
||||
public class BkjxAuthRequestFactory extends RequestFactory { |
||||
public static HttpRequest sessionCookieRequest() { |
||||
return DEFAULT_HTTP_REQUEST; |
||||
} |
||||
|
||||
private static final HttpRequest DEFAULT_HTTP_REQUEST = new HttpRequest(); |
||||
public static HttpRequest getDefaultHttpRequest() { |
||||
return DEFAULT_HTTP_REQUEST; |
||||
} |
||||
|
||||
public static class Legacy { |
||||
public static HttpRequest dataStringRequest() { |
@ -0,0 +1,9 @@ |
||||
package cn.linghang.mywust.network; |
||||
|
||||
public class RequestFactory { |
||||
protected static final HttpRequest DEFAULT_HTTP_REQUEST = new HttpRequest(); |
||||
|
||||
public static HttpRequest getDefaultHttpRequest() { |
||||
return DEFAULT_HTTP_REQUEST; |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
import cn.linghang.mywust.core.exception.BasicException; |
||||
import cn.linghang.mywust.core.service.auth.UnionLogin; |
||||
import cn.linghang.mywust.core.service.library.LibraryLogin; |
||||
import cn.linghang.mywust.core.service.undergraduate.JwcLogin; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.okhttp.SimpleOkhttpRequester; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Scanner; |
||||
|
||||
public class LibraryLoginTest { |
||||
public static void main(String[] args) throws BasicException, IOException { |
||||
new LibraryLoginTest().run(); |
||||
} |
||||
|
||||
private void run() throws BasicException, IOException { |
||||
System.out.println("图书馆登陆测试"); |
||||
System.out.println("输入账号(学号)和密码,用“ ”(空格)分割"); |
||||
|
||||
Scanner scanner = new Scanner(System.in); |
||||
|
||||
String input = scanner.nextLine(); |
||||
|
||||
String username = input.split(" ")[0]; |
||||
String password = input.split(" ")[1]; |
||||
|
||||
System.out.println("账号:" + username); |
||||
System.out.println("密码:" + password); |
||||
|
||||
Requester requester = new SimpleOkhttpRequester(); |
||||
LibraryLogin libraryLogin = new LibraryLogin(requester, new UnionLogin(requester)); |
||||
|
||||
RequestClientOption option = RequestClientOption.DEFAULT_OPTION; |
||||
|
||||
String cookies = libraryLogin.getLibraryLoginCookie(username, password, option); |
||||
|
||||
System.out.printf("获取到的cookies: %s \n", cookies); |
||||
|
||||
System.out.printf("检查Cookies: %s", libraryLogin.checkCookie(cookies)); |
||||
} |
||||
} |
Loading…
Reference in new issue