Hexo 文章密码保护教程 / Hexo Password Protection Tutorial

Hexo 文章密码保护教程 / Hexo Password Protection Tutorial

本教程将指导你如何为 Hexo 博客文章添加密码保护功能。通过这个功能,你可以将某些私密或特殊的文章设置为需要密码才能访问。
This tutorial shows you how to add password protection to your Hexo blog posts. With this feature, you can restrict access to private or special articles.

步骤 1:安装插件 / Step 1: Install the Plugin

  • 中文:在 Hexo 博客根目录运行以下命令安装 hexo-blog-encrypt 插件。
  • EN: Run the command below in your Hexo blog root directory to install the hexo-blog-encrypt plugin.
1
npm install hexo-blog-encrypt --save

步骤 2:配置站点 _config.yml / Step 2: Configure Site _config.yml

  • 中文:在站点级 _config.yml 文件末尾添加插件配置,设置全局提示文本与模板。
  • EN: Append the plugin configuration to your site-level _config.yml, customizing global prompts and template.
1
2
3
4
5
6
encrypt:
enable: true
abstract: "这是一篇受保护的文章,请输入密码访问。"
message: "请输入密码"
tags:
- {name: "tagName", password: "password"}

配置说明 / Configuration Details:

  • enable: 是否启用加密功能 / Enable encryption feature
  • abstract: 未解锁时显示的摘要文本 / Text shown before unlocking
  • message: 密码输入框的提示信息 / Password input prompt
  • tags: 可以为特定标签的所有文章设置统一密码 / Set unified password for all posts with specific tags

步骤 3:为文章添加密码 / Step 3: Add Password to Posts

  • 中文:在需要保护的文章 Front-matter 中加入 passwordabstractmessage 字段。
  • EN: Include password, abstract, and message fields in the front-matter of posts you want to protect.
1
2
3
4
5
6
7
8
9
---
title: 我的秘密文章
date: 2025-11-20
password: 123456
abstract: 这是一段摘要,所有人可见。
message: 请输入访问密码
---

这里是受保护的内容,只有输入正确密码才能看到。

Front-matter 字段说明 / Field Descriptions:

  • password: 访问文章所需的密码(必填)/ Password required to access the post (required)
  • abstract: 可选,覆盖全局设置的摘要 / Optional, overrides global abstract
  • message: 可选,覆盖全局设置的提示信息 / Optional, overrides global message

步骤 4:构建与验证 / Step 4: Build and Verify

  • 中文:运行以下命令清理缓存、生成静态文件并启动本地服务器,然后访问文章测试密码保护功能。
  • EN: Run these commands to clean cache, generate files, and start the local server, then test the password protection.
1
2
3
hexo clean
hexo generate
hexo server

在浏览器中访问受保护的文章,输入设置的密码即可查看完整内容。
Visit the protected post in your browser and enter the password to view the full content.

高级技巧 / Advanced Tips

1. 为整个标签设置密码 / Set Password for Entire Tag

如果你想为某个标签下的所有文章设置相同的密码,可以在站点配置中使用 tags 选项:
To set the same password for all posts with a specific tag, use the tags option in site config:

1
2
3
4
5
encrypt:
enable: true
tags:
- {name: "secret", password: "mySecret123"}
- {name: "private", password: "private456"}

2. 自定义样式 / Customize Styles

如果想调整密码输入框的样式,可以修改主题的 CSS 文件:
To customize the password input box style, modify your theme’s CSS file:

  • 文件位置 / File location: source/css/post.css 或主题的相关 CSS 文件 / or theme’s related CSS file
  • 可以调整颜色、边框、字体等样式 / You can adjust colors, borders, fonts, etc.

3. 安全建议 / Security Recommendations

  • ✅ 使用强密码,包含字母、数字和特殊字符 / Use strong passwords with letters, numbers, and special characters
  • ✅ 不同文章使用不同密码 / Use different passwords for different posts
  • ⚠️ 这种加密是前端加密,不适合保护高度敏感信息 / This is front-end encryption, not suitable for highly sensitive information
  • ⚠️ 密码会以加密形式存储在生成的 HTML 中 / Password is stored in encrypted form in generated HTML

故障排查 / Troubleshooting

问题 1:输入密码后无法解锁 / Issue 1: Cannot Unlock After Entering Password

解决方法 / Solution:

  1. 清理浏览器缓存 / Clear browser cache
  2. 运行 hexo clean 清理 Hexo 缓存 / Run hexo clean to clear Hexo cache
  3. 检查站点 _config.yml 中插件配置是否正确 / Verify plugin config in site _config.yml
  4. 确认密码拼写正确,注意大小写 / Confirm password spelling, case-sensitive

问题 2:加密功能不生效 / Issue 2: Encryption Not Working

解决方法 / Solution:

  1. 确认已正确安装 hexo-blog-encrypt 插件 / Confirm hexo-blog-encrypt is installed
  2. 检查 Front-matter 格式是否正确 / Check Front-matter format
  3. 重新运行 hexo clean && hexo generate / Re-run hexo clean && hexo generate

问题 3:部署到 GitHub Pages 后失效 / Issue 3: Not Working After Deploying to GitHub Pages

解决方法 / Solution:

  1. 确保部署前运行了 hexo generate / Ensure hexo generate was run before deployment
  2. 检查 public 目录中的文件是否包含加密内容 / Check if files in public directory contain encrypted content
  3. 清除 CDN 缓存(如果使用)/ Clear CDN cache (if using)

示例演示 / Example Demo

想要查看实际效果?请访问这篇加密文章示例(密码:123456)。
Want to see it in action? Check out this encrypted post example (password: 123456).

总结 / Summary

Hexo 文章密码保护功能让你可以轻松地为特定文章添加访问限制。虽然这不是军事级别的加密,但对于保护个人博客中的私密内容来说已经足够了。
Hexo password protection allows you to easily restrict access to specific posts. While not military-grade encryption, it’s sufficient for protecting private content on personal blogs.

记住:始终使用强密码,并定期更新!
Remember: Always use strong passwords and update them regularly!

COMMENTS