---
title: "“REMOTE_USER”를 이용해 인증하는 방법"
version: 5.1
locale: ko
source: https://docs.djangoproject.com/ko/5.1/howto/auth-remote-user/
canonical: https://djangodocs.dev/ko/5.1/howto/auth-remote-user/
---
# “REMOTE\_USER”를 이용해 인증하는 방법

This document describes how to make use of external authentication sources
(where the web server sets the `REMOTE_USER` environment variable) in your
Django applications.  This type of authentication solution is typically seen on
intranet sites, with single sign-on solutions such as IIS and Integrated
Windows Authentication or Apache and [mod\_authnz\_ldap](https://httpd.apache.org/docs/current/mod/mod_authnz_ldap.html), [CAS](https://www.apereo.org/projects/cas), [WebAuth](https://uit.stanford.edu/service/authentication),
[mod\_auth\_sspi](https://sourceforge.net/projects/mod-auth-sspi), etc.

웹 서버에서 인증을 관리할 때 대개 내부 어플리케이션을 사용하기 위해 `REMOTE_USER` 환경 변수를 설정합니다. `REMOTE_USER` 값은 `RemoteUserMiddleware` 혹은 `PersistentRemoteUserMiddleware`에서 사용됩니다. [`django.contrib.auth`](/ko/5.1/topics/auth/#module-django.contrib.auth)에서 [`RemoteUserBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend) 클래스를 찾을 수 있습니다.

## 설정

우선,  [`django.contrib.auth.middleware.AuthenticationMiddleware`](/ko/5.1/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware)를 설정하기 전에 [`MIDDLEWARE`](/ko/5.1/ref/settings/#std-setting-MIDDLEWARE)에 [`django.contrib.auth.middleware.RemoteUserMiddleware`](/ko/5.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware)를 추가해야 합니다.

```
MIDDLEWARE = [
    "...",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.auth.middleware.RemoteUserMiddleware",
    "...",
]
```

그 다음 , [`AUTHENTICATION_BACKENDS`](/ko/5.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) 세팅에 있는 [`ModelBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend)를 [`RemoteUserBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend)로 바꿔야 합니다.

```
AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.RemoteUserBackend",
]
```

설정을 마치고 난 후, `RemoteUserMiddleware`는 유저네임을 `request.META['REMOTE_USER']`에서 찾을 수 있을 것 입니다. 그리고 [`RemoteUserBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend)를 이용하여 사용자를 증명하고 자동 로그인을 할 것입니다.

이 설정은 인증을 기본 `ModelBackend`로 사용하지 못하게 합니다. 그 말은 만약 `REMOTE_USER` 값이 설정되어 있지 않으면 Django의 관리 인터페이스를 사용한다 하더라도 사용자는 로그인할 수 없다는 것입니다. `django.contrib.auth.backends.ModelBackend`를 `AUTHENTICATION_BACKENDS` 리스트에 추가하면 `REMOTE_USER`가 없을 경우\`\`ModelBackend\`\`로 대체하여 이 문제를 해결할 수 있습니다.

`contrib.admin`의 뷰와 [`createsuperuser`](/ko/5.1/ref/django-admin/#django-admin-createsuperuser) 관리 명령과 같은 Django의 사용자 관리는 원격 사용자와 통합할 수 없습니다. 이 인터페이스들은 `AUTHENTICATION_BACKENDS`와 상관없이 데이터페이스에 저장된 사용자와만 작동합니다.

> **Note**
>
> `RemoteUserBackend`는 `ModelBackend`를 상속하기 때문에 독자는 `ModelBackend`에서 구현된 권한 확인을 모두 그대로 가지고 있습니다.
>
> Users with [`is_active=False`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.models.User.is_active) won’t be allowed to
> authenticate. Use
> [`AllowAllUsersRemoteUserBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersRemoteUserBackend) if
> you want to allow them to.

If your authentication mechanism uses a custom HTTP header and not
`REMOTE_USER`, you can subclass `RemoteUserMiddleware` and set the
`header` attribute to the desired `request.META` key.  For example:

*`mysite/middleware.py`*

```python
 from django.contrib.auth.middleware import RemoteUserMiddleware

 class CustomHeaderRemoteUserMiddleware(RemoteUserMiddleware):
     header = "HTTP_AUTHUSER"
```

This custom middleware is then used in the [`MIDDLEWARE`](/ko/5.1/ref/settings/#std-setting-MIDDLEWARE) setting
instead of [`django.contrib.auth.middleware.RemoteUserMiddleware`](/ko/5.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware):

```
MIDDLEWARE = [
    "...",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "mysite.middleware.CustomHeaderRemoteUserMiddleware",
    "...",
]
```

> **Warning**
>
> 맞춤 HTTP 헤더로 `RemoteUserMiddleware` 서브클래스를 사용한다면 매우 조심해야 합니다. 독자는 반드시 자신의 프론트엔드 웹서버가 최종 사용자가 가짜 헤더 값을 보내는 것을 허용하지 않도록 헤더를 적절한 인증 확인을 기반으로 설정하여야 합니다. HTTP 헤더 `X-Auth-User`와 `X-Auth_User` (예를 들어) 모두 `request.META`에서 `HTTP_X_AUTH_USER` 키로 표준화되기 때문에 독자는 또한 본인의 웹 서버가 밑줄을 사용한 스푸핑된 헤더를 허용하지 않도록 확인하여야 합니다.
>
> 이 경고는 `header = 'REMOTE_USER'`인 기본 설정에서 `RemoteUserMiddleware`를 적용하지 않습니다. `request.META`에서 `HTTP_`로 시작하지 않는 키는 HTTP 요청 헤더로부터 직접이 아닌 WSGI 서버를 통해서만 설정될 수 있기 때문입니다.

만약 독자가 더 많은 제어를 필요로 할 경우, [`RemoteUserBackend`](/ko/5.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend)를 상속하는 인증 백엔드를 스스로 만들어 하나 이상의 속성과 메소드를 오버라이드할 수 있습니다.

## `REMOTE_USER`는 로그인 페이지에서만 사용할 수 있습니다.

인증 미들웨어인 `RemoteUserMiddleware`는 HTTP 요청 헤더인 `REMOTE_USER`가 모든 인증된 요청과 함께 존재한다는 것을 가정합니다. 이것은 Basic HTTP Auth가 `htpasswd` 또는 기타 간단한 메커니즘들과 함께일 때 기대거나 실용적이지만,  Negotiate(GSSAPI / Kerberos) 또는 기타 자원 집약적인 인증 방법과 함께 일 때는, 프런트엔드 HTTP 서버에서의 인증은 일반적으로 하나 또는 몇 개의 로그인 URL을 설정해야하고, 인증이 성공하면 응용 프로그램은 인증된 세션 자체가 유지 관리된다라는 조건이여야합니다.

[`PersistentRemoteUserMiddleware`](/ko/5.1/ref/middleware/#django.contrib.auth.middleware.PersistentRemoteUserMiddleware)는 사용 사례에 대한 지원을 제공합니다. 이것은 사용자에 의하여 명시적으로 로그아웃할 때까지 인증된 세션을 유지 관리할 것 입니다. 이 클래스는 위의 문서에서 [`RemoteUserMiddleware`](/ko/5.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware) 일시 대체품으로 사용되어질 수 있습니다.
