为应用程序级策略提供错误 403 页面是一种不好的做法吗?

发布于 2024-12-12 06:21:00 字数 234 浏览 0 评论 0原文

假设我有一个网站,允许任何人通过 oauth 或类似方式登录,但只允许某些用途创建或修改内容。如果他们以某种方式请求创建新帖子的页面,我将进行检查并在他们没有适当权限的情况下重定向他们。

在这种情况下重定向到“403错误”页面是否可以接受?没有带有 403 状态代码的实际 HTTP 响应,没有失败的数据库或服务器级查询 - 只有我的业务逻辑。如果我提供带有特定解释​​性消息的错误 403 页面,我是否盗用了 HTTP 状态代码的概念?

Say I have a website that allows anyone to log in through oauth or similar, but only allows certain uses to create or modify content. Should they somehow make a request for page for creating a new post, I'll do a check and redirect them if they don't have the appropriate permissions.

It is considered acceptable to redirect to the "403 Error" page in this situation? There was no actual HTTP response with a 403 status code, there was no database- or server- level query that was failed - just my business logic. Am I misappropriating the idea of HTTP status codes if I serve an error 403 page with a specific explanatory message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2024-12-19 06:21:00

您可以自由地这样做,但我认为如果您想公开 API,您可以使用实际的 403 响应,因为它们携带的含义将由客户端很好地处理。

如果您想向客户端显示页面并使用重定向,您将失去“403”的含义。

将它们重定向到解释页面而不包含“403”代码不是更好吗?或者更好的是,将他们重定向到更有用的地方,例如注册页面(如果他们必须这样做才能发帖),或者返回带有浮动消息的原始页面。

我们希望帮助用户更接近他们的目标,而不是让他们与技术错误代码混淆。

You are free to do so, but I think if you want to expose an API you would use an actual 403 response because they carry meaning that will be nicely handled by the client.

If you want to display a page to the client and will be using redirect, you will lose this meaning of the "403".

Isn't it better to just redirect them to an explanation page without including the "403" code. Or better yet, redirect them to a more helpful place, like the sign up page if that is what they have to do to make a post, or back to the original page with a floating message.

We want to help the user get closer to their goals instead of confusing them with technical error codes.

虐人心 2024-12-19 06:21:00

关于这个话题经常有很多讨论,最终归结为以下选择:

  • 5xx?当然不是。这不是服务器错误。
  • 400?不完全是,这不是一个格式错误的请求。
  • 401?可能不是,401一般用于一般授权,而不是应用程序级别的权限。如果您的用户已登录但角色错误,并且您想让用户知道,请使用其他内容。
  • 404?也许,因为服务器无法找到该特定用户的资源,但如果您想告诉用户“这样的资源可用,但因为缺乏权限而无法拥有它”然后去做别的事情。
  • 403?其实,这句话很有道理。这是 RFC 中的定义
    <块引用>

    403 Forbidden 服务器理解该请求,但拒绝满足该请求。授权不会有任何帮助,并且不应重复请求。如果请求方法不是 HEAD 并且服务器希望公开为什么请求没有被满足,它应该在实体中描述拒绝的原因。如果服务器不希望将此信息提供给客户端,则可以使用状态代码 404(未找到)。

在您的问题中,您提到您打算重定向用户。如果您正在制作RESTFUL Web服务,那么只需返回403。如果您正在制作整个Web应用程序,您可以控制403并重定向......

There is often a lot of discussion about this very topic and it comes down to the following choices:

  • a 5xx? Of course not. This is not a server error.
  • a 400? Not really, it wasn't a malformed request.
  • a 401? Probably not, 401 is generally for authorization in general, not application-level permissions. If your user has already logged in but has the wrong role, and you want to let the user know, then use something else.
  • a 404? Perhaps, as the server can't find the resource for this particular user, but if you want to tell the user "well such a resource is available but you can't have it because you lack permissions" then go with something else.
  • a 403? Actually, this one makes a lot of sense. Here is the definition from the RFC

    403 Forbidden The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

In your question you mention your intention to redirect the user. If you are making a RESTFUL web service then just return the 403. If you are doing an entire web app, you can control the 403 and redirect....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文