更改基本 HTTP 身份验证领域和登录对话框消息

Change Basic HTTP Authentication realm and login dialog message

我想更改在执行 Basic Auth.The 期间弹出的消息,当前默认消息是:

Server requires a username and password.

对我来说更准确的是:

Server requires an Email and Password.

我的问题是我找不到或不知道此消息的设置位置以及是否可以更改。大多数在线问题都是关于 Basic Auth 实现的,但这不是我的问题——我可以很好地实现它。我只是需要更准确的用户响应。

以下是我如何使用 echo 强制进行身份验证 window:

c.Response().Header().Set(echo.HeaderWWWAuthenticate, `Basic realm="Your Email is your Username"`)
return echo.ErrUnauthorized

注意:只有 Firefox 显示领域消息。 Chrome 和 Opera 都没有。

这与 Go 无关,但实际上与接收 header 时的浏览器行为有关。

似乎 Chrome/Chromium 有一个与此相关的已知问题,与开发团队认为不安全的功能有关,所以我认为除非您求助,否则您无法自行修复它到其他一些身份验证机制。

详情请看这里:

https://bugs.chromium.org/p/chromium/issues/detail?id=544244#c32

感谢您的回复,但并不令人满意。我必须阅读有关此主题的一些内容。

正确答案是登录 prompt/dialog 是内置于 user-agent/browser 的响应,服务器无法更改。这也解释了为什么有些浏览器显示领域而其他浏览器不显示领域。

根据 Wikipedia Basic access authentication 所有服务器所做的是:

When the server wants the user agent to authenticate itself towards the server, it must respond appropriately to unauthenticated requests.

Unauthenticated requests should return a response whose header contains a HTTP 401 Unauthorized status[4] and a WWW-Authenticate field.[5]

The WWW-Authenticate field for basic authentication (used most often) is constructed as following:

WWW-Authenticate: Basic realm="User Visible Realm"