页面二次认证

页面二次认证功能有什么用?

通过在页面上设置密码(passcode),增强了页面的安全性。用户访问设置了密码的页面时,需要输入正确的密码,才能正常访问页面,否则无法访问。用户输入正确密码后,一段时间内不需要再次输入密码认证。该时间长度可通过修改页面上的配置来调整。详见页面加入组件部分的说明。

为页面配置 二次认证

  1. 结构修改:在数据库_page表中增加一列:passcode,数据类型为VARCHAR(255)

    1. ALTER TABLE `_page` ADD COLUMN `passcode` varchar(255) NULL COMMENT '页面二次认证; passcode ' AFTER `sort`;
  2. 为页面配置密码:在_page表中,找到要添加密码的页面,在passcode字段中输入密码,并保存。
    注意:若passcode字段密码为空,则等同于该页面没有设置密码。

  3. 在页面内添加如下代码。

    1. <!-- 页面认证弹窗 >>>>>>>>>>>>> -->
    2. <v-dialog
    3. v-model="isPageValidationDialogShown" persistent="true" :hide-overlay="false"
    4. overlay-opacity="1"
    5. transition="dialog-top-transition" max-width="600">
    6. <v-card>
    7. <v-toolbar color="primary text-h7 pl-5" dark>页面认证</v-toolbar>
    8. <v-card-text>
    9. <v-row class="px-10 pt-5">
    10. <v-col cols="12" :class="{'pa-0': isMobile, 'px-4': isMobile, 'pt-6': isMobile}">
    11. <span class="jh-input-label">认证码</span>
    12. <v-text-field class="jh-v-input" dense filled single-line type="password" v-model="passcodeOfUser"></v-text-field>
    13. <p style="text-align: right;">认证成功后, {{ validationDurationHour }}小时内无需重复认证。</p>
    14. </v-col>
    15. </v-row>
    16. </v-card-text>
    17. <v-card-actions class="justify-end">
    18. <v-btn depressed class="mr-2 primary" @click="doUiAction('doPageValidation')">确定</v-btn>
    19. </v-card-actions>
    20. </v-card>
    21. </v-dialog>
    22. <!-- <<<<<<<<<<< 页面认证弹窗 -->
    23. passcodeOfServer: '<$ page.passcode $>',
    24. passcodeOfUser: null,
    25. // --------------- 二次认证 uiAction >>>>>>>>>> ---------------
    26. async preparePageValidationData() {
    27. const urlPathList = window.location.pathname.split('/');
    28. this.pageId = urlPathList && urlPathList[urlPathList.length - 1];
    29. },
    30. checkAndOpenPageValidationDialog() {
    31. if (!this.passcodeOfServer) {
    32. this.isPageValidationDialogShown = false
    33. return;
    34. }
    35. const pageValidation = this.getPageValidationCache({pageId: this.pageId});
    36. const {date, success} = pageValidation;
    37. let duration = 999999;
    38. try {
    39. duration = dayjs().diff(date, 'hour');
    40. // duration = dayjs().diff(date, 'minute');
    41. // duration = dayjs().diff(date, 'second');
    42. } catch (error) {
    43. console.error("[created]", "date 解析异常");
    44. }
    45. if (success === true && duration < this.validationDurationHour) {
    46. this.isPageValidationDialogShown = false;
    47. return;
    48. }
    49. this.isPageValidationDialogShown = true
    50. },
    51. getPageValidationCache({pageId}) {
    52. let pageValidationObj = {}
    53. try {
    54. const pageValidationObjStr = localStorage.getItem(`${window.appInfo.appId}_page_validation_obj`);
    55. pageValidationObj = JSON.parse(pageValidationObjStr || '{}');
    56. } catch (error) {
    57. console.error("[getPageValidationCache]", "json 数据异常");
    58. }
    59. return pageValidationObj[pageId] || {};
    60. },
    61. cachePageValidation({pageId, value}) {
    62. let pageValidationObj = {}
    63. try {
    64. const pageValidationObjStr = localStorage.getItem(`${window.appInfo.appId}_page_validation_obj`);
    65. pageValidationObj = JSON.parse(pageValidationObjStr || '{}');
    66. } catch (error) {
    67. console.error("[cachePageValidation]", "json 数据异常");
    68. }
    69. pageValidationObj[pageId] = value;
    70. localStorage.setItem(`${window.appInfo.appId}_page_validation_obj`, JSON.stringify(pageValidationObj));
    71. },
    72. doPageValidation() {
    73. if (this.passcodeOfUser !== this.passcodeOfServer) {
    74. window.vtoast.fail({message: '页面认证失败'});
    75. }
    76. if (this.passcodeOfUser === this.passcodeOfServer) {
    77. this.cachePageValidation({
    78. pageId: this.pageId,
    79. value: {
    80. pageId: this.pageId,
    81. date: dayjs(),
    82. success: true,
    83. }
    84. });
    85. this.isPageValidationDialogShown = false;
    86. }
    87. },
    88. // --------------- <<<<<<<<<< 二次认证 uiAction ---------------

    其中,validationDurationHour是当前密码认证的有效期,以小时计算。修改此数值即可调整密码认证的有效期。

  4. 重新访问页面,就需要输入页面密码了。