From eb0ab710172ce639ab55071ad0eb642ee9f8e7b1 Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 23 Dec 2021 21:50:06 +0800 Subject: [PATCH] =?UTF-8?q?A=20=E5=AE=89=E5=85=A8=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95IP=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/setting/models.py | 1 + spug_api/apps/setting/views.py | 9 +++++--- spug_api/libs/middleware.py | 12 ++++++---- .../pages/system/setting/SecuritySetting.js | 23 ++++++++++++++++++- spug_web/src/pages/system/setting/store.js | 6 +---- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/spug_api/apps/setting/models.py b/spug_api/apps/setting/models.py index fdac245..cdab32f 100644 --- a/spug_api/apps/setting/models.py +++ b/spug_api/apps/setting/models.py @@ -8,6 +8,7 @@ import json KEYS_DEFAULT = { 'MFA': {'enable': False}, 'verify_ip': True, + 'bind_ip': True, 'ldap_service': {}, 'spug_key': None, 'api_key': None, diff --git a/spug_api/apps/setting/views.py b/spug_api/apps/setting/views.py index dce5b90..417cc15 100644 --- a/spug_api/apps/setting/views.py +++ b/spug_api/apps/setting/views.py @@ -10,15 +10,18 @@ from libs.mail import Mail from libs.spug import send_login_wx_code from libs.mixins import AdminView from apps.setting.utils import AppSetting -from apps.setting.models import Setting +from apps.setting.models import Setting, KEYS_DEFAULT +from copy import deepcopy import platform import ldap class SettingView(AdminView): def get(self, request): - data = Setting.objects.all() - return json_response([x.to_view() for x in data]) + response = deepcopy(KEYS_DEFAULT) + for item in Setting.objects.all(): + response[item.key] = item.real_val + return json_response(response) def post(self, request): form, error = JsonParser( diff --git a/spug_api/libs/middleware.py b/spug_api/libs/middleware.py index 391a192..aa412b2 100644 --- a/spug_api/libs/middleware.py +++ b/spug_api/libs/middleware.py @@ -5,6 +5,7 @@ from django.utils.deprecation import MiddlewareMixin from django.conf import settings from .utils import json_response, get_request_real_ip from apps.account.models import User +from apps.setting.utils import AppSetting import traceback import time @@ -33,11 +34,12 @@ class AuthenticationMiddleware(MiddlewareMixin): if access_token and len(access_token) == 32: x_real_ip = get_request_real_ip(request.headers) user = User.objects.filter(access_token=access_token).first() - if user and x_real_ip == user.last_ip and user.token_expired >= time.time() and user.is_active: - request.user = user - user.token_expired = time.time() + 8 * 60 * 60 - user.save() - return None + if user and user.token_expired >= time.time() and user.is_active: + if x_real_ip == user.last_ip or AppSetting.get_default('bind_ip') is False: + request.user = user + user.token_expired = time.time() + 8 * 60 * 60 + user.save() + return None response = json_response(error="验证失败,请重新登录") response.status_code = 401 return response diff --git a/spug_web/src/pages/system/setting/SecuritySetting.js b/spug_web/src/pages/system/setting/SecuritySetting.js index adf692e..ce93916 100644 --- a/spug_web/src/pages/system/setting/SecuritySetting.js +++ b/spug_web/src/pages/system/setting/SecuritySetting.js @@ -12,6 +12,7 @@ import store from './store'; export default observer(function () { const [verify_ip, setVerifyIP] = useState(store.settings.verify_ip); + const [bind_ip, setBindIP] = useState(store.settings.bind_ip); const [mfa, setMFA] = useState(store.settings.MFA || {}); const [code, setCode] = useState(); const [visible, setVisible] = useState(false); @@ -36,6 +37,15 @@ export default observer(function () { }) } + function handleChangeBindIP(v) { + setBindIP(v); + http.post('/api/setting/', {data: [{key: 'bind_ip', value: v}]}) + .then(() => { + message.success('设置成功'); + store.fetchSettings() + }) + } + function handleChangeMFA(v) { if (v && !store.settings.spug_key) return message.error('开启MFA认证需要先在基本设置中配置调用凭据'); v ? setVisible(true) : handleMFAModify(false) @@ -66,13 +76,24 @@ export default observer(function () {
+ extra={建议开启,校验是否获取了真实的访问者IP,防止因为增加的反向代理层导致基于IP的安全策略失效,当校验失败时会在登录时弹窗提醒。如果你在内网部署且仅在内网使用可以关闭该特性。为什么没有获取到真实IP?}> + + + { this.isFetching = true; http.get('/api/setting/') - .then(res => { - for (let item of res) { - this.settings[item.key] = item.value; - } - }) + .then(res => this.settings = res) .finally(() => this.isFetching = false) };