0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gobuffaloのベーシック認証で401を返す最小の方法

Posted at

公式ページを基に実装すると

  • こちらの公式ページを基に実装すると、失敗時にhttp500が返却される。
  • 外部向けサイト等であれば、これで良いと思われる。
  • 但し、社内システム等で、一回失敗するとブラウザ再起動だと面倒。

リトライ出来るようする

  • 以下は例です。
auth := func(c buffalo.Context, u, p string) (bool, error) {
	if u == "username" && p == "password" {
		return true,nil
	}else{
		c.Response().Header().Set("WWW-Authenticate", `Basic realm="Basic Authentication"`)
		return false,c.Error(http.StatusUnauthorized, errors.New("Unauthorized"))
	}
}

app.Use(basicauth.Middleware(auth))
app.Middleware.Skip(basicauth.Middleware(auth), HomeHandler)

app.GET("/", HomeHandler)
app.GET("/add", AddMessage)
app.GET("/get", GetMessage)

本当にやりたい事はn回リトライさせる設定

  • そこまではやっていません。申し訳ございません。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?