What HTTP code to use in “Not Authenticated” and “Not authorized” cases?

后端 未结 4 982
死守一世寂寞
死守一世寂寞 2021-02-03 23:34

I read that \"401 Unauthorized\" code must be used when a user:

  1. Is not logged, but login is required (\"not authenticated\");
  2. Is logged, but his profile d
4条回答
  •  野的像风
    2021-02-04 00:03

    Unless you intend to use HTTP authentication, the correct response is 403 ("Forbidden").

    A response code of 401 triggers the browser to display a password dialog box, and then resubmit the same request with a WWW-Authenticate header with the password data that the user supplied. That's probably not the behavior you want.

    Don't get too hung up on the explanations in the RFCs -- what you really need to pay attention to are the browser and search engine side effects of the various response codes.

    As for the "Authorization will not help" bit, in this case that is correct, since using HTTP authorization (which specifically means the WWW-Authenticate header), in fact, will not help.

    A 403 response tells the browser that the user does not have permission to make that request, and the browser should not attempt to collect authentication data and resubmit the request. That's exactly the response you're after.

提交回复
热议问题