-
Notifications
You must be signed in to change notification settings - Fork 568
/
configuration.ts
60 lines (58 loc) 路 1.82 KB
/
configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
Configuration,
Inject,
Config,
MidwayApplicationManager,
} from '@midwayjs/core';
import * as DefaultConfig from './config/config.default';
import { CsrfMiddleware } from './middleware/csrf.middleware';
import { SecurityOptions } from './interface';
import { XFrameMiddleware } from './middleware/xframe.middleware';
import { HSTSMiddleware } from './middleware/hsts.middleware';
import { NoOpenMiddleware } from './middleware/noopen.middleware';
import { NoSniffMiddleware } from '.';
import { XSSProtectionMiddleware } from './middleware/xssProtection.middleware';
import { CSPMiddleware } from './middleware/csp.middleware';
import { SecurityHelper } from './middleware/helper';
@Configuration({
namespace: 'security',
importConfigs: [
{
default: DefaultConfig,
},
],
})
export class SecurityConfiguration {
@Inject()
applicationManager: MidwayApplicationManager;
@Config('security')
security: SecurityOptions;
async onReady() {
this.applicationManager
.getApplications(['koa', 'faas', 'express', 'egg'])
.forEach(app => {
app.useMiddleware(SecurityHelper);
if (this.security.csrf?.enable) {
app.useMiddleware(CsrfMiddleware);
}
if (this.security.csp?.enable) {
app.useMiddleware(CSPMiddleware);
}
if (this.security.xframe?.enable) {
app.useMiddleware(XFrameMiddleware);
}
if (this.security.hsts?.enable) {
app.useMiddleware(HSTSMiddleware);
}
if (this.security.noopen?.enable) {
app.useMiddleware(NoOpenMiddleware);
}
if (this.security.nosniff?.enable) {
app.useMiddleware(NoSniffMiddleware);
}
if (this.security.xssProtection?.enable) {
app.useMiddleware(XSSProtectionMiddleware);
}
});
}
}