fix: 优化未启用安全入口跳转逻辑 (#1210)

pull/1213/head
ssongliu 2023-06-01 10:14:11 +08:00 committed by GitHub
parent cbdf1ad7b7
commit bb48964cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 31 deletions

View File

@ -103,7 +103,12 @@ func (b *BaseApi) Captcha(c *gin.Context) {
// @Router /auth/issafety [get]
func (b *BaseApi) CheckIsSafety(c *gin.Context) {
code := c.DefaultQuery("code", "")
helper.SuccessWithData(c, authService.CheckIsSafety(code))
status, err := authService.CheckIsSafety(code)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, status)
}
// @Tags Auth

View File

@ -17,7 +17,7 @@ import (
type AuthService struct{}
type IAuthService interface {
CheckIsSafety(code string) bool
CheckIsSafety(code string) (string, error)
VerifyCode(code string) (bool, error)
Login(c *gin.Context, info dto.Login) (*dto.UserLoginInfo, error)
LogOut(c *gin.Context) error
@ -143,13 +143,16 @@ func (u *AuthService) VerifyCode(code string) (bool, error) {
return setting.Value == code, nil
}
func (u *AuthService) CheckIsSafety(code string) bool {
func (u *AuthService) CheckIsSafety(code string) (string, error) {
status, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance"))
if err != nil {
return false
return "", err
}
if len(status.Value) == 0 {
return true
return "disable", nil
}
return status.Value == code
if status.Value == code {
return "pass", nil
}
return "unpass", nil
}

View File

@ -18,7 +18,7 @@ export const logOutApi = () => {
};
export const checkIsSafety = (code: string) => {
return http.get<boolean>(`/auth/issafety?code=${code}`);
return http.get<string>(`/auth/issafety?code=${code}`);
};
export const checkIsDemo = () => {

View File

@ -65,6 +65,7 @@ const loadDataFromDB = async () => {
document.title = res.data.panelName;
i18n.locale.value = res.data.language;
i18n.warnHtmlMessage = false;
globalStore.entrance = res.data.securityEntrance;
globalStore.updateLanguage(res.data.language);
globalStore.setThemeConfig({ ...themeConfig.value, theme: res.data.theme });
globalStore.setThemeConfig({ ...themeConfig.value, panelName: res.data.panelName });

View File

@ -29,15 +29,6 @@ router.beforeEach((to, from, next) => {
}
if (!to.matched.some((record) => record.meta.requiresAuth)) return next();
if (!globalStore.isLogin) {
next({
name: 'entrance',
params: { code: globalStore.entrance },
});
NProgress.done();
return;
}
return next();
});

View File

@ -1,6 +1,6 @@
<template>
<div>
<div class="login-backgroud" v-if="isSafety && !isErr">
<div class="login-backgroud" v-if="isSafety && !isErr && !isNotFound">
<div class="login-wrapper">
<div :class="screenWidth > 1110 ? 'left inline-block' : ''">
<div class="login-title">
@ -15,15 +15,18 @@
</div>
</div>
</div>
<div v-if="!isSafety && !isErr">
<div v-if="!isSafety && !isErr && !isNotFound">
<UnSafe />
</div>
<div v-if="isErr && mySafetyCode.code === 'err-ip'">
<div v-if="isErr && mySafetyCode.code === 'err-ip' && !isNotFound">
<ErrIP />
</div>
<div v-if="isErr && mySafetyCode.code === 'err-domain'">
<div v-if="isErr && mySafetyCode.code === 'err-domain' && !isNotFound">
<ErrDomain />
</div>
<div v-if="isNotFound">
<ErrFound />
</div>
</div>
</template>
@ -32,6 +35,7 @@ import { checkIsSafety } from '@/api/modules/auth';
import LoginForm from '../components/login-form.vue';
import UnSafe from '@/components/error-message/unsafe.vue';
import ErrIP from '@/components/error-message/err_ip.vue';
import ErrFound from '@/components/error-message/404.vue';
import ErrDomain from '@/components/error-message/err_domain.vue';
import { ref, onMounted } from 'vue';
import { GlobalStore } from '@/store';
@ -40,6 +44,7 @@ const globalStore = GlobalStore();
const isSafety = ref(true);
const screenWidth = ref(null);
const isErr = ref();
const isNotFound = ref();
const mySafetyCode = defineProps({
code: {
@ -50,15 +55,24 @@ const mySafetyCode = defineProps({
});
const getStatus = async () => {
if (mySafetyCode.code === 'err-ip' || mySafetyCode.code === 'err-domain') {
isErr.value = true;
}
isErr.value = true;
const res = await checkIsSafety(mySafetyCode.code);
if (mySafetyCode.code === 'err-ip' || mySafetyCode.code === 'err-domain') {
isErr.value = false;
isErr.value = false;
globalStore.entrance = '';
if (res.data === 'disable') {
if (mySafetyCode.code === '') {
isNotFound.value = false;
} else {
isNotFound.value = true;
}
return;
}
isSafety.value = res.data;
if (isSafety.value) {
isNotFound.value = false;
if (res.data !== 'pass') {
isSafety.value = false;
return;
}
if (res.data === 'pass') {
globalStore.entrance = mySafetyCode.code;
}
};

View File

@ -31,7 +31,7 @@ const screenWidth = ref(null);
const getStatus = async () => {
const res = await checkIsSafety(globalStore.entrance);
if (!res.data) {
if (res.data === 'unpass') {
router.replace({ name: 'entrance' });
}
};

View File

@ -171,6 +171,8 @@ import { updateSetting, getSettingInfo, getSystemAvailable, updateSSL, loadSSLIn
import i18n from '@/lang';
import { MsgSuccess } from '@/utils/message';
import { Setting } from '@/api/interface/setting';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
const loading = ref(false);
const entranceRef = ref();
@ -280,8 +282,14 @@ const handleSSL = async () => {
await updateSSL({ ssl: 'disable', domain: '', sslType: '', key: '', cert: '', sslID: 0 });
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
let href = window.location.href;
globalStore.isLogin = false;
let address = href.split('://')[1];
window.open(`http://${address}/`, '_self');
if (globalStore.entrance) {
address = address.replaceAll('settings/safe', globalStore.entrance);
} else {
address = address.replaceAll('settings/safe', 'login');
}
window.location.href = `http://${address}`;
})
.catch(() => {
form.ssl = 'enable';

View File

@ -72,7 +72,14 @@ const onSavePort = async (formEl: FormInstance | undefined) => {
globalStore.isLogin = false;
let href = window.location.href;
let ip = href.split('//')[1].split(':')[0];
window.open(`${href.split('//')[0]}//${ip}:${form.serverPort}/${globalStore.entrance}`, '_self');
if (globalStore.entrance) {
window.open(
`${href.split('//')[0]}//${ip}:${form.serverPort}/${globalStore.entrance}`,
'_self',
);
} else {
window.open(`${href.split('//')[0]}//${ip}:${form.serverPort}/login`, '_self');
}
})
.catch(() => {
loading.value = false;

View File

@ -212,7 +212,11 @@ const onSaveSSL = async (formEl: FormInstance | undefined) => {
let href = window.location.href;
globalStore.isLogin = false;
let address = href.split('://')[1];
address = address.replaceAll('settings/safe', globalStore.entrance);
if (globalStore.entrance) {
address = address.replaceAll('settings/safe', globalStore.entrance);
} else {
address = address.replaceAll('settings/safe', 'login');
}
window.open(`https://${address}`, '_self');
});
});