---
title: "Django의 콘텐츠 보안 정책을 사용하는 방법"
version: 6.1
locale: ko
source: https://docs.djangoproject.com/ko/6.1/howto/csp/
canonical: https://djangodocs.dev/ko/6.1/howto/csp/
---
# Django의 콘텐츠 보안 정책을 사용하는 방법

## 기본 구성

Django 프로젝트에서 콘텐츠 보안 정책(CSP)을 활성화하려면 다음을 따르세요.

1. [`MIDDLEWARE`](/ko/6.1/ref/settings/#std-setting-MIDDLEWARE) 설정에 CSP 미들웨어를 추가합니다:

   ```
   MIDDLEWARE = [
       # ...
       "django.middleware.csp.ContentSecurityPolicyMiddleware",
       # ...
   ]
   ```
2. settings.py\`\`에서 [`SECURE_CSP`](/ko/6.1/ref/settings/#std-setting-SECURE_CSP) 또는 [`SECURE_CSP_REPORT_ONLY`](/ko/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY) 중 하나(또는 둘 다)를 사용하여 CSP 정책을 설정합니다. :ref:CSP 설정 문서 \<csp-settings\>\`에서는 두 설정의 차이점을 자세히 설명합니다:

   ```
   from django.utils.csp import CSP

   # To enforce a CSP policy:
   SECURE_CSP = {
       "default-src": [CSP.SELF],
       # Add more directives to be enforced.
   }

   # Or for report-only mode:
   SECURE_CSP_REPORT_ONLY = {
       "default-src": [CSP.SELF],
       # Add more directives as needed.
       "report-uri": "/path/to/reports-endpoint/",
   }
   ```

## 논스 구성

CSP 정책에서 논스를 사용하려면 기본 구성 외에도 다음을 수행해야 합니다.

1. CSP 설정에 [`NONCE`](/ko/6.1/ref/csp/#django.utils.csp.CSP.NONCE) 플레이스홀더 값을 추가하세요. 이는 `script-src` 또는 `style-src` 지시문에만 적용됩니다:

   ```
   from django.utils.csp import CSP

   SECURE_CSP = {
       "default-src": [CSP.SELF],
       # Allow self-hosted scripts and script tags with matching `nonce` attr.
       "script-src": [CSP.SELF, CSP.NONCE],
       # Example of the less secure 'unsafe-inline' option.
       "style-src": [CSP.SELF, CSP.UNSAFE_INLINE],
   }
   ```
2. [`TEMPLATES`](/ko/6.1/ref/settings/#std-setting-TEMPLATES) 설정에 [`csp()`](/ko/6.1/ref/templates/api/#django.template.context_processors.csp) 컨텍스트 프로세서를 추가하세요. 이렇게 하면 생성된 논스 값을 Django 템플릿에서 `csp_nonce` 컨텍스트 변수로 사용할 수 있습니다:

   ```
   TEMPLATES = [
       {
           "BACKEND": "django.template.backends.django.DjangoTemplates",
           "OPTIONS": {
               "context_processors": [
                   # ...
                   "django.template.context_processors.csp",
               ],
           },
       },
   ]
   ```
3. In your templates, add the nonce to elements that require it:

   For inline `<style>` or `<script>` tags, use the `csp_nonce` context
   variable directly:

   ```html+django
   <style nonce="{{ csp_nonce }}">
     /* These inline styles will be allowed. */
   </style>

   <script nonce="{{ csp_nonce }}">
     // This inline JavaScript will be allowed.
   </script>
   ```

   For external `<script src="...">` and `<link rel="stylesheet">`
   elements, use the [`csp_nonce_attr`](/ko/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr) template tag:

   ```html+django
   <script src="/path/to/script.js" {% csp_nonce_attr %}></script>
   <link rel="stylesheet" href="/path/to/style.css" {% csp_nonce_attr %}>
   ```

   To render a [`Media`](/ko/6.1/topics/forms/media/#django.forms.Media) object’s assets with the nonce
   applied to each element, pass the object to the [`csp_nonce_attr`](/ko/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr) tag:

   ```html+django
   {% csp_nonce_attr form.media %}
   ```

   > **Changed in Django 6.1**
   >
   > The [`csp_nonce_attr`](/ko/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr) template tag was added, including support for
   > rendering [`Media`](/ko/6.1/topics/forms/media/#django.forms.Media) objects.

> **캐싱과 논스 재사용**
>
> ``` ContentSecurityPolicyMiddleware`는 고유한 논스를 생성하고 템플릿에서 논스가 사용될 때 ``Content-Security-Policy` ``` (또는 `Content-Security-Policy-Report-Only`) 헤더에 적절한 `nonce-<value>` 소스 표현식을 추가하는 작업을 자동으로 처리합니다.
>
> 올바르게 동작하도록 하려면 HTML과 헤더가 모두 동일한 요청에서 생성되고, 캐시에서 제공되지 않아야 합니다. 구현 상세 정보와 중요한 캐싱 관련 고려 사항은 [Nonce usage](/ko/6.1/ref/csp/#csp-nonce) 참고 문서를 확인하세요.
