{"title":"Bagaimana mengotentikasi menggunakan REMOTE_USER","version":"6.1","locale":"id","docname":"howto/auth-remote-user","url":"/id/6.1/howto/auth-remote-user/","canonical":"https://djangodocs.dev/id/6.1/howto/auth-remote-user/","summary":"This document describes how to make use of external authentication sources in your Django applications. This type of authentication solution is typically seen on…","html":"<h1>Bagaimana mengotentikasi menggunakan <code class=\"docutils literal notranslate\">REMOTE_USER</code><a class=\"heading-anchor\" href=\"#how-to-authenticate-using-remote-user\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document describes how to make use of external authentication sources in\nyour Django applications. This type of authentication solution is typically\nseen on intranet sites, with single sign-on solutions such as IIS and\nIntegrated Windows Authentication or Apache and <a class=\"reference external\" href=\"https://httpd.apache.org/docs/current/mod/mod_authnz_ldap.html\">mod_authnz_ldap</a>, <a class=\"reference external\" href=\"https://www.apereo.org/projects/cas\">CAS</a>,\n<a class=\"reference external\" href=\"https://uit.stanford.edu/service/authentication\">WebAuth</a>, <a class=\"reference external\" href=\"https://sourceforge.net/projects/mod-auth-sspi\">mod_auth_sspi</a>, etc.</p>\n<p>When the web server takes care of authentication it typically provides the\nauthenticated user as <code class=\"docutils literal notranslate\">REMOTE_USER</code>. In Django, this value is made available\nin <a class=\"reference internal\" href=\"/id/6.1/ref/request-response/#django.http.HttpRequest.META\" title=\"django.http.HttpRequest.META\"><code class=\"xref py py-attr docutils literal notranslate\">request.META</code></a> (as <code class=\"docutils literal notranslate\">REMOTE_USER</code> when\nsupplied as an environment variable, as in WSGI, or <code class=\"docutils literal notranslate\">HTTP_REMOTE_USER</code> when\nsupplied via an HTTP header, as in ASGI). Django can be configured to make use\nof the <code class=\"docutils literal notranslate\">REMOTE_USER</code> value using the <code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> or\n<code class=\"docutils literal notranslate\">PersistentRemoteUserMiddleware</code>, and\n<a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserBackend</code></a> classes found in\n<a class=\"reference internal\" href=\"/id/6.1/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.auth</code></a>.</p>\n<section id=\"configuration\">\n<h2>Pengaturan<a class=\"heading-anchor\" href=\"#configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Pertama, andaharus menambahkan <a class=\"reference internal\" href=\"/id/6.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.middleware.RemoteUserMiddleware</code></a> pada pengaturan <a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> <strong>setelah</strong> <a class=\"reference internal\" href=\"/id/6.1/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.middleware.AuthenticationMiddleware</code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>MIDDLEWARE <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.RemoteUserMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Selanjutnya, anda harus mengganti <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend\" title=\"django.contrib.auth.backends.ModelBackend\"><code class=\"xref py py-class docutils literal notranslate\">ModelBackend</code></a> dengan <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserBackend</code></a> di pengaturan <a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a></p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>AUTHENTICATION_BACKENDS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;django.contrib.auth.backends.RemoteUserBackend&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>With this setup, <code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> will detect the username in\n<code class=\"docutils literal notranslate\">request.META['REMOTE_USER']</code> (or <code class=\"docutils literal notranslate\">request.META['HTTP_REMOTE_USER']</code> under\nASGI) and will authenticate and auto-login that user\nusing the <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserBackend</code></a>.</p>\n<p>Waspada bahwa setelan khusus ini meniadakan pembuktian keaslian dengan awal <code class=\"docutils literal notranslate\">ModelBackend</code>. Ini berarti bahwa jika nilai <code class=\"docutils literal notranslate\">REMOTE_USER</code> tidak disetel kemudian pengguna tidak dapat masuk, bahkan menggunakan antarmuka admin Django. Menambahkan <code class=\"docutils literal notranslate\">'django.contrib.auth.backends.ModelBackend'</code> pada daftar <code class=\"docutils literal notranslate\">AUTHENTICATION_BACKENDS</code> akan menggunakan <code class=\"docutils literal notranslate\">ModelBackend</code> sebagai alternatif jika <code class=\"docutils literal notranslate\">REMOTE_USER</code> tidak hadir, yang akan menyelesaikan masalah ini.</p>\n<p>Pengelola pengguna Django, seperti tampilan dalam perintah pengelola <code class=\"docutils literal notranslate\">contrib.admin</code> dan the <a class=\"reference internal\" href=\"/id/6.1/ref/django-admin/#django-admin-createsuperuser\"><code class=\"xref std std-djadmin docutils literal notranslate\">createsuperuser</code></a>, tidak dipadukan dengan pengguna kendali jauh. Antarmuka ini bekerja dengan pengguna disimpan dalam basisdata tanpa memperhatikan <code class=\"docutils literal notranslate\">AUTHENTICATION_BACKENDS</code>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Sejak <code class=\"docutils literal notranslate\">RemoteUserBackend</code> warisan dari <code class=\"docutils literal notranslate\">ModelBackend</code>, anda akan masih mempunyai semua pemeriksaan perizinan sama yang diterapkan dalam <code class=\"docutils literal notranslate\">ModelBackend</code>.</p>\n<p>Pengguna dengan <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">is_active=False</code></a> tidak akan diizinkan mengotentifikasi. Gunakan <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\" title=\"django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">AllowAllUsersRemoteUserBackend</code></a> jika anda ingin mengizinkan mereka.</p>\n</aside>\n<p>If your authentication mechanism uses a custom HTTP header and not\n<code class=\"docutils literal notranslate\">REMOTE_USER</code>, you can subclass <code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> and set the\n<code class=\"docutils literal notranslate\">header</code> attribute to the desired <code class=\"docutils literal notranslate\">request.META</code> key. For example:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">mysite/middleware.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code> <span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.middleware</span> <span class=\"kn\">import</span> RemoteUserMiddleware\n\n\n <span class=\"k\">class</span> <span class=\"nc\">CustomHeaderRemoteUserMiddleware</span><span class=\"p\">(</span>RemoteUserMiddleware<span class=\"p\">):</span>\n     header <span class=\"o\">=</span> <span class=\"s2\">&quot;HTTP_AUTHUSER&quot;</span>\n</code></pre></figure>\n<p>Middleware penyesuaian digunakan dalam pengaturan <a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> daripada <a class=\"reference internal\" href=\"/id/6.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.middleware.RemoteUserMiddleware</code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>MIDDLEWARE <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;mysite.middleware.CustomHeaderRemoteUserMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</p>\n<p><code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> must not be deployed in configurations where a\nclient can supply the header. You must be sure that your web server or\nreverse proxy always sets or strips that header based on the appropriate\nauthentication checks, never permitting an end user to submit a fake (or\n&quot;spoofed&quot;) header value.</p>\n<p>Since the HTTP headers <code class=\"docutils literal notranslate\">X-Auth-User</code> and <code class=\"docutils literal notranslate\">X-Auth_User</code> (for example)\nboth normalize to the <code class=\"docutils literal notranslate\">HTTP_X_AUTH_USER</code> key in <code class=\"docutils literal notranslate\">request.META</code>, you\nmust also check that your web server doesn't allow a spoofed header using\nunderscores in place of dashes.</p>\n<p>Under WSGI, this warning doesn't apply to <code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> in its\ndefault configuration with <code class=\"docutils literal notranslate\">header = &quot;REMOTE_USER&quot;</code>, since a key that\ndoesn't start with <code class=\"docutils literal notranslate\">HTTP_</code> in <code class=\"docutils literal notranslate\">request.META</code> can only be set by your\nWSGI server, not directly from an HTTP request header.</p>\n<p>This warning applies under ASGI in all configurations, because there is\nno equivalent for a WSGI server's ability to place a trusted value in the\nenviron. ASGI deployments <em>must</em> use a reverse proxy as described above\nwhen using this middleware.</p>\n</aside>\n<p>Jika anda butuh lebih kendali, anda dapat membuat backend pembuktian keaslian sendiri yang mewarisi dari <a class=\"reference internal\" href=\"/id/6.1/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserBackend</code></a> dan menimpa satu atau lebih atribut dan metodenya.</p>\n</section>\n<section id=\"using-remote-user-on-login-pages-only\">\n<span id=\"persistent-remote-user-middleware-howto\"></span><h2>Menggunakan <code class=\"docutils literal notranslate\">REMOTE_USER</code> hanya pada halaman masuk<a class=\"heading-anchor\" href=\"#using-remote-user-on-login-pages-only\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Middleware autentifikasi <code class=\"docutils literal notranslate\">RemoteUserMiddleware</code> beranggapan bahwa kepala peminta HTTP <code class=\"docutils literal notranslate\">REMOTE_USE</code> hadir dengan semua permintaan terautentifikasi. Itu mungkin diharapkan dan praktis ketika Basic HTTP Auth dengan <code class=\"docutils literal notranslate\">htpasswd</code> atau mekanisme yang mirip digunakan, tetapi dengan Negotiate (GSSAPI/Kerberos) atau metode otentikasi sumber daya intensif lainnya, autentifikasi dalam peladen HTTP font-end biasanya hanya menyetel untuk satu atau sedikit URL masuk, dan setelah autentifikasi berhasil, aplikasi diharapkan merawat sesu autentifikasi itu sendiri.</p>\n<p><a class=\"reference internal\" href=\"/id/6.1/ref/middleware/#django.contrib.auth.middleware.PersistentRemoteUserMiddleware\" title=\"django.contrib.auth.middleware.PersistentRemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">PersistentRemoteUserMiddleware</code></a> menyediakan dukungan untuk penggunaan kasus ini. Dia akan menjaga sesi dibuktikan keasliannya sampai keluar oleh pengguna. Kelas dapat digunakan sebagai pengganti dari <a class=\"reference internal\" href=\"/id/6.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserMiddleware</code></a> dalam dokumentasi diatas.</p>\n</section>","rootId":"how-to-authenticate-using-remote-user","toc":[{"title":"Pengaturan","anchor":"configuration","children":[]},{"title":"Menggunakan REMOTE_USER hanya pada halaman masuk","anchor":"using-remote-user-on-login-pages-only","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Panduan Cara-cara","url":"/id/6.1/howto/"}],"prev":{"docname":"howto/static-files/deployment","title":"Bagaimana menyebarkan berkas tetap","url":"/id/6.1/howto/static-files/deployment/"},"next":{"docname":"howto/csp","title":"How to use Django's Content Security Policy","url":"/id/6.1/howto/csp/"},"formats":{"html":"/id/6.1/howto/auth-remote-user/","markdown":"/id/6.1/howto/auth-remote-user.md","json":"/id/6.1/howto/auth-remote-user.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/howto/auth-remote-user.txt","official":"https://docs.djangoproject.com/id/6.1/howto/auth-remote-user/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}