mirror of
https://github.com/jakejarvis/npqueue.git
synced 2026-07-26 13:06:03 -04:00
69 lines
2.1 KiB
Markdown
69 lines
2.1 KiB
Markdown
# Secure
|
|
|
|
[](https://travis-ci.org/gin-contrib/secure)
|
|
[](https://codecov.io/gh/gin-contrib/secure)
|
|
[](https://goreportcard.com/report/github.com/gin-contrib/secure)
|
|
[](https://godoc.org/github.com/gin-contrib/secure)
|
|
|
|
Secure meddleware for [Gin](https://github.com/gin-gonic/gin/) framework.
|
|
|
|
## Example
|
|
|
|
See the [example1](example/code1/example.go), [example2](example/code2/example.go).
|
|
|
|
DefaultConfig returns a Configuration with strict security settings
|
|
|
|
[embedmd]:# (secure.go go /func DefaultConfig/ /^}$/)
|
|
```go
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
SSLRedirect: true,
|
|
IsDevelopment: false,
|
|
STSSeconds: 315360000,
|
|
STSIncludeSubdomains: true,
|
|
FrameDeny: true,
|
|
ContentTypeNosniff: true,
|
|
BrowserXssFilter: true,
|
|
ContentSecurityPolicy: "default-src 'self'",
|
|
IENoOpen: true,
|
|
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
|
}
|
|
}
|
|
```
|
|
|
|
[embedmd]:# (example/code1/example.go go)
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"github.com/gin-contrib/secure"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
router := gin.Default()
|
|
|
|
router.Use(secure.New(secure.Config{
|
|
AllowedHosts: []string{"example.com", "ssl.example.com"},
|
|
SSLRedirect: true,
|
|
SSLHost: "ssl.example.com",
|
|
STSSeconds: 315360000,
|
|
STSIncludeSubdomains: true,
|
|
FrameDeny: true,
|
|
ContentTypeNosniff: true,
|
|
BrowserXssFilter: true,
|
|
ContentSecurityPolicy: "default-src 'self'",
|
|
IENoOpen: true,
|
|
ReferrerPolicy: "strict-origin-when-cross-origin",
|
|
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
|
}))
|
|
|
|
router.GET("/ping", func(c *gin.Context) {
|
|
c.String(200, "pong")
|
|
})
|
|
|
|
// Listen and Server in 0.0.0.0:8080
|
|
router.Run()
|
|
}
|
|
```
|