摘要:有缺陷放棄傳輸密碼動(dòng)態(tài)加密解密這個(gè)模塊分離至項(xiàng)目權(quán)限管理系統(tǒng)與前后端分離實(shí)踐,感覺(jué)那樣寫(xiě)文章太長(zhǎng)了找不到重點(diǎn),分離出來(lái)要好點(diǎn)。這里的前后端加密解密下圖由于介紹的是動(dòng)態(tài)加密解密傳輸信息方案,這里并不會(huì)涉及之后的簽發(fā)等。
有缺陷放棄 傳輸密碼動(dòng)態(tài)加密解密
這個(gè)模塊分離至項(xiàng)目api權(quán)限管理系統(tǒng)與前后端分離實(shí)踐,感覺(jué)那樣寫(xiě)文章太長(zhǎng)了找不到重點(diǎn),分離出來(lái)要好點(diǎn)。
在用戶(hù)密碼登錄認(rèn)證中,明文傳輸用戶(hù)輸入的密碼是不可取的。在沒(méi)有用https的情況下,這里需要對(duì)用戶(hù)密碼加密傳輸,保證即使密碼泄露也不影響。
這里的前后端加密解密下圖:
由于介紹的是動(dòng)態(tài)加密解密傳輸信息方案,這里并不會(huì)涉及之后的JWT簽發(fā)等。
下面是實(shí)現(xiàn)細(xì)節(jié):
angular 前端發(fā)送get動(dòng)態(tài)秘鑰請(qǐng)求后會(huì)對(duì)對(duì)象進(jìn)行監(jiān)聽(tīng),在回調(diào)函數(shù)里獲取后端返回的秘鑰后再進(jìn)行加密處理,之后再發(fā)送登錄請(qǐng)求。在angular我把請(qǐng)求服務(wù)化了,下面的代碼片段會(huì)有點(diǎn)凌亂。
// 調(diào)用獲取tokenKey秘鑰服務(wù) this.loginService.getTokenKey().subscribe( data => { this.responseData = data; if (this.responseData.data.tokenKey !== undefined) { const tokenKey = this.responseData.data.tokenKey; // 調(diào)用服務(wù),發(fā)送認(rèn)證請(qǐng)求 this.loginService.login(this.appId, this.password, tokenKey).subscribe( data2 => { // 認(rèn)證成功返回jwt this.responseData = data2; if (this.responseData.meta.code === 1003 && this.responseData.data.jwt != null) { this.authService.updateAuthorizationToken(this.responseData.data.jwt); this.authService.updateUid(this.appId); this.authService.updateUser(this.responseData.data.user); this.router.navigateByUrl("/index"); } else { this.msg = "用戶(hù)名密碼錯(cuò)誤"; this.isDisabled = true; } }, error => { console.error(error); this.msg = error; this.isDisabled = true; } ); } } );
@Injectable() export class LoginService { constructor(private httpUtil: HttpUtil) { } getTokenKey() { const url = "account/login?tokenKey=get"; // 先向后臺(tái)申請(qǐng)加密tokenKey tokenKey=get // const getKeyParam = new HttpParams().set("tokenKey", "get"); return this.httpUtil.get(url); } login(appId: string, password: string, tokenKey: string) { const url = "account/login"; tokenKey = CryptoJS.enc.Utf8.parse(tokenKey); password = CryptoJS.enc.Utf8.parse(password); password = CryptoJS.AES.encrypt(password, tokenKey, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}).toString(); console.log(password); const param = new HttpParams().append("appId", appId) .append("password", password) .append("methodName", "login") .append("timestamp", new Date().toUTCString()); return this.httpUtil.post(url, param); } }
后端是在一個(gè)filter中對(duì)登錄注冊(cè)請(qǐng)求進(jìn)行攔截,判斷其是正常登錄注冊(cè)還是獲取動(dòng)態(tài)加密秘鑰請(qǐng)求,正常認(rèn)證就走shiro,判斷為獲取秘鑰則生成16隨機(jī)碼默認(rèn)AES加密秘鑰為約定16位,小于16位會(huì)報(bào)錯(cuò),將秘鑰以<遠(yuǎn)程IP,秘鑰>的
// 判斷若為獲取登錄注冊(cè)加密動(dòng)態(tài)秘鑰請(qǐng)求 if (isPasswordTokenGet(request)) { //動(dòng)態(tài)生成秘鑰,redis存儲(chǔ)秘鑰供之后秘鑰驗(yàn)證使用,設(shè)置有效期5秒用完即丟棄 String tokenKey = CommonUtil.getRandomString(16); try { redisTemplate.opsForValue().set("PASSWORD_TOKEN_KEY_"+request.getRemoteAddr().toUpperCase(),tokenKey,5, TimeUnit.SECONDS); // 動(dòng)態(tài)秘鑰response返回給前端 Message message = new Message(); message.ok(1000,"issued tokenKey success") .addData("tokenKey",tokenKey); RequestResponseUtil.responseWrite(JSON.toJSONString(message),response); }catch (Exception e) { LOGGER.warn(e.getMessage(),e); // 動(dòng)態(tài)秘鑰response返回給前端 Message message = new Message(); message.ok(1000,"issued tokenKey fail"); RequestResponseUtil.responseWrite(JSON.toJSONString(message),response); } return false; }
// 創(chuàng)建認(rèn)證信息,其中就有包括獲取redis中對(duì)應(yīng)IP的動(dòng)態(tài)秘鑰 private AuthenticationToken createPasswordToken(ServletRequest request) { Mapmap = RequestResponseUtil.getRequestParameters(request); String appId = map.get("appId"); String timestamp = map.get("timestamp"); String password = map.get("password"); String host = request.getRemoteAddr(); String tokenKey = redisTemplate.opsForValue().get("PASSWORD_TOKEN_KEY_"+host.toUpperCase()); return new PasswordToken(appId,password,timestamp,host,tokenKey); }
github:
bootshiro
usthe
碼云:
bootshiro
usthe
持續(xù)更新。。。。。。
分享一波阿里云代金券快速上云
轉(zhuǎn)載請(qǐng)注明 from tomsun28
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/69184.html
摘要:所以我們今天只談前端加密,一個(gè)部分人認(rèn)為沒(méi)有意義的工作。在中,認(rèn)證過(guò)程使用了非對(duì)稱(chēng)加密算法,非認(rèn)證過(guò)程中使用了對(duì)稱(chēng)加密算法。非對(duì)稱(chēng)加密上文中我們討論了前端的哈希加密以及應(yīng)用的場(chǎng)景。 showImg(https://segmentfault.com/img/bVAhTC); 當(dāng)然在談安全。 前端安全是Web安全的一部分,常見(jiàn)的安全問(wèn)題會(huì)有XSS、CSRF、SQL注入等,然而這些已經(jīng)在程師...
閱讀 333·2025-02-07 13:40
閱讀 503·2025-02-07 13:37
閱讀 786·2024-11-06 13:38
閱讀 972·2024-09-10 13:19
閱讀 1166·2024-08-22 19:45
閱讀 1439·2021-11-19 09:40
閱讀 2719·2021-11-18 13:14
閱讀 4351·2021-10-09 10:02