Skip to content

Commit

Permalink
Merge pull request #104 from wangle201210/add/api-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Dec 5, 2024
2 parents 5c7b1ce + 0d7dfec commit bdeac5c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
23 changes: 22 additions & 1 deletion docs/docs/WEB服务开发/接口文档/接口文档-OpenAPIv3.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,25 @@ func main() {

![](/markdown/452372a121db73abd8c5027077b3026e.png)

我们可以发现通过通用的 `OpenAPIv3` 对象我们可以自定义修改其内容,并且根据它生成其他各种自定义类型的接口文档。
我们可以发现通过通用的 `OpenAPIv3` 对象我们可以自定义修改其内容,并且根据它生成其他各种自定义类型的接口文档。

## 六、添加api.json(swagger)自定义鉴权

对于需要进行api文档鉴权的情况,可以使用 `ghttp.BindHookHandler` 方法对 `s.GetOpenApiPath()` 路由绑定前置方法进行鉴权,示例如下:

``` go
func main() {
s := g.Server()
// if api.json requires authentication, add openApiBasicAuth handler
s.BindHookHandler(s.GetOpenApiPath(), ghttp.HookBeforeServe, openApiBasicAuth)
s.Run()
}

func openApiBasicAuth(r *ghttp.Request) {
if !r.BasicAuth("OpenApiAuthUserName", "OpenApiAuthPass", "Restricted") {
r.ExitAll()
return
}
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,26 @@ The core API information has already been automatically generated. If developers

![](/markdown/452372a121db73abd8c5027077b3026e.png)

From this, we can see that with the general `OpenAPIv3` object, we can customize its content and generate various other types of custom API documentation based on it.
From this, we can see that with the general `OpenAPIv3` object, we can customize its content and generate various other types of custom API documentation based on it.


## VI、Add api.json(swagger) custom authentication

For situations where api document authentication is required, you can use the `ghttp.BindHookHandler` method to authenticate the `s.GetOpenApiPath()` routing binding pre-method. The example is as follows:

``` go
func main() {
s := g.Server()
// if api.json requires authentication, add openApiBasicAuth handler
s.BindHookHandler(s.GetOpenApiPath(), ghttp.HookBeforeServe, openApiBasicAuth)
s.Run()
}

func openApiBasicAuth(r *ghttp.Request) {
if !r.BasicAuth("OpenApiAuthUserName", "OpenApiAuthPass", "Restricted") {
r.ExitAll()
return
}
}

```

0 comments on commit bdeac5c

Please sign in to comment.