Son*_*ack 16 ruby-on-rails ruby-on-rails-4
protect_from_forgery with: :exception工作怎么样?
我想编辑代码来查看它并从中学习.但是,我无法找到更高级别的抽象位置.
Rai*_*dal 12
你可以在Github上找到它:https://github.com/rails/rails/blob/c60be72c5243c21303b067c9c5cc398111cf48c8/actionpack/lib/action_controller/metal/request_forgery_protection.rb#L88
def protect_from_forgery(options = {})
self.forgery_protection_strategy = protection_method_class(options[:with] || :null_session)
self.request_forgery_protection_token ||= :authenticity_token
prepend_before_action :verify_authenticity_token, options
end
Run Code Online (Sandbox Code Playgroud)
将with: :exception被传递给protection_method_class(:exception).这样做:
def protection_method_class(name)
ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify)
rescue NameError
raise ArgumentError, 'Invalid request forgery protection method, use :null_session, :exception, or :reset_session'
end
Run Code Online (Sandbox Code Playgroud)
然后这个ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify).name.to_s.classify这将是Exception.
然后你可以找到:
module ProtectionMethods
class Exception
def initialize(controller)
@controller = controller
end
def handle_unverified_request
raise ActionController::InvalidAuthenticityToken
end
end
end
Run Code Online (Sandbox Code Playgroud)
所有这些都设置了处理无效真实性的方式.然后它设置before_action::verify_authenticity_token.
def verify_authenticity_token
unless verified_request?
logger.warn "Can't verify CSRF token authenticity" if logger
handle_unverified_request
end
end
Run Code Online (Sandbox Code Playgroud)
其中使用了先前定义的策略:
def handle_unverified_request
forgery_protection_strategy.new(self).handle_unverified_request
end
Run Code Online (Sandbox Code Playgroud)
提出定义中的异常Exception.
| 归档时间: |
|
| 查看次数: |
10219 次 |
| 最近记录: |