---
title: "使用Caddy Security 为你的网站增加自动置登录鉴权"
description: "利用 Caddy 和 Caddy Security 来搭建并保护你的网站。Caddy 不仅可以作为强大的网站前置代理，还能自动申请和更新免费的 HTTPS 证书；而 Caddy Security 则可以帮你轻松实现登录鉴权。简单的配置，无需多余的开发，立刻让你的网站安全上线！"
url: "https://hicc.pro/p/blog/caddy-and-caddy-security-for-your-web-auth"
author: "tyler wang"
published: "2023-06-04T08:31:59.000Z"
updated: "2026-06-28T09:31:10.000Z"
category: "blog"
tags:
  - "Caddy"
lang: "zh-CN"
site: "Hipo日志"
---

# 使用Caddy Security 为你的网站增加自动置登录鉴权

很多情况下网站登录后访问是必不可少的，如果你的需求简单或者懒得开发。使用Caddy Security可以很简单的为你解决问题。

### 使用Caddy作为你网站的前置代理

[Caddy](https://caddyserver.com/) 是一个Go语言写的开源Web Server，功能挺强大。支持反向代理，以及自动化的HTTPS。很多人也用来替代Nginx，因为它可以自动申请续费[letsencrypt](https://letsencrypt.org/zh-cn/)之类的免费证书，而且配置也挺简单。

```json
yourdamain.com {
	encode zstd gzip
	reverse_proxy localhost:8080
}

```

### 1使用Caddy Security增加自动置登录鉴权

Caddy同时也支持多内置和第三方的模块。其中的[Caddy Security](https://github.com/greenpau/caddy-security) 模块就是今天的主角，它为Caddy带来了很丰富的登录方式。

<img height="131" width="721" alt="Caddy Security 登录功能" src="https://image.hicc.pro/pro/20230604175453Fi1214zN2JfCmP445fMD25K61NQ-jFu5Eqo5ZSjJB0azDRdfA.png" />

下面就是点流水账式的折腾记录：

#### 安装Caddy Security

Caddy Security属于第三方模块，需要单独安装。Caddy 官方似乎推荐使用[xcaddy](https://github.com/caddyserver/xcaddy)来安装，但是我没有成功，而是通过[官方的集成下载方式](https://caddyserver.com/download)搞定。

![Caddy官方的集成下载方式](https://image.hicc.pro/pro/20230604180357ForDPS6KhODMC9FZry3l-kerK4iM3EE_klCPjcc7wZwyvEMaa.png)

配置到path，或者 `/path/to/caddy start`也一样工作

#### 配置Caddy Security

这里给出账号密码登录的例子。可以按照官方[local鉴权](https://authp.github.io/docs/authenticate/local/local)的方式, 能配差不多。

这里是我的配置：

```json
{
	order authenticate before respond
	order authorize before basicauth
	
	security {
		local identity store localdb {
			realm local
			path /path/to/users.json
		}

		authentication portal myportal {
			crypto key sign-verify {env.JWT_SHARED_KEY}
			enable identity store localdb
			cookie domain youdomain.com
			ui {

				custom html header path /path/to/custom-head.html

				links {
					"Go App" https://app.youdomain.com icon "las la-binoculars"
				}
				custom js path /root/caddy/custom.js
			}

			transform user {
    				suffix match email @youdomain.com
    				add role authp/user
  			}
		}
		authorization policy admins_policy {
			set auth url http://auth.youdomain.com
			allow roles authp/admin authp/user
			crypto key verify {env.JWT_SHARED_KEY}
		}
	}
}

auth.youdomain.com {
	 authenticate with myportal
}

app.youdomain.com {
	authorize with admins_policy
	encode zstd gzip
	reverse_proxy localhost:8080
}
```

* 上面`app.youdomain.com`是业务网站，`auth.youdomain.com`是独立的鉴权页面。如果你想一个域名，可以尝试利用caddy`route`的功能。
* 想要添加用户，可以按照[Caddy Security local 密码指示](https://authp.github.io/docs/authenticate/local/password-management) 指示，添加新用户到`/path/to/users.json`中，注意用户权限也可以设为`authp/user`
* 支持自定义UI
  * 可以[配置自定义模版，添加head，js等](https://authp.github.io/docs/authenticate/ui-features)。
  * 上述links是可以指定登录成功后的跳转链接。

---

原文 / Source: <https://hicc.pro/p/blog/caddy-and-caddy-security-for-your-web-auth>
