查看完整版本: [轉貼]如何讓一個 login 的網頁只能經由 https 來登入

chun 2009-9-23 22:53

[轉貼]如何讓一個 login 的網頁只能經由 https 來登入

<DIV class=title-0>
<H2 class=title-1><A href="http://xml.nchu.edu.tw/blogs/?page_id=30">http://xml.nchu.edu.tw/blogs/?page_id=30</A></H2>
<H2 class=title-1>&nbsp;</H2>
<H2 class=title-1>如何讓一個 login 的網頁只能經由 https 來登入</H2></DIV>
<DIV class=clear>
<P>問題蠻複雜的,好像需要瞭解的東西也不少,所以我們慢慢來建構這個網頁。 </P>
<P><STRONG>動機:</STRONG> 在開發網頁資訊系統的時候,我們經常需要讓使用者登入,而且我們希望使用者登入的帳號以及密碼不會在 Internet 傳遞的時候被竊聽或者竊取,這時我們就需要使用 https 來對資料進行加密。可是往往設計一個登入的網頁(例如:login.php),我們不但可以 http://xml.nchu.edu.tw/test/login.php,也可以 https://xml.nchu.edu.tw/test/login.php 來存取。為了確保使用者只能使用 https 來登入,我們需要把所有 http://xml.nchu.edu.tw/test/login.php 的連線都轉成 https://xml.nchu.edu.tw/test/login.php。 </P>
<OL>
<LI>我們以 Apache2 為範例。你的 Apache2 需要支援 mod_ssl 以及 mod_rewrite,你可以利用 <CODE>/usr/local/apache2/bin/httpd -l</CODE> 來列印出你的 Apache2 是否支援 ssl 以及 rewrite 的 modules。如果沒有,你必須先完成安裝手續。</LI>
<LI>備份並修改你的 httpd.conf:請新增下列設定到你的 httpd.conf 內。 <PRE>01 #RewriteLog      /www/logs/rewrite.log
02 #RewriteLogLevel 1
03
04 &lt;Directory "/www/htdocs/test"&gt;
05    RewriteEngine   on
06    Options +FollowSymLinks
07    Order allow,deny
08    Allow from all
09    RewriteCond %{SERVER_PORT} !^443$
10    RewriteRule ^login\.php$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
11 &lt;/Directory&gt;
</PRE>
<UL>
<LI>第 01 和 02 行的目的是為了使用 mod_rewrite 時的所有 log 的紀錄。如果你沒辦法將 http 的登入網頁自動變成 https,請你將這兩行最前面的 # 拿掉。注意,如果一切都 OK 的話,儘量不要使用 log,因為它會使得 Apache2 慢很多。</LI>
<LI>第 04 和 11 行的目的是為了限定 /www/htdocs/test 這個目錄內的設定才有用。</LI>
<LI>啟動 mod_rewrite 需要第 05、06、07、08 行。</LI>
<LI>第 10 行用來指定當使用者要存取 login.php 這個網頁的時候,Apache2 會將這個連線轉成 https。</LI>
<LI>第 09 行用來指定當使用者經由非 443 這個 port 來存取 login.php 這個網頁的時候,狀況為真。</LI>
<LI>如果我們把第 10 行的 <FONT color=red>^login\.php$</FONT> 改成 <FONT color=red>^.*$</FONT> 則代表所有進入 test 這個目錄內的連結都會以 https 的方式完成。</LI></UL></LI>
<LI>重新啟動 httpd。</LI></OL>
<P>今天(12/29/2008)發現另一個方式,這個方式僅適用於 Wordpress 的環境。</P>
<OL>
<LI>Wordpress 2.5 版以後,我們可以在 wp-config.php 中加入<CODE>define('FORCE_SSL_ADMIN', true);</CODE>的方式。</LI>
<LI>之後,使用 login.php,它會自動進入 https 的連線型態;而且依照我使用的情形來看,這個方式會比之前利用 Apache/mod_ssl/mod_rewrite 的方式來得穩定。</LI></OL>
<P><STRONG>參考資料</STRONG></P>
<UL>
<LI><A onclick="pageTracker._trackPageview('/outgoing/www.cs.utk.edu/_sammons/docs/redirect.php?referer=http://phorum.study-area.org/index.php?topic=54533.0');" href="http://www.cs.utk.edu/~sammons/docs/redirect.php"><FONT color=#0000ff>Apache URL Redirection</FONT></A></LI>
<LI><A onclick="pageTracker._trackPageview('/outgoing/www.whoopis.com/howtos/apache-rewrite.html?referer=http://phorum.study-area.org/index.php?topic=54533.0');" href="http://www.whoopis.com/howtos/apache-rewrite.html"><FONT color=#0000ff>Using Apache’s RewriteEngine to redirect requests to other URLS and to https://</FONT></A></LI>
<LI><A onclick="pageTracker._trackPageview('/outgoing/httpd.apache.org/docs/1.3/mod/mod_rewrite.html?referer=http://phorum.study-area.org/index.php?topic=54533.0');" href="http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html"><FONT color=#0000ff>Module mod_rewrite: URL Rewriting Engine</FONT></A></LI></UL></DIV>

chun 2009-9-23 22:54

[轉貼]https與閒置時間設定

<P><A href="http://phorum.study-area.org/index.php?topic=54533.0">http://phorum.study-area.org/index.php?topic=54533.0</A></P>
<P>&nbsp;</P><TR><TD height="100%" width="85%" valign="top">
<DIV class=post>第1個問題,經詢問<A href="http://www.google.com.tw/search?q=login+by+https&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:zh-TW<img src=" alt=":o" border="0" smilieid="6" shocked.gif? default smilies images .>fficial&amp;client=firefox-a" target=_blank&gt;<FONT color=#0000ff>google</FONT></A>後<BR><A href="http://xml.nchu.edu.tw/blogs/?page_id=30" target=_blank><FONT color=#0000ff>http://xml.nchu.edu.tw/blogs/?page_id=30</FONT></A><BR>以<A href="http://www.apachefriends.org/zh_tw/xampp-linux.html" target=_blank><FONT color=#0000ff>xampp</FONT></A>系統為例,如果登入頁面為index.php,用https;登入成功為login_ok.php,用http,則<BR><BR>先在/opt/lampp/etc/extra/http-ssl.conf開啟欲開啟的目錄之ssl權限<BR><BR>DocumentRoot "/opt/lampp/htdocs"<BR>ServerName abc.com.tw:443<BR>##ServerAdmin <A href="mailto:you@example.com"><FONT color=#0000ff>you@example.com</FONT></A><BR>ErrorLog /opt/lampp/logs/error_log<BR>TransferLog /opt/lampp/logs/access_log<BR><BR>接下來在/opt/lampp/etc/http.conf下新增以下<BR><BR>&lt;Directory "/opt/lampp/htdocs/login"&gt;<BR>&nbsp; &nbsp; RewriteEngine&nbsp; &nbsp;on<BR>&nbsp; &nbsp; Options +FollowSymLinks<BR>&nbsp; &nbsp; Order allow,deny<BR>&nbsp; &nbsp; Allow from all<BR>&nbsp; &nbsp; RewriteCond %{SERVER_PORT} !^443$<BR>&nbsp; &nbsp; RewriteRule ^index.php$ <A href="https://%%7BSERVER_NAME%7D%%7BREQUEST_URI%7D" target=_blank><FONT color=#0000ff>https://%{SERVER_NAME}%{REQUEST_URI}</FONT></A> [L,R]<BR>&nbsp; &nbsp; RewriteCond %{SERVER_PORT} !^80$<BR>&nbsp; &nbsp; RewriteRule ^login_ok.php$ <A href="http://%%7BSERVER_NAME%7D%%7BREQUEST_URI%7D" target=_blank><FONT color=#0000ff>http://%{SERVER_NAME}%{REQUEST_URI}</FONT></A> [L,R]<BR>&lt;/Directory&gt;<BR></DIV>
<P></TD></TR><TR><TD class=smalltext width="85%" valign="bottom">
<TABLE style="TABLE-LAYOUT: fixed" border=0 width="100%">
<TBODY>
<TR>
<TD class=smalltext width="100%" colSpan=2></TD></TR>
<TR>
<TD id=modified_278575 class=smalltext vAlign=bottom>« <I>最後編輯時間: 2008-11-22 10:24 由 ebook</I> » </TD></TR></TBODY></TABLE></TD></TR></P>
頁: [1]
查看完整版本: [轉貼]如何讓一個 login 的網頁只能經由 https 來登入