前端使用JMeter測(cè)試JSEncrypt加密登錄
簡(jiǎn)介:前端開(kāi)發(fā)時(shí)會(huì)接觸到用戶登錄,登錄時(shí)為了數(shù)據(jù)的安全會(huì)使用到j(luò)sencrypt加密工具,同時(shí)我們需要使用jmeter來(lái)進(jìn)行壓測(cè),幫助我們了解Web應(yīng)用程序在高負(fù)載情況下的性能表現(xiàn),從而為優(yōu)化應(yīng)用程序性能提供參考
環(huán)境:
1.JMeter: 5.5
2.JDK: 8.0
3.插件:
安裝插件:jmeter-plugins-manager-1.3.jar
1.創(chuàng)建線程組
2. 創(chuàng)建HTTP信息頭管理器
Content-Type :application/json
3. 創(chuàng)建HTTP請(qǐng)求
3.1請(qǐng)求接口說(shuō)明requestBody:
loginType登錄類型,phone登錄的手機(jī)號(hào),password加密后的密碼
{
"loginType":0,
"phone":"${username}",
"password":"${rsa_pwd}"
}
4. 添加前置處理器
4.1 添加csv文件
4.2 添加JSR223預(yù)處理程序
4.2.1 獲取公鑰并保存
腳本:
// 導(dǎo)入必需的類
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
// 創(chuàng)建一個(gè) HTTPS 連接
def httpClient = HttpClients.createDefault()
// 創(chuàng)建一個(gè) HTTP GET 請(qǐng)求
def httpRequest = new HttpGet("https://XXXXX.com/auth-server/oauth/publickey")
// 發(fā)送 HTTP GET 請(qǐng)求并獲取響應(yīng)
def httpResponse = httpClient.execute(httpRequest)
// 獲取響應(yīng)數(shù)據(jù)
def response = EntityUtils.toString(httpResponse.getEntity())
// 將響應(yīng)數(shù)據(jù)保存到 JMeter 變量中
vars.put("response", response)
// 關(guān)閉連接
httpClient.close()
4.2.2 加密用戶的密碼
var log = org.apache.logging.log4j.LogManager.getLogger();
var response = vars.get("response");
var navigator = this;
var window = this;
// 引用在線jsencrypt.min.js包,也可以下載到本地再引用
load("https://passport.cnblogs.com/scripts/jsencrypt.min.js");
log.info("==================================開(kāi)始加密==============================");
// 獲取公鑰
var pubKey = JSON.parse(response).data;
log.info("公鑰:"+pubKey);
// 使用JSEncrypt庫(kù)對(duì)數(shù)據(jù)進(jìn)行加密
var encrypt = new JSEncrypt();
encrypt.setPublicKey(pubKey);
// 用戶名
var username = '${phone}';
log.info("用戶名:"+username);
// 將用戶名保存到 JMeter 變量中
vars.put("username", username)
// 獲取密碼
var password = '${password}';
log.info("密碼:"+password);
// 獲取當(dāng)前時(shí)間戳
var currentTime = new Date().getTime();
log.info("當(dāng)前時(shí)間戳:"+currentTime);
// 將密碼和當(dāng)前時(shí)間進(jìn)行拼接
var data = password + "," + currentTime;
var rsa_pwd = encrypt.encrypt(data);
log.info("加密后數(shù)據(jù):"+rsa_pwd);
// 將加密后數(shù)據(jù)保存到 JMeter 變量中
vars.put("rsa_pwd", rsa_pwd)
log.info("==================================結(jié)束加密==============================");
5. 添加-斷言-JSON斷言
6.添加-后置處理器-JSON提取器
提取登錄成功后,后端返回的token
7.添加-監(jiān)聽(tīng)器-察看結(jié)果樹(shù)
8.添加-監(jiān)聽(tīng)器-聚合報(bào)告
9.開(kāi)始測(cè)試
9.1接口響應(yīng)結(jié)果:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-667673.html
9.2聚合報(bào)告:
補(bǔ)充:你也可以直接使用我配置好的jmx文件,在jmeter中直接打開(kāi)即可:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-667673.html
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="測(cè)試計(jì)劃" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用戶定義的變量" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="聚合報(bào)告" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看結(jié)果樹(shù)" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<kg.apc.jmeter.perfmon.PerfMonCollector guiclass="kg.apc.jmeter.vizualizers.PerfMonGui" testclass="kg.apc.jmeter.perfmon.PerfMonCollector" testname="jp@gc - PerfMon Metrics Collector" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
<longProp name="interval_grouping">1000</longProp>
<boolProp name="graph_aggregated">false</boolProp>
<stringProp name="include_sample_labels"></stringProp>
<stringProp name="exclude_sample_labels"></stringProp>
<stringProp name="start_offset"></stringProp>
<stringProp name="end_offset"></stringProp>
<boolProp name="include_checkbox_state">false</boolProp>
<boolProp name="exclude_checkbox_state">false</boolProp>
<collectionProp name="metricConnections">
<collectionProp name="-1338873306">
<stringProp name="645947445">xxxxxx.com</stringProp>
<stringProp name="0"></stringProp>
<stringProp name="66952">CPU</stringProp>
<stringProp name="0"></stringProp>
</collectionProp>
<collectionProp name="655379669">
<stringProp name="645947445">xxxxxx.com</stringProp>
<stringProp name="0"></stringProp>
<stringProp name="-1993889503">Memory</stringProp>
<stringProp name="0"></stringProp>
</collectionProp>
<collectionProp name="767719685">
<stringProp name="645947445">xxxxxx.com</stringProp>
<stringProp name="0"></stringProp>
<stringProp name="-274342153">Network I/O</stringProp>
<stringProp name="0"></stringProp>
</collectionProp>
</collectionProp>
</kg.apc.jmeter.perfmon.PerfMonCollector>
<hashTree/>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="線程組" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循環(huán)控制器" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">10</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
<hashTree>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息頭管理器" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="Content-Type" elementType="Header">
<stringProp name="Header.name">Content-Type</stringProp>
<stringProp name="Header.value">application/json</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="login" enabled="true">
<boolProp name="HTTPSampler.postBodyRaw">true</boolProp>
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments">
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">false</boolProp>
<stringProp name="Argument.value">{
"loginType":0,
"phone":"${username}",
"password":"${rsa_pwd}"
}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp name="HTTPSampler.domain">xxxxxx.com</stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol">https</stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">/auth-server/oauth/login</stringProp>
<stringProp name="HTTPSampler.method">POST</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</HTTPSamplerProxy>
<hashTree>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV 數(shù)據(jù)文件設(shè)置" enabled="true">
<stringProp name="delimiter">,</stringProp>
<stringProp name="fileEncoding">UTF-8</stringProp>
<stringProp name="filename">C:/Users/chenhongxin/Desktop/20用戶.csv</stringProp>
<boolProp name="ignoreFirstLine">true</boolProp>
<boolProp name="quotedData">false</boolProp>
<boolProp name="recycle">false</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
<boolProp name="stopThread">true</boolProp>
<stringProp name="variableNames">phone,password</stringProp>
</CSVDataSet>
<hashTree/>
<JSR223PreProcessor guiclass="TestBeanGUI" testclass="JSR223PreProcessor" testname="獲取公鑰并保存" enabled="true">
<stringProp name="scriptLanguage">groovy</stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="filename"></stringProp>
<stringProp name="cacheKey">true</stringProp>
<stringProp name="script">// 導(dǎo)入必需的類
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
// 創(chuàng)建一個(gè) HTTPS 連接
def httpClient = HttpClients.createDefault()
// 創(chuàng)建一個(gè) HTTP GET 請(qǐng)求
def httpRequest = new HttpGet("https://xxxxxx.com/auth-server/oauth/publickey")
// 發(fā)送 HTTP GET 請(qǐng)求并獲取響應(yīng)
def httpResponse = httpClient.execute(httpRequest)
// 獲取響應(yīng)數(shù)據(jù)
def response = EntityUtils.toString(httpResponse.getEntity())
// 將響應(yīng)數(shù)據(jù)保存到 JMeter 變量中
vars.put("response", response)
// 關(guān)閉連接
httpClient.close()
</stringProp>
</JSR223PreProcessor>
<hashTree/>
<JSR223PreProcessor guiclass="TestBeanGUI" testclass="JSR223PreProcessor" testname="加密用戶的密碼" enabled="true">
<stringProp name="scriptLanguage">javascript</stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="filename"></stringProp>
<stringProp name="cacheKey">true</stringProp>
<stringProp name="script">
var log = org.apache.logging.log4j.LogManager.getLogger();
var response = vars.get("response");
var navigator = this;
var window = this;
// 引用在線jsencrypt.min.js包,也可以下載到本地再引用
load("https://passport.cnblogs.com/scripts/jsencrypt.min.js");
log.info("==================================開(kāi)始加密==============================");
// 獲取公鑰
var pubKey = JSON.parse(response).data;
log.info("公鑰:"+pubKey);
// 使用JSEncrypt庫(kù)對(duì)數(shù)據(jù)進(jìn)行加密
var encrypt = new JSEncrypt();
encrypt.setPublicKey(pubKey);
// 用戶名
var username = '${phone}';
log.info("用戶名:"+username);
// 將用戶名保存到 JMeter 變量中
vars.put("username", username)
// 獲取密碼
var password = '${password}';
log.info("密碼:"+password);
// 獲取當(dāng)前時(shí)間戳
var currentTime = new Date().getTime();
log.info("當(dāng)前時(shí)間戳:"+currentTime);
// 將密碼和當(dāng)前時(shí)間進(jìn)行拼接
var data = password + "," + currentTime;
var rsa_pwd = encrypt.encrypt(data);
log.info("加密后數(shù)據(jù):"+rsa_pwd);
// 將加密后數(shù)據(jù)保存到 JMeter 變量中
vars.put("rsa_pwd", rsa_pwd)
log.info("==================================結(jié)束加密==============================");
//
//
將加密后的密碼和用戶名添加到HTTP請(qǐng)求參數(shù)中
//vars.put("rsa_pwd", rsa_pwd);
//vars.put("username", username);
//
//
</stringProp>
</JSR223PreProcessor>
<hashTree/>
<JSONPathAssertion guiclass="JSONPathAssertionGui" testclass="JSONPathAssertion" testname="JSON斷言" enabled="true">
<stringProp name="JSON_PATH">$.code</stringProp>
<stringProp name="EXPECTED_VALUE">200</stringProp>
<boolProp name="JSONVALIDATION">true</boolProp>
<boolProp name="EXPECT_NULL">false</boolProp>
<boolProp name="INVERT">false</boolProp>
<boolProp name="ISREGEX">true</boolProp>
</JSONPathAssertion>
<hashTree/>
<JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON提取器" enabled="true">
<stringProp name="JSONPostProcessor.referenceNames">token</stringProp>
<stringProp name="JSONPostProcessor.jsonPathExprs">$.data.token</stringProp>
<stringProp name="JSONPostProcessor.match_numbers">1</stringProp>
</JSONPostProcessor>
<hashTree/>
</hashTree>
<DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="調(diào)試取樣器${token}" enabled="false">
<boolProp name="displayJMeterProperties">false</boolProp>
<boolProp name="displayJMeterVariables">true</boolProp>
<boolProp name="displaySystemProperties">false</boolProp>
</DebugSampler>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
到了這里,關(guān)于【JMeter】前端使用JMeter測(cè)試JSEncrypt加密登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!