王良
8 months ago
committed by
GitHub
6 changed files with 62 additions and 14 deletions
@ -1,2 +0,0 @@
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const start = require('./start/index.js') |
@ -0,0 +1,52 @@
|
||||
const defaultAllowHeaders = '*' |
||||
const defaultAllowMethods = 'GET,POST,PUT,DELETE,HEAD,OPTIONS,PATCH' // CONNECT、TRACE被认为是不安全的请求,通常不建议允许跨域
|
||||
|
||||
function readConfig (config, defaultConfig) { |
||||
if (config) { |
||||
if (Object.isArray(config)) { |
||||
config = config.join(',') |
||||
} |
||||
} else { |
||||
config = defaultConfig |
||||
} |
||||
return config |
||||
} |
||||
|
||||
module.exports = { |
||||
requestIntercept (context, interceptOpt, req, res, ssl, next) { |
||||
const { rOptions, log } = context |
||||
|
||||
// 不是 OPTIONS 请求,或请求头中不含 origin 时,跳过当前拦截器
|
||||
if (rOptions.method !== 'OPTIONS' || rOptions.headers.origin == null) { |
||||
return |
||||
} |
||||
|
||||
// 从请求头中获取跨域相关信息;如果不存在,则从配置中获取的值;如果还不存在,则使用默认值
|
||||
const allowHeaders = rOptions.headers['access-control-request-headers'] || readConfig(interceptOpt.optionsAllowHeaders, defaultAllowHeaders) |
||||
const allowMethods = rOptions.headers['access-control-request-method'] || readConfig(interceptOpt.optionsAllowMethods, defaultAllowMethods) |
||||
|
||||
const headers = { |
||||
// 允许跨域
|
||||
'Dev-Sidecar-Interceptor': 'options', |
||||
'Access-Control-Allow-Origin': rOptions.headers.origin, |
||||
'Access-Control-Allow-Headers': allowHeaders, |
||||
'Access-Control-Allow-Methods': allowMethods, |
||||
'Access-Control-Max-Age': interceptOpt.optionsMaxAge > 0 ? interceptOpt.optionsMaxAge : 2592000, // 默认有效一个月
|
||||
Date: new Date().toUTCString() |
||||
} |
||||
|
||||
// 判断是否允许
|
||||
if (interceptOpt.optionsCredentials !== false && interceptOpt.optionsCredentials !== 'false') { |
||||
headers['Access-Control-Allow-Credentials'] = 'true' |
||||
} |
||||
|
||||
res.writeHead(200, headers) |
||||
res.end() |
||||
|
||||
log.info('options intercept:', (rOptions.original || rOptions).url) |
||||
return true // true代表请求结束
|
||||
}, |
||||
is (interceptOpt) { |
||||
return !!interceptOpt.options |
||||
} |
||||
} |
Loading…
Reference in new issue