src/app.ts
const app = express();
app.use(rTracer.expressMiddleware());
app.use(requestLogger);
app.use(helmet());
app.use(
cors({
credentials: true,
origin: getAllowedOrigins,
}),
);
src/cors.ts
import { NODE_ENV } from 'common/constants';
export function getAllowedOrigins(
origin: string | undefined,
callback: Function,
) {
let allowedOrigins: string[] = [];
if (NODE_ENV === 'development') {
allowedOrigins = [
'http://localhost:3000',
'http://localhost:8000',
'http://localhost:34273',
];
} else if (NODE_ENV === 'test') {
allowedOrigins = ['http://localhost:3000'];
} else if (NODE_ENV === 'production') {
allowedOrigins = ['https://www.somewebsite.com', 'https://somewebsite.com'];
}
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
return callback(null, true);
} else {
return callback(new Error(origin + ' not allowed by CORS'));
}
}
src/app.ts
const app = express();
app.use(rTracer.expressMiddleware());
app.use(requestLogger);
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
styleSrc: ["'self'", 'https:', "'unsafe-inline'"],
baseUri: ["'self'"],
fontSrc: ["'self'", 'https:', 'data:'],
},
},
}),
);
根据该人的解决方案修改了我的 app.ts,让我们再试一次
src/app.ts
const app = express();
app.use(rTracer.expressMiddleware());
app.use(requestLogger);
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
styleSrc: ["'self'", 'https:', "'unsafe-inline'"],
baseUri: ["'self'"],
fontSrc: ["'self'", 'https:', 'data:'],
},
},
}),
);
app.use(
cors({
credentials: true,
origin: getAllowedOrigins,
}),
);
天哪,
src/app.ts
const app = express();
app.use(rTracer.expressMiddleware());
app.use(requestLogger);
app.use(
cors({
credentials: true,
origin: getAllowedOrigins,
}),
);
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
styleSrc: ["'self'", 'https:', "'unsafe-inline'"],
baseUri: ["'self'"],
fontSrc: ["'self'", 'https:', 'data:'],
},
},
}),
);
天哪,我们又
const app = express();
app.use(rTracer.expressMiddleware());
app.use(requestLogger);
app.use(
cors({
credentials: true,
origin: getAllowedOrigins,
}),
);
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false,
crossOriginResourcePolicy: false,
originAgentCluster: false,
referrerPolicy: false,
strictTransportSecurity: false,
xContentTypeOptions: false,
xDnsPrefetchControl: false,
xDownloadOptions: false,
xFrameOptions: false,
xPermittedCrossDomainPolicies: false,
xXssProtection: false,
}),
);
这可行,但我不知道为什么