Basic認証

@IT:Apacheでユーザー認証を行うには(Basic認証編)を見ながら遊んでみる。

認証エリアの作成

# mkdir /var/www/member

secretユーザ作成

# htpasswd -c /etc/apache2/sites-available/.htpasswd secret
New password:
Re-type new password:
Adding password for user secret

Apacheの設定ファイル編集

# vi /etc/apache2/sites-available/default(↓を追記)
<Directory "/var/www/html/member">
  AuthType Basic
  AuthName "Secret Zone"
  AuthUserFile /etc/apache2/sites-available/.htpasswd
  Require user secret
</Directory>

Apacheを再起動

# /etc/init.d/apache2 restart
 * Restarting web server apache2
[ OK ]

サーバ側のコマンドラインから、http://localhost/member/にアクセス

# telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /member HTTP/1.0
Host: localhost:80


HTTP/1.1 401 Authorization Required
Date: Sun, 29 Mar 2009 17:13:42 GMT
Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.5 with Suhosin-Patch
WWW-Authenticate: Basic realm="Secret Zone"
Content-Length: 515
Connection: close
Content-Type: text/html; charset=iso-8859-1


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.5 with Suhosin-Patch Server at localhost Port 80</address>
</body></html>
Connection closed by foreign host.

クライアント側のブラウザから、http://ubuntu-vm/member/にアクセス