Jmeter結構如圖前端
目的:java
須要在每一個組織下面分別添加5個設備資源mysql
思路:算法
1.先登陸平臺sql
2.進入系統配置頁面數據庫
3.獲取到每一個區域的IDapache
4.在每一個區域下面添加設備資源oracle
重點及難點:sqlserver
1.登陸加密過程post
2.數據庫鏈接獲取到多個參數如何提取
3.如何將獲取到的數據寫入文件中
------------------------------------------------------------------------------------------------
1.登陸
通過與開發溝通了解到,登陸過程使用的是前端js rsa加密。有兩種實現加密的方式:前端js加密或者使用java方法進行加密
使用js加密還未調試經過,下面是用java方式加密的代碼
在網上百度到相應算法以後,實現以下:
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
String RSA_PUB_KEY="${pubkey}";
String KEY_ALGORITHM = "RSA";
String SIGNATURE_ALGORITHM = "MD5withRSA";
int MAX_ENCRYPT_BLOCK = 117;
int MAX_DECRYPT_BLOCK = 128;
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
throws Exception {
byte[] keyBytes = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicK);
int inputLen = encryptedData.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 對數據分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
public static byte[] encryptByPublicKey(byte[] data, String publicKey)
throws Exception {
byte[] keyBytes = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
// 對數據加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicK);
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 對數據分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
String str = "${pwd}";
String result ="";
try {
result = Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY));
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print(result);
vars.put("npsw",result);
return result;
注意:能夠將代碼封裝爲一個類,打包成jar包,放置於lib\ext中
2.經過從數據庫查詢獲取全部區域ID
2.1新建數據庫鏈接
補充:
MySQL數據庫:
1)驅動包:https://mvnrepository.com/artifact/mysql/mysql-connector-java(下載路徑)
2)驅動類名:com.mysql.jdbc.Driver
3)JDBC的URL:jdbc:mysql://IP地址:端口號/數據庫名字
注:端口號缺省爲:3306
SQL server數據庫:
1)驅動包:https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4(下載路徑)
2)驅動類名:com.microsoft.jdbc.sqlserver.SQLServerDriver
3)JDBC的URL:jdbc:microsoft:sqlserver://IP地址:端口號;DatabaseName=數據庫名
4)sqljdbc和sqljdbc4區別:https://blog.csdn.net/cainiao_M/article/details/53404222
注:端口號缺省爲:1433
Oracle數據庫:
1)驅動包:https://mvnrepository.com/artifact/com.oracle/ojdbc6(下載路徑)
2)驅動類名:oracle.jdbc.driver.OracleDriver
3)JDBC的URL:jdbc:oracle:thin:@IP地址:端口號:數據庫名
4)ojdbc6和ojdbc14的區別:ojdbc14.jar(適合java-1.4和1.5),ojdbc6(適合java-1.6)
注:端口號缺省爲:1521 #
HSQLDB
#jdbc.driverClassName=org.hsqldb.jdbcDriver
#jdbc.url=jdbc:hsqldb:hsql://localhost:9001/bookstore
PostgreSQL
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/bookstore
下載對應的驅動包,而後在測試計劃中添加引用,如圖
2.2新建jdbc請求
請求結果以下,須要獲取全部的regionID,而且把它寫入到文件中
代碼以下:
遍歷全部regionID代碼以下:
String model_num =vars.get("sum_1");//sum是查出總共有多少個regionID
int num = Integer.parseInt(model_num);
for(int i=1;i<=num;i++){
String ss="regionID_"+i;
log.info(ss);
String regionid= vars.get(ss);
log.info(regionid);
}
2.3 將返回的regionID寫入txt文件中(其中包括遍歷獲取到的全部regionID)
代碼如圖
import java.io.BufferedReader;
import java.io.FileReader;
FileWriter fstream= new FileWriter("E:\\00-test\\1903\\regionID.txt",true);BufferedWriter out = new BufferedWriter(fstream);log.info("-------------------------");String model_num =vars.get("sum_1");int num = Integer.parseInt(model_num);log.info("model_num:"+model_num);log.info("666666666666666666666666666666666");//log.info(num);for(int i=1;i<=num;i++){ String ss="regionID_"+i; log.info(ss); String regionid= vars.get(ss); log.info(regionid); out.write(regionid +"\n");}out.close();fstream.close();