How to override X-Frame-Options for a controller or action in Rails 4

后端 未结 3 1951
鱼传尺愫
鱼传尺愫 2020-12-04 10:53

Rails 4 appears to set a default value of SAMEORIGIN for the X-Frame-Options HTTP response header. This is great for security, but it does

3条回答
  •  眼角桃花
    2020-12-04 11:23

    If you want to remove the header completely, you can create an after_action filter:

    class FilesController < ApplicationController
      after_action :allow_iframe, only: :embed
    
      def embed
      end
    
    private
    
      def allow_iframe
        response.headers.except! 'X-Frame-Options'
      end
    end
    

    Or, of course, you can code the after_action to set the value to something different:

    class FacebookController < ApplicationController
      after_action :allow_facebook_iframe
    
    private
    
      def allow_facebook_iframe
        response.headers['X-Frame-Options'] = 'ALLOW-FROM https://apps.facebook.com'
      end
    end
    

    Note that you need to clear your cache in certain browsers (Chrome for me) while debugging this.

提交回复
热议问题