From b38ceba32f7707a67bace6c9c63984f6f256f59f Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 28 May 2021 16:15:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dsession=E5=88=B0?= =?UTF-8?q?=E6=9C=9F=E6=B2=A1=E6=9C=89=E8=BD=AC=E5=88=B0=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/commons/utils/ShiroUtils.java | 1 + .../controller/SystemParameterController.java | 8 ++++ frontend/src/business/App.vue | 46 ++++++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java b/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java index 0f8042b419..888483190c 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ShiroUtils.java @@ -48,6 +48,7 @@ public class ShiroUtils { filterChainDefinitionMap.put("/document/**", "anon"); filterChainDefinitionMap.put("/system/theme", "anon"); filterChainDefinitionMap.put("/system/save/baseurl/**", "anon"); + filterChainDefinitionMap.put("/system/timeout", "anon"); filterChainDefinitionMap.put("/v1/catalog/**", "anon"); filterChainDefinitionMap.put("/v1/agent/**", "anon"); diff --git a/backend/src/main/java/io/metersphere/controller/SystemParameterController.java b/backend/src/main/java/io/metersphere/controller/SystemParameterController.java index 84be30c4eb..8773b4259d 100644 --- a/backend/src/main/java/io/metersphere/controller/SystemParameterController.java +++ b/backend/src/main/java/io/metersphere/controller/SystemParameterController.java @@ -11,6 +11,7 @@ import io.metersphere.ldap.domain.LdapInfo; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.notice.domain.MailInfo; import io.metersphere.service.SystemParameterService; +import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -22,6 +23,8 @@ import java.util.List; public class SystemParameterController { @Resource private SystemParameterService SystemParameterService; + @Resource + private Environment env; @PostMapping("/edit/email") @MsAuditLog(module = "system_parameter_setting", type = OperLogConstants.UPDATE, title = "邮件设置", beforeEvent = "#msClass.getMailLogDetails()", content = "#msClass.getMailLogDetails()", msClass = SystemParameterService.class) @@ -44,6 +47,11 @@ public class SystemParameterController { return SystemParameterService.getValue("ui.theme"); } + @GetMapping("timeout") + public long getTimeout() { + return env.getProperty("session.timeout", Long.class, 43200L); // 默认43200s, 12个小时 + } + @GetMapping("/mail/info") public MailInfo mailInfo() { return SystemParameterService.mailInfo(ParamConstants.Classify.MAIL.getValue()); diff --git a/frontend/src/business/App.vue b/frontend/src/business/App.vue index 55617d4c59..bb98cf5782 100644 --- a/frontend/src/business/App.vue +++ b/frontend/src/business/App.vue @@ -13,7 +13,7 @@ - + @@ -39,6 +39,7 @@ const requireComponent = require.context('@/business/components/xpack/', true, / const header = requireComponent.keys().length > 0 ? requireComponent("./license/LicenseMessage.vue") : {}; const display = requireComponent.keys().length > 0 ? requireComponent("./display/Display.vue") : {}; const theme = requireComponent.keys().length > 0 ? requireComponent("./display/Theme.vue") : {}; +let timer = null; export default { name: 'app', @@ -49,11 +50,13 @@ export default { auth: false, header: {}, logoId: '_blank', - color: '' - } + color: '', + sessionTimer: null, + }; }, created() { registerRequestHeaders(); + this.initSessionTimer(); if (!hasLicense()) { setDefaultTheme(); this.color = ORIGIN_COLOR; @@ -63,20 +66,20 @@ export default { this.color = res.data ? res.data : ORIGIN_COLOR; setColor(this.color, this.color, this.color, this.color, this.color); this.$store.commit('setTheme', res.data); - }) + }); } if (localStorage.getItem("store")) { - this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(localStorage.getItem("store")))) + this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(localStorage.getItem("store")))); this.$get("/project/listAll", response => { let projectIds = response.data; if (projectIds && projectIds.length <= 0) { this.$store.commit('setProjectId', undefined); } - }) + }); } window.addEventListener("beforeunload", () => { - localStorage.setItem("store", JSON.stringify(this.$store.state)) - }) + localStorage.setItem("store", JSON.stringify(this.$store.state)); + }); }, beforeCreate() { this.$get("/isLogin").then(response => { @@ -93,12 +96,33 @@ export default { display.default.showHome(this); } } else { - window.location.href = "/login" + window.location.href = "/login"; } }).catch(() => { - window.location.href = "/login" + window.location.href = "/login"; }); }, + methods: { + initSessionTimer() { + this.$get('/system/timeout') + .then(response => { + window.addEventListener('click', () => { + this.currentTime(response.data.data); + }); + }) + .catch(() => { + }); + }, + currentTime(timeout) { // 超时退出 + if (timer) { + window.clearTimeout(timer); + timer = null; + } + timer = window.setTimeout(() => { + this.$refs.headerUser.logout(); + }, 1000 * timeout); + }, + }, components: { MsLanguageSwitch, MsUser, @@ -108,7 +132,7 @@ export default { "LicenseMessage": header.default, "Theme": theme.default } -} +};