OpenJDK下SpringBoot使用HttpSession時(shí)頁面打開卡住

近期將一個(gè)老項(xiàng)目向ARM版的CentOS7移植時(shí),遇到了SpringBoot啟動(dòng)順利,但訪問頁面卡住的問題。由于是aarch74架構(gòu),因此使用了openjdk,這個(gè)項(xiàng)目之前在x86_64環(huán)境下一直是用Oracle的ServerJRE,沒有遇到問題。此次啟動(dòng)正常,但啟動(dòng)完成后,訪問部分頁面正常,部分頁面會(huì)卡住,卡住的時(shí)間不固定,有時(shí)長有時(shí)短,毫無規(guī)律可言。而且當(dāng)卡住的頁面正常后,再刷新不會(huì)再次卡住。

成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括伊春網(wǎng)站建設(shè)、伊春網(wǎng)站制作、伊春網(wǎng)頁制作以及伊春網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,伊春網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到伊春省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

第一想法肯定是查日志,在首次訪問卡頓頁面時(shí),Spring框架有一條這樣的WARN:

2019-12-09?17:40:32.995??WARN?15161?---?[https-jsse-nio-443-exec-8]?o.a.c.util.SessionIdGeneratorBase????????:
?Creation?of?SecureRandom?instance?for?session?ID?generation?using?[SHA1PRNG]?took?[178,241]?milliseconds.

為Session創(chuàng)建SecureRandom實(shí)例耗時(shí)將近3分鐘,這就是頁面卡住的原因,同時(shí)也解釋了為什么只有部分頁面卡住,因?yàn)椴皇撬许撁娑际褂昧薙ession,同時(shí)也解析了為什么卡住的頁面可訪問后再刷新就正常了,因?yàn)閯?chuàng)建SecureRandom instance只進(jìn)行一次。

翻回來看看原因,項(xiàng)目中使用了Tomcat Embed作為內(nèi)嵌WEB服務(wù)器,而Tomcat在生成session ID時(shí)會(huì)使用org.apache.catalina.util.SessionIdGeneratorBase來產(chǎn)生安全隨機(jī)類SecureRandom實(shí)例。為了算法保密性較強(qiáng),需要用到偽隨機(jī)數(shù)生成器,Tomcat用到的是SHA1PRNG算法,為了得到隨機(jī)種子,在Linux中,一般從/dev/random或/dev/urandom中產(chǎn)生,兩者原理都是利用系統(tǒng)的環(huán)境噪聲產(chǎn)生一定數(shù)量的隨機(jī)比特,區(qū)別在于系統(tǒng)環(huán)境噪聲不夠時(shí),random會(huì)阻塞,而urandom會(huì)犧牲安全性避免阻塞。

從卡頓現(xiàn)象上看,一定是用了/dev/random導(dǎo)致的,看一下$JAVA_HOME/jre/lib/security/java.security文件,找到下面的內(nèi)容:

#
#?Sun?Provider?SecureRandom?seed?source.
#
#?Select?the?primary?source?of?seed?data?for?the?"SHA1PRNG"?and
#?"NativePRNG"?SecureRandom?implementations?in?the?"Sun"?provider.
#?(Other?SecureRandom?implementations?might?also?use?this?property.)
#
#?On?Unix-like?systems?(for?example,?Solaris/Linux/MacOS),?the
#?"NativePRNG"?and?"SHA1PRNG"?implementations?obtains?seed?data?from
#?special?device?files?such?as?file:/dev/random.
#
#?On?Windows?systems,?specifying?the?URLs?"file:/dev/random"?or
#?"file:/dev/urandom"?will?enable?the?native?Microsoft?CryptoAPI?seeding
#?mechanism?for?SHA1PRNG.
#
#?By?default,?an?attempt?is?made?to?use?the?entropy?gathering?device
#?specified?by?the?"securerandom.source"?Security?property.??If?an
#?exception?occurs?while?accessing?the?specified?URL:
#
#?????SHA1PRNG:
#?????????the?traditional?system/thread?activity?algorithm?will?be?used.
#
#?????NativePRNG:
#?????????a?default?value?of?/dev/random?will?be?used.??If?neither
#?????????are?available,?the?implementation?will?be?disabled.
#?????????"file"?is?the?only?currently?supported?protocol?type.
#
#?The?entropy?gathering?device?can?also?be?specified?with?the?System
#?property?"java.security.egd".?For?example:
#
#???%?java?-Djava.security.egd=file:/dev/random?MainClass
#
#?Specifying?this?System?property?will?override?the
#?"securerandom.source"?Security?property.
#
#?In?addition,?if?"file:/dev/random"?or?"file:/dev/urandom"?is
#?specified,?the?"NativePRNG"?implementation?will?be?more?preferred?than
#?SHA1PRNG?in?the?Sun?provider.
#

securerandom.source=file:/dev/random

果然用的是/dev/random,按照上面的注釋部分,解決方案也不復(fù)雜,可以添加啟動(dòng)參數(shù)或者修改java.security:

解決方法1:

啟動(dòng)參數(shù)添加-Djava.security.egd=file:/dev/urandom,如:

java?-Djava.security.egd=file:/dev/urandom?-jar?xxxxx.jar

解決方法2:

修改$JAVA_HOME/jre/lib/security/java.security,找到securerandom.source并修改:

securerandom.source=file:/dev/urandom

再重啟站點(diǎn),卡頓現(xiàn)象消失。

網(wǎng)站題目:OpenJDK下SpringBoot使用HttpSession時(shí)頁面打開卡住
標(biāo)題鏈接:http://muchs.cn/article10/jogigo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、外貿(mào)建站、App開發(fā)定制網(wǎng)站、服務(wù)器托管、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司