目录

「Authelia」我家大门常打开

配置 Authelia 给网站加认证。

部署

基于 Docker 进行容器化部署非常方便。

容器配置

services:
  authelia:
    image: authelia/authelia:latest
    container_name: authelia
    # profiles: ["disable"]
    restart: unless-stopped
    networks:
      - traefik_proxy
    ports:
      # - "9091:9091"  # only for debug, must access by traefik
    volumes:
      - /path/to/your/authelia/config:/config
    environment:
      - TZ=Asia/Shanghai
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.authelia.rule=Host(`authelia.aio.yirami.xyz`)"
      - "traefik.http.routers.authelia.entrypoints=websecure"
      - "traefik.http.routers.authelia.tls=true"
      - "traefik.http.routers.authelia.tls.certresolver=le"
      - "traefik.http.services.authelia.loadbalancer.server.port=9091"
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
        reservations:
          memory: 64M
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:9091/api/health"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 10s

功能配置

用户数据库配置 / users_database.yml

自定义登录密码,并使用 argon2id 算法进行 hash 转换。

docker run --rm authelia/authelia:latest authelia crypto hash generate argon2 --password "my_password"

这一步将生成形如 Digest: $argon2id$v=19$m=65536,t=3,p=4$... 的哈希字符串,请将其填入如下文件内容写入命令的模板处。

cat << EOF > /path/to/your/authelia/config/users_database.yml
users:
  yirami:
    password: '$argon2id$v=19$m=65536,t=3,p=4$...'  # 注意此处不能使用 "",其有转义行为
    displayname: "自在即为仙"
    email: "?@yirami.xyz"
    groups:
      - admins
      - dev
EOF

服务配置 / configuration.yml

基础

为避于手动创建配置模板,可以直接启动容器。服务将在配置(config)目录下自动生成默认的配置文件。

docker compose start authelia  # 自动创建默认配置文件

执行下述命令 $4$ 次,分别为 JWT secret(jwt_secret), Storage secret(encryption_key), Session secret(secret), OIDC HMAC secret(hmac_secret) 这四个配置字段生成随机加密密钥。

docker run --rm authelia/authelia:latest authelia crypto rand --length 64

分别搜索 jwt_secret 、 encryption_key 、 secret 、 hmac_secret 并填入之前生成的密钥,注意取消相关注释以开启其所在模块。

重启 Authelia 容器,可能不会顺利启动,需要补充如下的一些配置:

  • access_control 中配置域名
  • session 中配置 cookies
  • notifier 中配置 filesystem 通知
OIDC

为 OpenID Connect(OIDC) 生成用于非对称加密的 RSA 私钥,稍后用于配置 OIDC 。

# docker run --rm authelia/authelia:latest authelia crypto pair rsa generate --bits 4048
docker run --rm authelia/authelia:latest sh -c "authelia crypto pair rsa generate --bits 4048 --directory /tmp && cat /tmp/private.pem"  # 可将密钥直接显示在终端
Forgejo

应用配置

在 Authelia 服务配置中添加全局默认访问控制

access_control:
  default_policy: 'deny'
    - domain: 'authelia.aio.yirami.xyz'
      policy: 'two_factor'

    # 默认保护 `AIO` 下所有子域
    - domain: '*.aio.yirami.xyz'
      policy: 'two_factor'

OIDC 连接 Forgejo

为 Forgejo 创建随机密钥,并对该密钥进行单向哈希加密。在服务器端(Authelia)只存储密钥的哈希,以防服务器端的密钥泄漏。应用携带密钥发起请求时, Authelia 会以相同哈希算法对其变换并与本地进行比对,以完成认证。

# generate OIDC secret for app Forgejo (`client_secret`)
docker run --rm authelia/authelia:latest authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker run --rm authelia/authelia:latest authelia crypto hash generate argon2 --password 'generated_secret'
  1. 修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Forgejo),同时填入前面生成的 RSA 密钥(注意缩进)
  2. 登录 Forgejo 网页,并 网站管理 -> 身份及认证 -> 认证源 -> 添加认证源
    • 认证类型:OAuth2
    • 认证名称:Authelia (严格对应 configuration.yml 配置中 callback 的 URL 中的名称)
    • OAuth2 提供程序:OpenID Connect
    • 客户端 ID (Client ID):forgejo (严格对应 configuration.yml 配置中的 client_id)
    • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
    • OpenID 连接自动发现 URL:https://authelia.aio.yirami.xyz:9443/.well-known/openid-configuration
  3. 用原登录方式登录 Forgejo ,并在依次点击 设置 -> 安全 -> 已绑定的账号 -> 链接账户 完成 Authelia 统一登录的绑定
  4. 在 ./config 目录创建 assets 文件夹,并放入 favicon.ico 文件,同时在 configuration.yml 中开启 asset_path 配置,即可修改网页图标。若 Chrome 浏览器图标不刷新,可以调试模式(F12)打开 Authelia 登录页,并在左上角刷新上右键并选择清空缓存并硬性重新加载

MetaCubeXD(mihomo)

在 Authelia 容器中声明 ForwardAuth 中间件

services:
  authelia:
    labels:
      # 声明 ForwardAuth 中间件
      - "traefik.http.middlewares.authelia-auth.forwardauth.address=http://authelia:9091/api/verify?rd=https://authelia.aio.yirami.xyz:9443/"
      - "traefik.http.middlewares.authelia-auth.forwardauth.trustForwardHeader=true"
      - "traefik.http.middlewares.authelia-auth.forwardauth.authResponseHeaders=Remote-User,Remote-Groups,Remote-Name,Remote-Email"

在 MetaCubeXD 容器中绑定 ForwardAuth 中间件

services:
  mihomo-server:
    labels:
      - "traefik.http.routers.mihomo-ui.middlewares=authelia-auth"

在 Authelia 服务配置中添加访问控制

access_control:
  default_policy: 'deny'
    # 新增访问控制
    - domain: 'mihomo.aio.yirami.xyz'
      policy: 'two_factor'

重启相关容器

docker compose up -d
# docker compose up -d --force-recreate authelia mihomo-dashboard

qBittorrent

在 qBittorrent 容器中绑定 ForwardAuth 中间件

services:
  qbittorrent:
    labels:
      - "traefik.http.routers.qb.middlewares=authelia-auth"

在 qBittorrent 的 Web 端的 设置 -> WebUI -> 对 IP 子网白名单中的客户端跳过身份验证 中补充

# docker network inspect traefik_proxy  # 查询子网
172.22.0.0/16  # 上条命令查询到的当前 docker 子网

在 Authelia 服务配置中添加访问控制

access_control:
  default_policy: 'deny'
    # 新增访问控制
    - domain: 'bt.aio.yirami.xyz'
      policy: 'two_factor'

tinyMediaManager

在 tinyMediaManager 容器中绑定 ForwardAuth 以及 VNC 自动配置中间件

services:
  tinymediamanager:
    labels:
      - "traefik.http.routers.tmm.middlewares=authelia-auth, tmm-autoconnect"  # 绑定中间件
      # 定义 VNC 自动连接中间件,并自动调整分辨率为当前远程端分辨率
      - "traefik.http.middlewares.tmm-autoconnect.redirectregex.regex=^https://tmm.aio.yirami.xyz:9443/$$"
      - "traefik.http.middlewares.tmm-autoconnect.redirectregex.replacement=https://tmm.aio.yirami.xyz:9443/vnc.html?autoconnect=true&resize=remote"

同时,删除上述 docker compose 配置中的密码,避免重复的密码验证。

在 Authelia 服务配置中添加访问控制

access_control:
  default_policy: 'deny'
    # 新增访问控制
    - domain: 'tmm.aio.yirami.xyz'
      policy: 'two_factor'

MDC-NG

在 MDC-NG 容器中绑定 ForwardAuth 中间件

services:
  mdc:
    labels:
      - "traefik.http.routers.mdc.middlewares=authelia-auth"

同时,删除上述 docker compose 配置中的密码,避免重复的密码验证。

在 Authelia 服务配置中添加访问控制

access_control:
  default_policy: 'deny'
    # 新增访问控制
    - domain: 'mdc.aio.yirami.xyz'
      policy: 'two_factor'

在 Navidrome 容器中绑定 ForwardAuth 中间件

services:
  navidrome:
    labels:
      - "traefik.http.routers.navidrome.middlewares=authelia-auth"

在 Authelia 服务配置中添加访问控制(旁路)

access_control:
  default_policy: 'deny'
    # 旁路访问控制
    - domain: 'music.aio.yirami.xyz'
      policy: 'bypass'

旁路该服务是应为其不涉及敏感信息,且只读挂载数据。若开启访问控制还需配合放行第三方客户端需要的路径,较为麻烦。

如果不要求服务形式上都经过 Authelia 亦可不添加上述中间件,则 Authelia 中也无需进行相应配置。

OIDC 连接 Audiobookshelf

与 Forgejo 类似,为 Audiobookshelf 创建随机密钥,并对该密钥进行单向哈希加密。

# generate OIDC secret for app Audiobookshelf (`client_secret`)
docker exec -it authelia authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker exec -it authelia authelia crypto hash generate argon2 --password 'generated_secret'
  1. 修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Audiobookshelf),同时填入前面生成的 RSA 密钥(注意缩进)
  2. 登录 Audiobookshelf 网页,并 设置 -> 身份验证 -> (勾选) OpenID 连接身份验证
    • Issuer URL:https://authelia.aio.yirami.xyz:9443,然后点击 Auto-populate
    • 客户端 ID (Client ID):audiobookshelf (严格对应 configuration.yml 配置中的 client_id)
    • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
    • 切换匹配现有用户选项为 Match by username
    • 勾选自动启动(允许已经 Authelia 登录的用户自动跳转登录)

OIDC 连接 Immich

与 Forgejo 类似,为 Immich 创建随机密钥,并对该密钥进行单向哈希加密。

# generate OIDC secret for app Immich (`client_secret`)
docker exec -it authelia authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker exec -it authelia authelia crypto hash generate argon2 --password 'generated_secret'
  1. 参考官方模板修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Immich),同时填入前面生成的 RSA 密钥(注意缩进)
  2. 登录 Immich 网页,并 系统管理 -> 设置 -> 认证设置 -> OAuth -> (勾选)使用 OAuth 登录
    • Issuer URL:https://authelia.aio.yirami.xyz:9443
    • 客户端 ID (Client ID):immich (严格对应 configuration.yml 配置中的 client_id)
    • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
    • Token Endpoint Auth Method:client_secret_basic
    • 按钮文字:Login with Authelia
    • 自动注册:开启(注意限制 Authelia 端的自动注册)
    • 自动启动:先关闭(待测试完成后开启)
  3. 用原登录方式登录 Immich 网页,并 账户设置 -> 2FA OAuth -> 绑定 OAuth 完成 Authelia 统一登录的绑定
  4. 开启自动启用并禁用密码登录
    • 确保管理员账户已经关联 Authelia
    • 在 系统管理 -> 设置 -> 认证设置 -> OAuth 下配置
      • 自动启动:开启(允许已经 Authelia 登录的用户自动跳转登录)
    • 在 系统管理 -> 设置 -> 认证设置 -> 密码登录 下取消勾选使用邮箱和密码登录
      # 重置该设定
      docker exec -it immich_server immich-admin enable-password-login

OIDC 连接 Kavita

与 Forgejo 类似,为 Kavita 创建随机密钥,并对该密钥进行单向哈希加密。

# generate OIDC secret for app Kavita (`client_secret`)
docker exec -it authelia authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker exec -it authelia authelia crypto hash generate argon2 --password 'generated_secret'
  1. 修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Kavita),同时填入前面生成的 RSA 密钥(注意缩进)
  2. 登录 Kavita 网页,并 设置 -> 服务器 -> OpenID 连接
    • 授权:https://authelia.aio.yirami.xyz:9443
    • 客户端 ID (Client ID):kavita (严格对应 configuration.yml 配置中的 client_id)
    • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
    • 提供商名称:Login with Authelia
    • 取消勾选需要经过验证的电子邮件
    • 勾选自动启动(允许已经 Authelia 登录的用户自动跳转登录)
    • 在确保管理员账户已经关联 Authelia 后可以勾选禁用密码验证

OIDC 连接 Cloudflare Zero Trust

与 Forgejo 类似,为 Cloudflare Zero Trust 创建随机密钥,并对该密钥进行单向哈希加密。

# generate OIDC secret for Cloudflare Zero Trust (`client_secret`)
docker exec -it authelia authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker exec -it authelia authelia crypto hash generate argon2 --password 'generated_secret'
  1. 参考官方文档,修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Cloudflare Zero Trust),同时填入前面生成的 RSA 密钥(注意缩进)
  2. 重要:参考官方文档 中的 works around 解决 Cloudflare 不支持 OpenID Connect 1.0 的问题
  3. 登录 Cloudflare 网页控制台,并依次进入 Zero Trust -> Integrations -> Identity providers -> Add an identity provider
    • 选择 OpenID Connect
    • 名称 (Name):Login with Authelia
    • 应用 ID (App ID):cloudflare-access(严格对应 configuration.yml 配置中的 client_id)
    • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
    • 下述 URL 可在浏览器打开 https://authelia.aio.yirami.xyz:9443/.well-known/openid-configuration 查询
      • Auth URL:https://authelia.aio.yirami.xyz:9443/api/oidc/authorization
      • Token URL:https://authelia.aio.yirami.xyz:9443/api/oidc/token
      • Certificate URL:https://authelia.aio.yirami.xyz:9443/jwks.json
    • 使能 Proof Key for Code Exchange (PKCE)
    • Email claim:email
    • OIDC Claims:添加 preferred_username 和 mail
    • OIDC Scopes:添加 groups
    • 保存后使用专用测试页面测试下鉴权过程

OIDC 链接 Gokapi

与 Forgejo 类似,为 Gokapi 创建随机密钥,并对该密钥进行单向哈希加密。

# generate OIDC secret for app Kavita (`client_secret`)
docker exec -it authelia authelia crypto rand --length 64
# and then generate hash for this `client_secret`
docker exec -it authelia authelia crypto hash generate argon2 --password 'generated_secret'
  1. 修改 configuration.yml 以配置开启 identity_providers 下的 oidc ,并添加客户端(如 Gokapi),同时填入前面生成的 RSA 密钥(注意缩进)

      -
       client_id: 'gokapi'
       client_name: 'Gokapi'
       client_secret: '$argon2id$???'
       public: false
       consent_mode: 'pre-configured'
       redirect_uris:
         - 'https://share.aio.yirami.xyz:9443/oauth-callback'
       scopes:
         - 'openid'
         - 'email'
         - 'profile'
         - 'groups'
       token_endpoint_auth_method: 'client_secret_basic'
       userinfo_signed_response_alg: 'none'
  2. 启动 Gokapi 应用容器,打开设置向导页

    • 在 Authentication 页选择 OAuth2 OpenID Connect
    • 在 Credentials 页填写
      • Provider URL: https://authelia.aio.yirami.xyz:9443
      • 客户端 ID (Client ID):gokapi (严格对应 configuration.yml 配置中的 client_id)
      • 客户端密钥 (Client Secret):…(严格对应 configuration.yml 配置中使用的 client_secret)
      • 管理员邮箱:i@yirami.xyz
      • 身份重验间隔(Recheck identity every):12h
      • 勾选Only allow already existing users to log in