로그인 페이지로 리디렉션 할 때 올바른 HTTP 상태 코드는 무엇입니까?
사용자가 로그인하지 않고 로그인이 필요한 페이지에 액세스하려고하면 로그인 페이지로 리디렉션하기위한 올바른 HTTP 상태 코드는 무엇입니까?
W3C에 의해 설정된 3xx 응답 코드 중 어느 것도 요구 사항에 맞지 않는 것 같습니다 .
10.3.1 300 객관식
요청 된 자원은 각각 고유 한 위치를 가진 일련의 표현 중 하나에 해당하며, 사용자 주도의 협상 정보 (섹션 12)가 제공되어 사용자 (또는 사용자 에이전트)가 선호하는 표현을 선택하고 경로를 재 지정할 수 있습니다. 해당 위치에 요청하십시오.
HEAD 요청이 아닌 한, 응답은 사용자 또는 사용자 에이전트가 가장 적합한 것을 선택할 수있는 자원 특성 및 위치 목록을 포함하는 엔티티를 포함해야한다. 엔터티 형식은 내용 형식 헤더 필드에 지정된 미디어 유형으로 지정됩니다. 형식과 기능에 따라
가장 적합한 선택의 선택은 자동으로 수행 될 수있다. 그러나이 사양에서는 이러한 자동 선택에 대한 표준을 정의하지 않습니다.
서버가 선호하는 표현 방식을 선택한다면, 해당 표현에 대한 특정 URI를 Location 필드에 포함시켜야합니다. 사용자 에이전트는 자동 리디렉션을 위해 위치 필드 값을 사용할 수 있습니다. 달리 명시하지 않는 한이 응답은 캐시 가능합니다.
10.3.2 301 영구적으로 이동
요청 된 리소스에 새로운 영구 URI가 할당되었으며이 리소스에 대한 이후의 참조는 반환 된 URI 중 하나를 사용해야합니다. 링크 편집 기능이있는 클라이언트는 요청 URI에 대한 참조를 가능한 경우 서버가 리턴 한 하나 이상의 새 참조에 자동으로 다시 링크해야합니다. 달리 명시하지 않는 한이 응답은 캐시 가능합니다.
새로운 영구 URI는 응답의 Location 필드에서 제공해야합니다. 요청 방법이 HEAD가 아닌 한, 응답 엔티티는 새로운 URI에 대한 하이퍼 링크와 함께 짧은 하이퍼 텍스트 노트를 포함해야한다.
GET 또는 HEAD 이외의 요청에 대한 응답으로 301 상태 코드가 수신되면, 사용자 에이전트는 요청이 발행 된 조건을 변경할 수 있으므로 사용자가 확인할 수 없으면 요청을 자동으로 리디렉션해서는 안됩니다.
Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.
10.3.3 302 발견
요청 된 리소스가 일시적으로 다른 URI에 있습니다. 경우에 따라 리디렉션이 변경 될 수 있으므로 클라이언트는 향후 요청에 계속 Request-URI를 사용해야합니다. 이 응답은 Cache-Control 또는 Expires 헤더 필드로 표시된 경우에만 캐시 가능합니다.
임시 URI는 응답의 Location 필드에서 제공해야합니다. 요청 방법이 HEAD가 아닌 한, 응답 엔티티는 새로운 URI에 대한 하이퍼 링크와 함께 짧은 하이퍼 텍스트 노트를 포함해야한다.
GET 또는 HEAD 이외의 요청에 대한 응답으로 302 상태 코드가 수신되면, 사용자 에이전트는 요청이 발행 된 조건을 변경할 수 있으므로 사용자가 확인할 수없는 경우 요청을 자동으로 리디렉션해서는 안됩니다.
Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it
원래 요청 방법에 관계없이 Location 필드 값에 대해 GET을 수행하는 303 응답이었습니다. 클라이언트에 어떤 종류의 반응이 예상되는지 명확하게 나타내려는 서버에 대해 상태 코드 303 및 307이 추가되었습니다.
10.3.4 303 다른 참조
요청에 대한 응답은 다른 URI에서 찾을 수 있으며 해당 리소스의 GET 메소드를 사용하여 검색해야합니다. 이 방법은 주로 POST 활성화 스크립트의 출력이 사용자 에이전트를 선택된 리소스로 리디렉션하도록 허용합니다. 새로운 URI는 원래 요청 된 자원의 대체 참조가 아닙니다. 303 응답은 캐시해서는 안되지만 두 번째 (리디렉션 된) 요청에 대한 응답은 캐시 할 수 있습니다.
응답의 위치 필드에서 다른 URI를 제공해야합니다. 요청 방법이 HEAD가 아닌 한, 응답 엔티티는 새로운 URI에 대한 하이퍼 링크와 함께 짧은 하이퍼 텍스트 노트를 포함해야한다.
Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.
10.3.5 304 수정되지 않음
클라이언트가 조건부 GET 요청을 수행하고 액세스가 허용되었지만 문서가 수정되지 않은 경우 서버는이 상태 코드로 응답해야합니다. 304 응답은 메시지 본문을 포함해서는 안되므로 헤더 필드 다음의 첫 번째 빈 줄로 항상 종료됩니다.
응답에는 다음 헤더 필드가 포함되어야합니다.
- Date, unless its omission is required by section 14.18.1 If a
시계없는 오리진 서버는 이러한 규칙을 준수하며 프록시와 클라이언트는 하나없이 수신 된 응답 ([RFC 2068], 섹션 14.19에 따름)에 자신의 날짜를 추가하면 캐시가 올바르게 작동합니다.
- ETag and/or Content-Location, if the header would have been sent in a 200 response to the same request - Expires, Cache-Control, and/or Vary, if the field-value might differ from that sent in any previous response for the same variant If the conditional GET used a strong cache validator (see
section 13.3.3), the response SHOULD NOT include other entity-headers. Otherwise (i.e., the conditional GET used a weak validator), the response MUST NOT include other entity-headers; this prevents inconsistencies between cached entity-bodies and updated headers.
If a 304 response indicates an entity not currently cached, then the cache MUST disregard the response and repeat the request without the conditional.
If a cache uses a received 304 response to update a cache entry, the cache MUST update the entry to reflect any new field values given in the response.
10.3.6 305 Use Proxy
The requested resource MUST be accessed through the proxy given by the Location field. The Location field gives the URI of the proxy. The recipient is expected to repeat this single request via the proxy. 305 responses MUST only be generated by origin servers.
Note: RFC 2068 was not clear that 305 was intended to redirect a single request, and to be generated by origin servers only. Not observing these limitations has significant security consequences.
10.3.7 306 (Unused)
The 306 status code was used in a previous version of the specification, is no longer used, and the code is reserved.
10.3.8 307 Temporary Redirect
The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.
If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
I'm using 302 for now, until I find the correct answer.
Update & conclusion:
HTTP 302 is better since its known to have best compatibility with clients/browsers.
I'd say
303 see other
302 Found:
The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
fits a login page most closely in my opinion. I initially considered 303 see other
which would work just as well. After some thought, I'd say 302 Found
is more fitting because the requested resource was found, there just is another page to go through before it can be accessed. The response doesn't get cached by default which is fine as well.
This is a misuse of HTTP redirection mechanism. If user is not authorized then your app must return 401 Unauthorized
. In case that the user is authorized but does not have an access to the requested resource then 403 Forbidden
must be returned.
You should do the redirect on client side, e.g. by javascript. status code for redirection because required authorization does not exist. Using 30x for this does not conform to HTTP.
How to Think About HTTP Status Codes by Mark Nottingham
401 Unauthorized triggers HTTP’s request authentication mechanism.
401 Unauthorized
status code requires presence of WWW-Authenticate
header that supports various authentication types:
WWW-Authenticate: <type> realm=<realm>
Bearer, OAuth, Basic, Digest, Cookie, etc
- Hypertext Transfer Protocol (HTTP) Authentication Scheme Registry
- Cookie-based HTTP Authentication - DRAFT
I think the appropriate solution is the HTTP 401 (Not Authorized) header.
http://en.wikipedia.org/wiki/HTTP_codes#4xx_Client_Error
The purpose of this header is exactly this. But, instead of redirecting to a login page, the correct process would be something like:
- User not logged try to access a login-restricted page.
- system identifies user is not logged
- system returns HTTP 401 header, AND display the login form in the same response (not a redirect).
This is a good practice, like providing a useful 404 page, with sitemap links, and a search form for example.
See you.
I had rare cases where the Firefox browser cached the 302 redirect. That is the reason why I'm using 307 for login pages and e.g. redirects to the newest article/post/comment/etc.
If you are using 302, don't forget to double check that caching is disabled:
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Cache-Control: post-check=0, pre-check=0', false);
'Programming' 카테고리의 다른 글
인터페이스, 구현 또는 둘 다를 언급하십니까? (0) | 2020.07.17 |
---|---|
wix 'KeyPath'속성은 무엇입니까? (0) | 2020.07.17 |
Android에서 어댑터의 역할은 무엇입니까? (0) | 2020.07.17 |
Maven 종속성을 확인할 수 없습니다, 아티팩트를 해결할 수 없습니다 (0) | 2020.07.17 |
SQL-다 대다 테이블 기본 키 (0) | 2020.07.17 |