ApacheのBASIC認証といえば、ダイアログが出てきてIDとパワードを入力するものだと思っていました。
Apacheの2.3あたりからmod_auth_formというのがあって、BASIC認証なんだけど入力フォームをHTMLで作れる機能がある、ってことを知りました。
ならばやってみるしかありません。
ちょうど認証を入れているものがあったので試してみました。
最初に見つけたサイトのは.htaccessを使うのだったので、2か所に.htaccessを設定するとかでなんとなく理解できず500エラーに。。
https://www.pressmantech.com/tech/1533
を参考にhttpd.confを設定し、passwdの設定をsqlite3にした以下のようなものになりました。
・・・/auth/ 以下にあるアプリ(PHPで作っています)のURLを直接アクセスすると、302応答で/login.htmlへ遷移して、その後/auth/index.htmlにアクセスするので、「認証後に元のアクセス先URLを表示する」というよくあるアプリ動作にならないのが要調査ポイントです。
理想:/auth/hoge.php?aaa=bbb → 認証画面 → /auth/hoge.php?aaa=bbb
理想:/auth/hoge.php?aaa=ccc → 認証画面 → /auth/hoge.php?aaa=ccc
現実:/auth/hoge.php?aaa=bbb → 認証画面 → /auth/index.html
------------------------------------------------------------
DBDriver sqlite3
DBDParams "/var/www/auth/auth.db"
Alias /auth /var/www/auth
<Location /auth>
require valid-user
AuthFormProvider dbd
AuthDBDUserPWQuery "SELECT password from users where user = %s "
AuthType form
AuthName realm
AuthFormLoginRequiredLocation http://%{SERVER_NAME}/login.html
Session On
SessionCookieName auth_form path=/
SessionCryptoPassphrase hogefugahogefugahogefugahogefuga
</Location>
<Location /dologin.html>
SetHandler form-login-handler
AuthFormLoginRequiredLocation http://%{SERVER_NAME}/login.html
AuthFormLoginSuccessLocation http://%{SERVER_NAME}/auth/index.html
AuthFormProvider dbd
AuthDBDUserPWQuery "SELECT password from users where user = %s "
AuthType form
AuthName realm
Session On
SessionCookieName auth_form path=/
SessionCryptoPassphrase hogefugahogefugahogefugahogefuga
</Location>
<Location /dologout.html>
SetHandler form-logout-handler
AuthFormLogoutLocation http://%{SERVER_NAME}/logout.html
Session On
SessionMaxAge 1
SessionCookieName auth_form path=/
SessionCryptoPassphrase hogefugahogefugahogefugahogefuga
</Location>
login.html-------------------
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>ログイン</title>
</head>
<body class="login">
<form method="POST" action="dologin.html" class="loginform">
<p>ユーザー名:<input type="text" name="httpd_username" value=""></p>
<p>パスワード:<input type="password" name="httpd_password" value=""></p>
<p><button type="submit" name="login" value="Login">ログイン</button></p>
</form>
</body>
</html>
logout.html-------------------
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>ログアウト</title>
</head>
<body>
<p>ログアウトしました。</p>
<p>[<a href="login.html">ログイン画面に戻る</a>]</p>
</body>
</html>
コメント
コメントを投稿