首页 > 解决方案 > 使用 Django cookiecutter 使用 Traefik 和 Docker 进行基本身份验证

问题描述

我将 Docker 与 Traefik 和 Django cookiecutter 一起使用。我想向它添加基本身份验证以屏蔽它。我的 docker compose yaml 文件如下所示:

version: '3'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_traefik: {}

services:
  ...

  traefik:
    build:
      context: .
      dockerfile: ./compose/production/traefik/Dockerfile
    image: myod_production_traefik
    depends_on:
      - django
    volumes:
      - production_traefik:/etc/traefik/acme:z
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.auth.basicauth.users=myod:$$2y$$05$......"

  ...

但是,当我运行时docker-compose -f production.yml up,我仍然可以访问该网站而无需进行身份验证。我错过了什么?

标签: dockerbasic-authenticationtraefik

解决方案


您必须使用中间件启用 HTTP 基本身份验证。所以你应该添加标签:

traefik.http.returs.your-name-for-https-route.middlewares=auth

至少根据这个https://blog.creekorful.com/2020/01/how-to-expose-traefik-2-dashboard-securely-docker-swarm/你应该可以按照你的想象去做。


推荐阅读