ri > WEBrick::HTTPAuth

🔖 WEBrick::HTTPAuth

HTTPAuth provides both basic and digest authentication.

🚀 Quick Reference

Use Case Command Description
Create a user database WEBrick::HTTPAuth::Htpasswd.new 'password_file' Create a new Htpasswd database for digest auth
Add user to database htpasswd.set_passwd realm, username, password Set password for a user in a realm
Create digest authenticator WEBrick::HTTPAuth::DigestAuth.new config Create an authenticator with user database
Authenticate a request authenticator.authenticate req, res Authenticate a request in a servlet

To enable authentication for requests in WEBrick you will need a user database and an authenticator. To start, here's an Htpasswd database for use with a DigestAuth authenticator:

  config = { :Realm => 'DigestAuth example realm' }

  htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'my_password_file'
  htpasswd.auth_type = WEBrick::HTTPAuth::DigestAuth
  htpasswd.set_passwd config[:Realm], 'username', 'password'
  htpasswd.flush

The :Realm is used to provide different access to different groups across several resources on a server. Typically you'll need only one realm for a server.

This database can be used to create an authenticator:

  config[:UserDB] = htpasswd

  digest_auth = WEBrick::HTTPAuth::DigestAuth.new config

To authenticate a request call #authenticate with a request and response object in a servlet:

  def do_GET req, res
    @authenticator.authenticate req, res
  end

For digest authentication the authenticator must not be created every request, it must be passed in as an option via WEBrick::HTTPServer#mount.

📋 Instance methods

WEBrick::HTTPAuth
🔖 WEBrick::HTTPAuth 🚀 Quick Reference
📋 Instance methods

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-29 14:37 @216.73.217.111
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format