From 0b21fc8e6ae386c6b86c44d11b552ea0dc0882d1 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 1 Jul 2021 11:43:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=90=8E=E5=8F=B0=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E8=BF=87=E5=90=8E=EF=BC=8C=E5=89=8D=E7=AB=AF=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E9=A1=B5=E9=9D=A2=E5=BC=B9=E6=A1=86=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/SessionConstants.java | 2 ++ .../metersphere/controller/IndexController.java | 6 +++++- .../io/metersphere/security/ApiKeyFilter.java | 3 ++- .../java/io/metersphere/security/CsrfFilter.java | 3 ++- frontend/src/common/js/ajax.js | 16 ++++++++-------- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java b/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java index 85e4ff1935..a9b3489112 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java +++ b/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java @@ -2,4 +2,6 @@ package io.metersphere.commons.constants; public class SessionConstants { public static final String ATTR_USER = "user"; + public static final String AUTHENTICATION_STATUS = "Authentication-Status"; + public static final String AUTHENTICATION_INVALID = "invalid"; } diff --git a/backend/src/main/java/io/metersphere/controller/IndexController.java b/backend/src/main/java/io/metersphere/controller/IndexController.java index 2d82278414..e8a8c308e8 100644 --- a/backend/src/main/java/io/metersphere/controller/IndexController.java +++ b/backend/src/main/java/io/metersphere/controller/IndexController.java @@ -1,10 +1,13 @@ package io.metersphere.controller; +import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.utils.SessionUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import javax.servlet.http.HttpServletResponse; + @Controller @RequestMapping public class IndexController { @@ -15,8 +18,9 @@ public class IndexController { } @GetMapping(value = "/login") - public String login() { + public String login(HttpServletResponse response) { if (SessionUtils.getUser() == null) { + response.setHeader(SessionConstants.AUTHENTICATION_STATUS, SessionConstants.AUTHENTICATION_INVALID); return "login.html"; } else { return "redirect:/"; diff --git a/backend/src/main/java/io/metersphere/security/ApiKeyFilter.java b/backend/src/main/java/io/metersphere/security/ApiKeyFilter.java index bf1d3b8652..8cadbac193 100644 --- a/backend/src/main/java/io/metersphere/security/ApiKeyFilter.java +++ b/backend/src/main/java/io/metersphere/security/ApiKeyFilter.java @@ -1,5 +1,6 @@ package io.metersphere.security; +import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.utils.LogUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; @@ -31,7 +32,7 @@ public class ApiKeyFilter extends AnonymousFilter { } if (!SecurityUtils.getSubject().isAuthenticated()) { - ((HttpServletResponse) response).setHeader("Authentication-Status", "invalid"); + ((HttpServletResponse) response).setHeader(SessionConstants.AUTHENTICATION_STATUS, SessionConstants.AUTHENTICATION_INVALID); } } catch (Exception e) { if (ApiKeyHandler.isApiKeyCall(WebUtils.toHttp(request))) { diff --git a/backend/src/main/java/io/metersphere/security/CsrfFilter.java b/backend/src/main/java/io/metersphere/security/CsrfFilter.java index 56b2e74a0c..cbd66db63b 100644 --- a/backend/src/main/java/io/metersphere/security/CsrfFilter.java +++ b/backend/src/main/java/io/metersphere/security/CsrfFilter.java @@ -1,5 +1,6 @@ package io.metersphere.security; +import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.CodingUtil; import io.metersphere.commons.utils.CommonBeanFactory; @@ -25,7 +26,7 @@ public class CsrfFilter extends AnonymousFilter { HttpServletRequest httpServletRequest = WebUtils.toHttp(request); if (!SecurityUtils.getSubject().isAuthenticated()) { - ((HttpServletResponse) response).setHeader("Authentication-Status", "invalid"); + ((HttpServletResponse) response).setHeader(SessionConstants.AUTHENTICATION_STATUS, SessionConstants.AUTHENTICATION_INVALID); return true; } // api 过来的请求 diff --git a/frontend/src/common/js/ajax.js b/frontend/src/common/js/ajax.js index 1627a825c8..ca30bbfa4b 100644 --- a/frontend/src/common/js/ajax.js +++ b/frontend/src/common/js/ajax.js @@ -1,6 +1,6 @@ import {Message, MessageBox} from 'element-ui'; import axios from "axios"; -import i18n from '../../i18n/i18n' +import i18n from '../../i18n/i18n'; import {TokenKey} from "@/common/js/constants"; export function registerRequestHeaders() { @@ -33,7 +33,7 @@ export function login() { callback: () => { axios.get("/signout"); localStorage.setItem('Admin-Token', "{}"); - window.location.href = "/login" + window.location.href = "/login"; } }); } @@ -86,7 +86,7 @@ export function get(url, success) { } } -export function post (url, data, success, failure) { +export function post(url, data, success, failure) { let result = {loading: true}; if (!success) { return axios.post(url, data); @@ -139,7 +139,7 @@ export function fileUpload(url, file, files, param, success, failure) { if (files) { files.forEach(f => { formData.append("files", f); - }) + }); } formData.append('request', new Blob([JSON.stringify(param)], {type: "application/json"})); let axiosRequestConfig = getUploadConfig(url, formData); @@ -156,15 +156,15 @@ export default { if (!axios) { window.console.error('You have to install axios'); - return + return; } if (!Message) { window.console.error('You have to install Message of ElementUI'); - return + return; } - let login = login; + // let login = login; axios.defaults.withCredentials = true; @@ -189,4 +189,4 @@ export default { Vue.prototype.$fileUpload = fileUpload; } -} +};