[优化] 添加基础鉴权配置支持,包括用户名和密码

This commit is contained in:
ryan
2026-05-26 09:37:42 +08:00
parent dd58e0df66
commit baef42f920
7 changed files with 195 additions and 10 deletions
@@ -1020,6 +1020,118 @@ function PowSection({
);
}
const basicAuthSchema = z
.object({
basic_auth_enabled: z.boolean(),
basic_auth_username: z.string(),
basic_auth_password: z.string(),
})
.superRefine((value, context) => {
if (value.basic_auth_enabled) {
if (!value.basic_auth_username.trim()) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['basic_auth_username'],
message: '请输入账号',
});
}
if (!value.basic_auth_password.trim()) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['basic_auth_password'],
message: '请输入密码',
});
}
}
});
type BasicAuthValues = z.infer<typeof basicAuthSchema>;
function BasicAuthSection({
route,
saving,
onSave,
}: {
route: ProxyRouteItem;
saving: boolean;
onSave: SaveHandler;
}) {
const form = useForm<BasicAuthValues>({
resolver: zodResolver(basicAuthSchema),
defaultValues: {
basic_auth_enabled: route.basic_auth_enabled,
basic_auth_username: route.basic_auth_username || '',
basic_auth_password: route.basic_auth_password || '',
},
});
useEffect(() => {
form.reset({
basic_auth_enabled: route.basic_auth_enabled,
basic_auth_username: route.basic_auth_username || '',
basic_auth_password: route.basic_auth_password || '',
});
}, [form, route]);
const watchedEnabled = form.watch('basic_auth_enabled');
return (
<ConfigSectionShell
title="认证配置"
description="配置基础鉴权访问,需要输入账号密码才能访问网站。"
formId="proxy-route-auth-form"
saving={saving}
>
<form
id="proxy-route-auth-form"
className="space-y-5"
onSubmit={form.handleSubmit((values) => {
onSave(
buildPayloadFromRoute(route, {
basic_auth_enabled: values.basic_auth_enabled,
basic_auth_username: values.basic_auth_username.trim(),
basic_auth_password: values.basic_auth_password.trim(),
}),
{ message: '认证配置已保存。' },
);
})}
>
<ToggleField
label="启用 Basic Auth 鉴权"
description="拦截所有请求,需要输入正确的账号和密码才能访问。"
checked={watchedEnabled}
onChange={(checked) =>
form.setValue('basic_auth_enabled', checked, { shouldDirty: true })
}
/>
<ResourceField
label="账号"
error={form.formState.errors.basic_auth_username?.message}
>
<ResourceInput
disabled={!watchedEnabled}
placeholder="admin"
{...form.register('basic_auth_username')}
/>
</ResourceField>
<ResourceField
label="密码"
error={form.formState.errors.basic_auth_password?.message}
>
<ResourceInput
disabled={!watchedEnabled}
type="text"
placeholder="secret123"
{...form.register('basic_auth_password')}
/>
</ResourceField>
</form>
</ConfigSectionShell>
);
}
export function ProxyRouteConfigPage({
routeId,
initialSection,
@@ -1240,6 +1352,16 @@ export function ProxyRouteConfigPage({
}
/>
) : null}
{currentSection === 'auth' ? (
<BasicAuthSection
route={route}
saving={saveMutation.isPending}
onSave={(payload, context) =>
saveMutation.mutate({ payload, context })
}
/>
) : null}
</div>
</div>
</div>
@@ -192,6 +192,9 @@ export function ProxyRouteCreateDrawer({
custom_headers: [],
pow_enabled: false,
pow_config: '{}',
basic_auth_enabled: false,
basic_auth_username: '',
basic_auth_password: '',
remark: values.remark.trim(),
});
},
@@ -30,6 +30,11 @@ export const websiteConfigSections = [
label: 'PoW 防护',
description: '配置 Proof-of-Work 反爬虫策略。',
},
{
key: 'auth',
label: '认证配置',
description: '配置基础鉴权访问,需要输入账号密码才能访问网站。',
},
] as const;
export type WebsiteConfigSectionKey =
@@ -292,6 +297,9 @@ export function buildPayloadFromRoute(
remark: route.remark || '',
pow_enabled: route.pow_enabled,
pow_config: JSON.stringify(route.pow_config),
basic_auth_enabled: route.basic_auth_enabled,
basic_auth_username: route.basic_auth_username,
basic_auth_password: route.basic_auth_password,
...overrides,
};
}
@@ -49,6 +49,9 @@ export interface ProxyRouteItem {
custom_header_list: ProxyRouteCustomHeader[];
pow_enabled: boolean;
pow_config: ProxyRoutePoWConfig;
basic_auth_enabled: boolean;
basic_auth_username: string;
basic_auth_password: string;
remark: string;
created_at: string;
updated_at: string;
@@ -81,6 +84,9 @@ export interface ProxyRouteMutationPayload {
custom_headers: ProxyRouteCustomHeader[];
pow_enabled: boolean;
pow_config: string;
basic_auth_enabled: boolean;
basic_auth_username?: string;
basic_auth_password?: string;
remark: string;
}