日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

JAVA——基于HttpClient的正方教务系统[1999-2020]模拟登录基本解决方案

發布時間:2024/10/5 windows 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA——基于HttpClient的正方教务系统[1999-2020]模拟登录基本解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介?

通過HttpClient獲取網頁數據源,通過Jsoup解析數據。先模擬登錄,再獲取信息。模擬瀏覽器正常操作,封裝請求頭信息獲取SESSIONID。模擬登錄成功后切勿斷開會話,依賴登錄請求得到的Cookie進行二次請求。請求信息時需打開谷歌瀏覽器或Fiddler抓包查看參數及請求頭信息。

Maven

<dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>4.8.0</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20160810</version></dependency>

基本步驟

1.獲取exponent、modulus生成公鑰進行密碼加密

2.爬蟲爬取csrftoken校驗

3.添加Post參數模擬瀏覽器登錄獲取Cookie(SESSIONID)

4.二次請求

源代碼

Util

package club.zstuca.util;import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.RSAPublicKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.HashMap; import java.util.Map;import javax.crypto.Cipher;public class RSAUtil {private static Map<Integer, String> keyMap = new HashMap<Integer, String>(); //用于封裝隨機產生的公鑰與私鑰/** * 隨機生成密鑰對 * @throws NoSuchAlgorithmException */ public static void genKeyPair() throws NoSuchAlgorithmException { // KeyPairGenerator類用于生成公鑰和私鑰對,基于RSA算法生成對象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); // 初始化密鑰對生成器,密鑰大小為96-1024位 keyPairGen.initialize(1024,new SecureRandom()); // 生成一個密鑰對,保存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私鑰 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公鑰 String publicKeyString = new String(Base64.getEncoder().encode(publicKey.getEncoded())); // 得到私鑰字符串 String privateKeyString = new String(Base64.getEncoder().encode((privateKey.getEncoded()))); // 將公鑰和私鑰保存到MapkeyMap.put(0,publicKeyString); //0表示公鑰keyMap.put(1,privateKeyString); //1表示私鑰} /** * RSA公鑰加密 * * @param str * 加密字符串* @param publicKey * 公鑰 * @return 密文 * @throws Exception * 加密過程中的異常信息 */ public static String encryptByX509EncodedKeySpec( String str, String publicKey ) throws Exception{//base64編碼的公鑰byte[] decoded = Base64.getDecoder().decode(publicKey.getBytes("UTF-8"));RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));//RSA加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, pubKey);String outStr = Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes("UTF-8")));return outStr;} /** * RSA公鑰加密 * * @param str * 加密字符串* @param modulus * 模數 * @param publicExponent* 公眾指數 * @return 密文 * @throws Exception * 加密過程中的異常信息 */ public static String encryptByRSAPublicKeySpec( String str, String modulus, String publicExponent ) throws Exception{//base64編碼的公鑰RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(new BigInteger(1,Base64.getDecoder().decode(modulus.getBytes("UTF-8"))),new BigInteger(1,Base64.getDecoder().decode(publicExponent.getBytes("UTF-8")))));//RSA加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, pubKey);String outStr = Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes("UTF-8")));return outStr;} /** * RSA私鑰解密* * @param str * 加密字符串* @param privateKey * 私鑰 * @return 銘文* @throws Exception * 解密過程中的異常信息 */ public static String decryptByPKCS8EncodedKeySpec(String str, String privateKey) throws Exception{//64位解碼加密后的字符串byte[] inputByte = Base64.getDecoder().decode(str.getBytes("UTF-8"));//base64編碼的私鑰byte[] decoded = Base64.getDecoder().decode(privateKey.getBytes("UTF-8")); RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); //RSA解密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, priKey);String outStr = new String(cipher.doFinal(inputByte));return outStr;} }

?Main?

package club.zstuca.tools;import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document;import club.zstuca.util.RSAUtil;import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List;public class ZFsoft {public static void main(String[] args) {ZFsoft zFsoft=new ZFsoft();//zFsoft.login("2018329621200","xxxx");List<Score>scoreList=zFsoft.login("2018329621200","xxxx").checkScore("","");for(Score score:scoreList){System.out.println(score);}System.out.println(scoreList.size());}private final String LOGIN_URL="http://10.11.247.52/jwglxt/xtgl/login_slogin.html?language=zh_CN&_t=";private final String PUBLICKEY_URL="http://10.11.247.52/jwglxt/xtgl/login_getPublicKey.html?time=";private final String CHECK_SCORE_URL="http://10.11.247.52/jwglxt/cjcx/cjcx_cxDgXscj.html?doType=query&gnmkdm=N305005";private CloseableHttpClient httpClient;private BasicCookieStore basicCookieStore;public ZFsoft(){basicCookieStore=new BasicCookieStore();httpClient= HttpClients.custom().setDefaultCookieStore(basicCookieStore).build();}/*** 密碼加密 RSA* @param password* @return*/private String encryp(String password){//一、獲取 exponent modulus 生成公鑰String exponent=null,modulus=null;HttpGet gpkHttpGet=new HttpGet(PUBLICKEY_URL+new Date().getTime());gpkHttpGet.setHeader("Accept","application/json, text/javascript, */*; q=0.01");gpkHttpGet.setHeader("Accept-Encoding","gzip, deflate");gpkHttpGet.setHeader("Accept-Language","zh-CN,zh;q=0.9");gpkHttpGet.setHeader("Connection","keep-alive");gpkHttpGet.setHeader("Host","jwgl.hebtu.edu.cn");gpkHttpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");gpkHttpGet.setHeader("X-Requested-With","XMLHttpRequest");CloseableHttpResponse gpkResponse=null;try {gpkResponse = httpClient.execute(gpkHttpGet);if (gpkResponse.getStatusLine().getStatusCode() == 200) {String emJson = EntityUtils.toString(gpkResponse.getEntity(), "utf8");JSONObject jsonObject = new JSONObject(emJson);exponent = jsonObject.getString("exponent");modulus = jsonObject.getString("modulus");}}catch (Exception e){e.printStackTrace();}finally {try {gpkResponse.close();} catch (IOException e) {e.printStackTrace();}}//二、根據公鑰進行密碼加密System.out.println(modulus);System.out.println(exponent);System.out.println(password);try {password=RSAUtil.encryptByRSAPublicKeySpec(password, modulus, exponent);} catch (Exception e) {// TODO 自動生成的 catch 塊e.printStackTrace();}System.out.println(password);return password;}/*** 獲取Token* @param timestamp* @return*/private String crawlCsrfToken(String timestamp){String csrftoken=null;HttpGet csrftokenHttpGet=new HttpGet(LOGIN_URL+timestamp);CloseableHttpResponse csrftokenResponse=null;try {csrftokenResponse = httpClient.execute(csrftokenHttpGet);if (csrftokenResponse.getStatusLine().getStatusCode() == 200) {Document csrftokenDoc = Jsoup.parse(EntityUtils.toString(csrftokenResponse.getEntity(), "utf8"));csrftoken = csrftokenDoc.select(".col-sm-4").select(".sl_log_rt").select("input[id=csrftoken]").first().attr("value");return csrftoken;}}catch (Exception e){e.printStackTrace();}finally {try {csrftokenResponse.close();} catch (IOException e) {e.printStackTrace();}}return null;}/*** 模擬登錄* @param username* @param password* @return*/public ZFsoft login(String username,String password){String timestamp=""+new Date().getTime();HttpPost loginHttpPost=new HttpPost(LOGIN_URL+timestamp);loginHttpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");loginHttpPost.setHeader("Accept-Encoding","gzip, deflate");loginHttpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9");loginHttpPost.setHeader("Cache-Control","max-age=0");loginHttpPost.setHeader("Connection","keep-alive");loginHttpPost.setHeader("Content-Type","application/x-www-form-urlencoded");loginHttpPost.setHeader("Host","jwgl.hebtu.edu.cn");loginHttpPost.setHeader("Origin","http://jwgl.hebtu.edu.cn");loginHttpPost.setHeader("Upgrade-Insecure-Requests","1");loginHttpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");List<NameValuePair> loginParams=new ArrayList<NameValuePair>();password=this.encryp(password);String csrftoken=this.crawlCsrfToken(timestamp);loginParams.add(new BasicNameValuePair("csrftoken",csrftoken));loginParams.add(new BasicNameValuePair("yhm",username));loginParams.add(new BasicNameValuePair("mm",password));loginParams.add(new BasicNameValuePair("mm",password));CloseableHttpResponse loginResponse=null;try {loginHttpPost.setEntity(new UrlEncodedFormEntity(loginParams, "utf8"));loginResponse = httpClient.execute(loginHttpPost);List<Cookie>cookies=basicCookieStore.getCookies();if(cookies.isEmpty()){System.out.println("The Cookie Is None.");}else {for(Cookie cookie:cookies){}}}catch (Exception e){e.printStackTrace();}return this;}/*** 查看成績* @param xnm* @param xqm* @return*/public List<Score> checkScore(String xnm,String xqm){HttpPost scoreHttpPost=new HttpPost(CHECK_SCORE_URL);scoreHttpPost.setHeader("Accept","application/json, text/javascript, */*; q=0.01");scoreHttpPost.setHeader("Accept-Encoding","gzip, deflate");scoreHttpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9");scoreHttpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");scoreHttpPost.setHeader("Host","jwgl.hebtu.edu.cn");scoreHttpPost.setHeader("Origin","http://jwgl.hebtu.edu.cn");scoreHttpPost.setHeader("Proxy-Connection","keep-alive");scoreHttpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");scoreHttpPost.setHeader("X-Requested-With","XMLHttpRequest");List<NameValuePair>scoreParams=new ArrayList<NameValuePair>();scoreParams.add(new BasicNameValuePair("xnm",xnm));scoreParams.add(new BasicNameValuePair("xqm",xqm));scoreParams.add(new BasicNameValuePair("_search","false"));scoreParams.add(new BasicNameValuePair("nd",""+new Date().getTime()));scoreParams.add(new BasicNameValuePair("queryModel.showCount","100"));scoreParams.add(new BasicNameValuePair("queryModel.currentPage","1"));scoreParams.add(new BasicNameValuePair("queryModel.sortName",""));scoreParams.add(new BasicNameValuePair("queryModel.sortOrder","asc"));scoreParams.add(new BasicNameValuePair("time","1"));try {scoreHttpPost.setEntity(new UrlEncodedFormEntity(scoreParams, "utf8"));CloseableHttpResponse scoreResponse = httpClient.execute(scoreHttpPost);if (scoreResponse.getStatusLine().getStatusCode() == 200) {if (scoreResponse.getEntity() != null) {String scoreJson = EntityUtils.toString(scoreResponse.getEntity(), "utf8");System.out.print(scoreJson);JSONObject jsonObject = new JSONObject(scoreJson);JSONArray jsonArray = jsonObject.getJSONArray("items");List<Score>scoreList=new ArrayList<Score>();for (int i = 0; i < jsonArray.length(); ++i) {JSONObject item = (JSONObject) jsonArray.get(i);Score score=new Score();score.setXm(item.getString("xm"));score.setKcmc(item.getString("kcmc"));score.setBj(item.getString("bj"));score.setCj(item.getString("cj"));String jd = "0";try {jd = item.getString("jd");} catch (Exception e) {e.printStackTrace();}score.setJd(jd);score.setJgmc(item.getString("jgmc"));score.setKch(item.getString("kch"));score.setKcxzmc(item.getString("kcxzmc"));score.setKsxz(item.getString("ksxz"));scoreList.add(score);}return scoreList;}}}catch (Exception e){e.printStackTrace();}return null;} }

運行結果

?

參考文章

https://www.cnblogs.com/hbsdljz/p/10874099.html

https://shentuzhigang.blog.csdn.net/article/details/103995547

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的JAVA——基于HttpClient的正方教务系统[1999-2020]模拟登录基本解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。