日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Shrio Unable to execute ‘doFinal‘ with cipher instance

發(fā)布時(shí)間:2025/3/15 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shrio Unable to execute ‘doFinal‘ with cipher instance 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天項(xiàng)目啟動(dòng)后登錄項(xiàng)目,突然爆出Unable to execute ‘doFinal’ with cipher instance錯(cuò)誤。清除cookie登錄測(cè)試,又不報(bào)錯(cuò)了,以前也見(jiàn)過(guò)類(lèi)似問(wèn)題,因?yàn)椴挥绊懯褂?#xff0c;于是就忽略了,今天又遇到了,特研究一下。

原來(lái),項(xiàng)目中使用Shiro作為認(rèn)證權(quán)限控制框架,問(wèn)題就出在RememberMe功能的配置上。問(wèn)題產(chǎn)生的原因是rememberMe的cookie在第二次打開(kāi)頁(yè)面后shiro無(wú)法解密。

@Bean public RememberMeManager rememberMeManager() {CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();//注入自定義cookie(主要是設(shè)置壽命, 默認(rèn)的一年太長(zhǎng))SimpleCookie simpleCookie = new SimpleCookie("rememberMe");simpleCookie.setHttpOnly(true);//設(shè)置RememberMe的cookie有效期為7天simpleCookie.setMaxAge(604800);rememberMeManager.setCookie(simpleCookie); return rememberMeManager;}

框架源碼

public AbstractRememberMeManager() {this.serializer = new DefaultSerializer<PrincipalCollection>();AesCipherService cipherService = new AesCipherService();this.cipherService = cipherService;setCipherKey(cipherService.generateNewKey().getEncoded());}public void setCipherKey(byte[] cipherKey) {//Since this method should only be used in symmetric ciphers//(where the enc and dec keys are the same), set it on both:setEncryptionCipherKey(cipherKey);setDecryptionCipherKey(cipherKey);}

rememberMeManager繼承了AbstractRememberMeManager,然而AbstractRememberMeManager的構(gòu)造方法中每次都會(huì)重新生成對(duì)稱(chēng)加密密鑰,意味著每次重啟程序都會(huì)重新生成一對(duì)加解密密鑰。

這就會(huì)導(dǎo)致了,第一次啟動(dòng)程序shiro使用A密鑰加密了cookie,第二次啟動(dòng)程序shiro重新生成了密鑰B,當(dāng)用戶(hù)訪問(wèn)頁(yè)面時(shí),shiro會(huì)用密鑰B去解密上一次用密鑰A加密的cookie,導(dǎo)致解密失敗,導(dǎo)致報(bào)錯(cuò),所以這不影響用戶(hù)登錄操作(rememberMe失效罷了),所以這種異常只會(huì)在程序重啟(shiro清除session)第一次打開(kāi)頁(yè)面的時(shí)候出現(xiàn)。

解決辦法:手動(dòng)設(shè)置對(duì)稱(chēng)加密秘鑰。

//手動(dòng)設(shè)置對(duì)稱(chēng)加密秘鑰,防止重啟系統(tǒng)后系統(tǒng)生成新的隨機(jī)秘鑰,防止導(dǎo)致客戶(hù)端cookie無(wú)效 rememberMeManager.setCipherKey(Base64.decode("6ZmI6I2j3Y+R1aSn5BOlAA=="));

文章轉(zhuǎn)自

總結(jié)

以上是生活随笔為你收集整理的Shrio Unable to execute ‘doFinal‘ with cipher instance的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。