Doing a Http basic authentication in rails

后端 未结 8 1111
独厮守ぢ
独厮守ぢ 2020-12-14 16:09

Hi I\'m from Grails background and new to Rails. I wish to do http basic authentication in rails.

I have a code in grails which does basic authentication like this:<

8条回答
  •  情深已故
    2020-12-14 16:32

    Above answers are correct, but it is better to not put the user and password in the source code.

    Better have the password in environment variables for production (in the code is OK for development)

    class YourController..
      http_basic_authenticate_with name: ENV["HTTP_BASIC_AUTH_USER"], password: ENV["HTTP_BASIC_AUTH_PASSWORD"], if: -> { ENV['RAILS_ENV'] == 'production' }
      http_basic_authenticate_with name: "user", password: "pass", if: -> { ENV['RAILS_ENV'] != 'production' }
    

提交回复
热议问题