给所有请求都加上jwt,隐藏生成jwt的secret(放到.env中),隐藏app-secret(放在pm2运行配置文件中,后续直接读取环境配置即可)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 🔒 服务器端专用:OAuth 敏感配置
|
||||
*
|
||||
* 此文件只在服务器端运行,确保环境变量在运行时读取
|
||||
* Remix 会自动排除 .server.ts 文件不打包到客户端
|
||||
*/
|
||||
|
||||
// 用于控制日志输出(避免重复日志)
|
||||
let hasLoggedSecret = false;
|
||||
|
||||
/**
|
||||
* 从环境变量获取 OAuth Client Secret
|
||||
* 在服务器运行时动态读取,不依赖构建时的环境变量注入
|
||||
*/
|
||||
export function getOAuthClientSecret(): string {
|
||||
const secret = process.env.OAUTH_CLIENT_SECRET;
|
||||
|
||||
// 只在第一次调用时输出详细日志(避免启动时就输出)
|
||||
if (!hasLoggedSecret) {
|
||||
hasLoggedSecret = true;
|
||||
|
||||
console.log('🔍 [oauth-secret.server] 读取 OAUTH_CLIENT_SECRET:');
|
||||
console.log(' - 值存在:', !!secret);
|
||||
console.log(' - 值长度:', secret?.length || 0);
|
||||
console.log(' - 值预览:', secret ? `${secret.substring(0, 10)}...` : 'undefined');
|
||||
console.log(' - 是否为占位符:', secret === 'placeholder' || secret === 'none');
|
||||
|
||||
if (!secret || secret === 'placeholder' || secret === 'none') {
|
||||
console.warn('⚠️ 警告:未设置有效的 OAUTH_CLIENT_SECRET 环境变量');
|
||||
console.warn('⚠️ 当前值:', secret);
|
||||
console.warn('⚠️ 包含 OAUTH 的环境变量:', Object.keys(process.env).filter(k => k.includes('OAUTH')));
|
||||
console.warn('⚠️ 包含 SECRET 的环境变量:', Object.keys(process.env).filter(k => k.includes('SECRET')));
|
||||
} else {
|
||||
console.log('✅ [oauth-secret.server] OAUTH_CLIENT_SECRET 已成功读取');
|
||||
}
|
||||
}
|
||||
|
||||
return secret || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器端 OAuth 配置
|
||||
*/
|
||||
export function getServerOAuthConfigRuntime() {
|
||||
const secret = getOAuthClientSecret();
|
||||
|
||||
// 从基础配置中获取其他 OAuth 参数
|
||||
const baseConfig = {
|
||||
serverUrl: process.env.NEXT_PUBLIC_OAUTH_SERVER_URL || 'http://10.79.112.85',
|
||||
clientId: process.env.NEXT_PUBLIC_OAUTH_CLIENT_ID || '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
|
||||
redirectUri: process.env.NEXT_PUBLIC_OAUTH_REDIRECT_URI || 'http://10.79.97.17/',
|
||||
appId: process.env.NEXT_PUBLIC_OAUTH_APP_ID || 'idaasoauth2',
|
||||
};
|
||||
|
||||
return {
|
||||
...baseConfig,
|
||||
clientSecret: secret
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user