赫謙小便籤

HTTP Basic Authentication in Rails 3

有的時候想要偷懶採用比較簡單的認證方式時其實用HTTP Basic Authentication就可以了,但是在Rails中要怎樣實現呢?

答案是這個:authenticate_or_request_with_http_basic

把這個加在application_controller.rb內用before_filter去弄就好,像是

然後,另外一個比較簡單的方式是:http_basic_authenticate_with,用了這個我們就不需要再自己寫before_filter弄了,像是:

其中,realm是會提示給使用者看的訊息。

Comments