ruby - rails限制多个IP访问的方法?
黄舟
黄舟 2017-04-21 10:58:01
0
1
643

对某一个action的IP的限制方式是,在routes.rb中:

  constraints(:ip => Settings.ip.allow) do
     post '/somecontroller/callback', to: 'somecontroller#callback' , as: 'somecontroller_callback'
  end

在settings.yml中:

  ip:
    allow: '127.0.0.1'

这是可以的.

但是想对多个IP允许访问,在settings.yml中如下:

  ip:
    allow: '/127.0.0.1|127.0.0.2/'

却不行。哪里不对呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
黄舟

Because Settings.ip.allow always returns String, but in fact, in the second case, what you need is a RegExp, so it is invalid. You can try changing it like this:

config/routes.rb:

constraints(:ip => %r{#{Settings.ip.allow}}) do
 post '/somecontroller/callback', to: 'somecontroller#callback' , as: 'somecontroller_callback'
end

config/settings.rb

ip:
  allow: '127.0.0.1|127.0.0.2'
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template