2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d8249367417b2b2512c40
|
2021-07-15 13:04:11 +05:30
|
|
|
title: 使用 helmet() 中间件来配置 Helmet
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 2
|
2020-09-17 03:53:22 -07:00
|
|
|
forumTopicId: 301575
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: configure-helmet-using-the-parent-helmet-middleware
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
请注意,本项目在[这个 Repl.it 项目](https://replit.com/github/freeCodeCamp/boilerplate-infosec)的基础上进行开发。你也可以从 [GitHub](https://github.com/freeCodeCamp/boilerplate-infosec/) 上克隆。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
`app.use(helmet())` 将自动包括上面介绍的所有中间件,除了 `noCache()` 和 `contentSecurityPolicy()`,但如果有必要,这些可以被启用。 你也可以使用配置对象,单独禁用或配置任何其他中间件。
|
2020-09-17 03:53:22 -07:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
**例如:**
|
2020-09-17 03:53:22 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
app.use(helmet({
|
2021-02-06 04:42:36 +00:00
|
|
|
frameguard: { // configure
|
2020-09-17 03:53:22 -07:00
|
|
|
action: 'deny'
|
|
|
|
},
|
2021-02-06 04:42:36 +00:00
|
|
|
contentSecurityPolicy: { // enable and configure
|
2020-09-17 03:53:22 -07:00
|
|
|
directives: {
|
2021-07-15 13:04:11 +05:30
|
|
|
defaultSrc: ["'self'"],
|
2020-09-17 03:53:22 -07:00
|
|
|
styleSrc: ['style.com'],
|
|
|
|
}
|
|
|
|
},
|
2021-02-06 04:42:36 +00:00
|
|
|
dnsPrefetchControl: false // disable
|
2020-09-17 03:53:22 -07:00
|
|
|
}))
|
|
|
|
```
|
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
为了教学目的和便于测试,我们分别介绍了每个中间件。 使用“父”`helmet()` 中间件很容易在真实项目中实现。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2020-09-17 03:53:22 -07:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
没有测试 - 这是一个描述性的挑战
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(true);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
*/
|
|
|
|
```
|