当在我的某个Rails控制器中调用某个方法时,我想检查用户的IP地址是否在可信列表中,如果是,则重写request.forgery_whitelisted?方法设置为true,这样就不会强制实施CSRF保护。
我读过的一篇博客文章似乎表明,在控制器操作中声明以下内容可以实现这一点,但它仍然会抛出CSRF保护错误。
if request.remote_ip = "127.0.0.1"
def request.forgery_whitelisted?; true; end
end是否有其他地方需要发生这种情况,以便尽早覆盖该方法以使其生效?
发布于 2011-07-02 16:36:20
以下任一操作都应该有效:
ApplicationController:中的
def verify_authenticity_token
super unless request.remote_ip = '127.0.0.1' # TODO: replace this with actual white-listing logic
endmodule ActionDispatch
class Request
def forgery_whitelisted?
super if remote_ip == '127.0.0.1' # TODO: replace this with actual white-listing logic
end
end
endhttps://stackoverflow.com/questions/6545392
复制相似问题