亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何忽略一個路由器進行使用Echo框架進行BasicAuth檢查?

如何忽略一個路由器進行使用Echo框架進行BasicAuth檢查?

Go
開心每一天1111 2022-08-15 17:35:17
為了使用基本的身份驗證來保護整個應用程序,這種方式可以對所有路由器都做些什么:package mainimport (    "crypto/subtle"    "net/http"    "github.com/labstack/echo/v4"    "github.com/labstack/echo/v4/middleware")type Message struct {    Status string `json:"status"`}func main() {    e := echo.New()    e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {        if subtle.ConstantTimeCompare([]byte(username), []byte("username")) == 1 &&            subtle.ConstantTimeCompare([]byte(password), []byte("password")) == 1 {            return true, nil        }        return false, nil    }))    e.GET("/", func(c echo.Context) error {        return c.String(http.StatusOK, "Hello, World!")    })    e.GET("/hello", func(c echo.Context) error {        return c.String(http.StatusOK, "Hello")    })    e.GET("/healthcheck", func(c echo.Context) error {        u := &Message{            Status: "OK",        }        return c.JSON(http.StatusOK, u)    })    e.Logger.Fatal(e.Start(":8080"))}如果要忽略一個路由器 - 到其目標,該怎么辦?/healthcheck如果設置為除 /healthcheck 以外的每個路由器將解決此問題,但它需要許多代碼。e.Use(middleware.BasicAuth)
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

您可以對路由進行分組


package main


import (

    "crypto/subtle"

    "net/http"


    "github.com/labstack/echo/v4"

    "github.com/labstack/echo/v4/middleware"

)


type Message struct {

    Status string `json:"status"`

}


func main() {

    e := echo.New()

    g := e.Group("/")

    g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {

        if subtle.ConstantTimeCompare([]byte(username), []byte("username")) == 1 &&

            subtle.ConstantTimeCompare([]byte(password), []byte("password")) == 1 {

            return true, nil

        }

        return false, nil

    }))


    g.GET("/", func(c echo.Context) error {

        return c.String(http.StatusOK, "Hello, World!")

    })


    e.GET("/healthcheck", func(c echo.Context) error {

        u := &Message{

            Status: "OK",

        }

        return c.JSON(http.StatusOK, u)

    })

    e.Logger.Fatal(e.Start(":8080"))

}


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號