サイト管理人Blog

cloudfreeで「さぁ始めましょう♪」

cloudfreeでBASIC認証

2023-11-13 記載
概要 : cloudfreeサーバーでフォルダにBASIC認証を設定する
Keyword : cloudfree, BASIC認証, apache, htaccess,


cloudfreeサーバのフォルダにBASIC認証をかけてみました。
対象のフォルダを
/public_html/downloadfiles/needauth/
とします。

認証ユーザーを記載する.htpasswdファイルの置き場ですが、cloudfreeの場合、public_htmlと同じ階層にhtpasswdという名のフォルダを作ってくれてます。

.htpasswdはココにおいてくれ、という感じですね。どこに置いてもよいのですが、このファイルの所在パスはサーバー内のルートフォルダからのパスになる点が注意必要です。

手順
[1] .htaccess にBASIC認証を記載して当該フォルダに配置
[2] .htpasswdファイルを作成
[3] .htpasswdファイルを htpasswdフォルダに配置

まずは
/public_html/downloadfiles/needauth/.htaccess
のファイル内容と配置。(3行のwebは各自のサーバーIDに)

AuthType Basic
AuthName "Please enter your ID and password"
AuthUserFile /home/web/web.cloudfree.jp/htpasswd/downloadfiles_needauth.htpasswd
require valid-user

.htpasswd のファイル名をここでは
downloadfiles_needauth.htpasswd
としています。
どこのフォルダのユーザー:パスワードなのかがわかるように。
.htpasswd という名前に限られる場合もあるかもしれませんが、cloudfreeで試したところこれで認証できました。

.htpasswd の中身は
ユーザーID:パスワード
という1行が1ユーザーで、その羅列なのですが、パスワードを平文で書くとエラーになりました。localhostではいけましたがcloudfreeでは暗号化必須のようです。
暗号化にはApache24\binフォルダ内にあるhtpasswd.exeを使います。
ウェブで手軽に暗号化できるサイトもあるようです。しかし変換のビッグデータが蓄えられていてどこかで使われるかも、という可能性もなくはないです。

C:\pgm\Apache24\bin>htpasswd.exe -c C:\home\web\web.cloudfree.jp\htpasswd\downloadfiles.needauth.htpasswd user0001
New password: ********
Re-type new password: ********
Adding password for user user0001

C:\pgm\Apache24\bin>htpasswd.exe C:\home\web\web.cloudfree.jp\htpasswd\downloadfiles.needauth.htpasswd user0002
New password: ********
Re-type new password: ********
Adding password for user user0002

最初のユーザーと追加のユーザーとで、-c が違います。
これでdownloadfiles.needauth.htpasswdの中身は以下。

user0001:$apr1$ONMezh89$WB5A.ZnX0K/lNmGyMPfuN1
user0002:$apr1$BYbnWYYn$YbtDjcqkC5w3s5Bf3b542.

htpasswd.exeのhelpは以下。

C:\pgm\Apache24\bin>htpasswd.exe -h
htpasswd.exe: illegal option -- h
Usage:
        htpasswd [-cimBdpsDv] [-C cost] passwordfile username
        htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password

        htpasswd -n[imBdps] [-C cost] username
        htpasswd -nb[mBdps] [-C cost] username password
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -b  Use the password from the command line rather than prompting for it.
 -i  Read password from stdin without verification (for script usage).
 -m  Force MD5 encryption of the password (default).
 -B  Force bcrypt encryption of the password (very secure).
 -C  Set the computing time used for the bcrypt algorithm
     (higher is more secure but slower, default: 5, valid: 4 to 17).
 -d  Force CRYPT encryption of the password (8 chars max, insecure).
 -s  Force SHA encryption of the password (insecure).
 -p  Do not encrypt the password (plaintext, insecure).
 -D  Delete the specified user.
 -v  Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.

.htaccess
downloadfiles.needauth.htpasswd
をそれぞれ配置して完了。
ちなみにApach & Windows の環境だと、.htaccess記載のパス
AuthUserFile /home/web/web.cloudfree.jp/htpasswd/downloadfiles_needauth.htpasswd
は先頭にC:をつけて、C:\/home/~を探します。
よってローカルのファイルを C:/home/web/ 配下に持てば、BASIC認証関係の設定はサーバーとローカル、ファイル編集なしで共通して作動します。

投稿へのコメントは コチラ(掲示板) へお願いします。

サイト管理人Blog