簡體   English   中英

“string”類型與“clientStorageInterface”類型沒有共同的屬性 - Angular

[英]Type 'string' has no properties in common with type 'clientStorageInterface' - Angular

有人可以告訴我理解錯誤以及如何正確使用crypto.JS嗎?

在嘗試什么?

嘗試加密/解密 sessionStorage 中的數據。

但為什么?

由於 3rd 方應用程序引發的一些安全問題,我正在嘗試加密和解密 sessionStorage 信息。 由於這個危險信號,我無法運行 jenkins 構建。

什么是紅旗?

嚴重性 - 高。

類別 - 違反數據安全合規性。

問題聲明 - 在 sessionStorage 中暴露敏感信息。

export interface IFilterKeys {
  'courceAccessedByLocation'?: string;
}
export interface clientStorageInterface {
    'courseId'?: string;
    'superAdminId'?: string;
    'courseFilter'?: IFilterKeys;
}

export declare const KEYS: {
  testData: string;
};

private mySecret: string = 'random-scret'

const testData = {
  courseId: '0000',
  superAdminId: '111',
  courseFilter : [
    {
      courceAccessedByLocation : 'Data Science'
    }
  ]
}

setFilter (saveFilters: clientStorageInterface) {
  sessionStorage.setItem('courseFilter', this.encrypt(JSON.stringify(testData)));
}

getFilter() : clientStorageInterface {
  let data = JSON.parse(sessionStorage.getItem('courseFilter')!);
    return this.decrypt(data) as any; // when removing any, am getting "Type 'string' has no properties in common with type 'clientStorageInterface'"
}



private encrypt(txt: string) {
  return CryptoJS.AES.encrypt(txt, this.mySecret).toString();
}
private decrypt(txtToDecrypt: string) {
  return CryptoJS.AES.decrypt(txtToDecrypt, this.mySecret).toString(CryptoJS.enc.Utf8);
}

如果有任何其他可能的最佳解決方案,請向我提出建議。

感謝您的指導。

getFilter() function 應該返回類型為clientStorageInterface的數據,但它的返回字符串作為decrypt function 返回字符串。 因此,typescript 正在抱怨它。

只需將其重寫如下,它應該可以解決問題

getFilter(): clientStorageInterface {
    const savedData = sessionStorage.getItem("courseFilter")!; // get the data from sessionStorage
    const decryptedData = this.decrypt(savedData); // decrypt the data
    const filter = JSON.parse(decryptedData); // convert it to object
    return filter as clientStorageInterface; // return the object as clientStorageInterface
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM