由于我正在解决多个问题,我想知道是否有人知道如何阻止基于浏览器访问 Rails 应用程序?我想阻止 Internet Explorer(所有版本),有什么想法吗?
您可以使用rack-noie作为Rails中间件:
宝石档案:
gem "rack-noie", :require => "noie"
然后在你的 config/application.rb 中:
config.middleware.use Rack::NoIE, {:redirect => "/why-i-dont-support-ie.html",
:minimum => 29}
当然,这会阻止 IE 直到版本 29。如果您只想让 9 或 10 进入,请进行编辑。您将人们重定向到哪里也取决于您。
从
Rails 7.2
开始,有一种内置的方法可以阻止浏览器。
例如:
class ApplicationController < ActionController::Base
# Allow only browsers natively supporting webp images, web push, badges, import maps, CSS nesting + :has
allow_browser versions: :modern
end
class ApplicationController < ActionController::Base
# All versions of Chrome and Opera will be allowed, but no versions of "internet explorer" (ie). Safari needs to be 16.4+ and Firefox 121+.
allow_browser versions: { safari: 16.4, firefox: 121, ie: false }
end
class MessagesController < ApplicationController
# In addition to the browsers blocked by ApplicationController, also block Opera below 104 and Chrome below 119 for the show action.
allow_browser versions: { opera: 104, chrome: 119 }, only: :show
end