{"id":591,"date":"2008-10-06T23:40:04","date_gmt":"2008-10-06T14:40:04","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=591"},"modified":"2021-07-15T14:43:45","modified_gmt":"2021-07-15T05:43:45","slug":"basic-auth-on-rails2","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/basic-auth-on-rails2\/","title":{"rendered":"Rails \u3067 Basic\u8a8d\u8a3c\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b Basic \u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u306e\u4f7f\u3044\u65b9\u3002\u30e6\u30fc\u30b6\u540d <code>chihaya<\/code>, \u30d1\u30b9\u30ef\u30fc\u30c9 <code>password72<\/code> \u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u884c\u3048\u308b\u3002<\/p>\n<h2>Rails5.1\u4ee5\u964d<\/h2>\n<pre><code class=\"language-ruby\">class ApplicationController &lt; ActionController::Base\n  http_basic_authenticate_with name name: &#039;chihaya&#039;, password: &#039;password72&#039; unless Rails.env.production?\nend<\/code><\/pre>\n<p>\u7279\u5b9a\u306e\u51e6\u7406\u3067\u30b9\u30ad\u30c3\u30d7\u3057\u305f\u3044\u5834\u5408\u3001 <code>except<\/code> \u3092\u4f7f\u3046\u304b\u3001<code>before_action<\/code> \u3067\u51e6\u7406\u3059\u308b<\/p>\n<pre><code class=\"language-ruby\"># except \u3092\u4f7f\u3046\u5834\u5408\nclass NanohaController &lt; ApplicationController\n  http_basic_authenticate_with name name: &#039;chihaya&#039;, password: &#039;password72&#039;, except: :show unless Rails.env.production?\n\n  def show\n  end\nend<\/code><\/pre>\n<pre><code class=\"language-ruby\"># before_action \u3092\u4f7f\u3046\u5834\u5408\nclass ApplicationController &lt; ActionController::Base\n  before_action :basic_authentication\n\n  private\n\n  def basic_authentication\n    return if Rails.env.production?\n\n    http_basic_authenticate_with name name: &#039;chihaya&#039;, password: &#039;password72&#039;\n  end\nend\n\nclass NanohaController &lt; ApplicationController\n  skip_before_action :basic_authentication, only: [:show]\n\n  def show\n  end\nend\n<\/code><\/pre>\n<h2>Rails5.0\u4ee5\u524d<\/h2>\n<pre><code class=\"language-ruby\">class ApplicationController &lt; ActionController::Base\n  before_filter :basic_authentication\n\n  # \u30c6\u30b9\u30c8\u3060\u3051\u4f7f\u3046Basic\u8a8d\u8a3c\n  protected\n  def basic_authentication\n    return if Rails.env.production?\n\n    authenticate_or_request_with_http_basic do |user, pass|\n      [user, pass] == [&#039;chihaya&#039;, &#039;password72&#039;]\n    end\n  end\nend<\/code><\/pre>\n<p>\u7279\u5b9a\u306eAction\u306fBasic\u8a8d\u8a3c\u304b\u3051\u305f\u304f\u306a\u3044\u306a\u3089\u3070\u3001<code>skip_before_filter<\/code> \u3092\u4f7f\u3046\u3002<\/p>\n<pre><code class=\"language-ruby\">class NanohaController &lt; ApplicationController\n  skip_before_filter :basic_authentication, only: [:show]\n\n  def show\n  end\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b Basic \u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u306e\u4f7f\u3044\u65b9\u3002\u30e6\u30fc\u30b6\u540d chihaya, \u30d1\u30b9\u30ef\u30fc\u30c9 password72 \u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u884c\u3048\u308b\u3002 Rails5.1\u4ee5\u964d class ApplicationCo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[10],"class_list":["post-591","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":5,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"predecessor-version":[{"id":4340,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/591\/revisions\/4340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}