简体   繁体   English

如何防止Rails向移动客户端发送cookie _appname_session(禁用cookie)?

[英]How to prevent Rails from sending cookie _appname_session to mobile clients (disable cookies)?

I use Rails 4.0.2 and Devise 3.3.0. 我使用Rails 4.0.2和Devise 3.3.0。 This application works with both Web clients and mobile clients. 该应用程序可用于Web客户端和移动客户端。 Web applications use sessions and mobile applications are authenticated using auth_token that is sent in params with every request. Web应用程序使用会话,并且使用auth_token对移动应用程序进行身份验证,该auth_token与每个请求一起以参数形式发送。

Right now I can't find a way to prevent Rails from setting and sending cookies to mobile clients - responses always contain 目前,我找不到阻止Rails设置Cookie并将其发送到移动客户端的方法-响应始终包含

Set-Cookie = request_method=GET; path=/, _myapp_session=<token...>; path=/; HttpOnly

I would highly appreciate any hints on what should I do inside my Rails Controllers by using filters or any custom rack middlewares. 对于使用过滤器或任何自定义机架中间件在Rails控制器中应该做什么的任何提示,我将不胜感激。 Also I guess that this can be solved using some custom Device strategy or something like that. 另外,我猜可以使用某些自定义设备策略或类似方法来解决。

Let me know if I should provide any additional information. 让我知道是否需要提供其他信息。

Thanks. 谢谢。

This solution: Rails 3 disabling session cookies worked for me. 这个解决方案: Rails 3禁用会话cookie对我有用。

I ended up setting a middleware: 我最终设置了一个中间件:

module MyApp
  class MobileClientsCookieFilter
    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)

      request = Rack::Request.new env

      if request.params['device'].present? or any other mobile clients checks ok?
        headers.delete 'Set-Cookie'
      end

      [status, headers, body]
    end
  end
end

and within application.rb 并在application.rb中

config.middleware.insert_before ::ActionDispatch::Cookies, MyApp::MobileClientsCookieFilter

Looks like similar solution is also possible: to subclass ActionDispatch::Cookies, in case of web clients do super call and do nothing there in case of mobile clients. 看起来类似的解决方案也是可行的:将ActionDispatch :: Cookies子类化,如果Web客户端进行超级调用,而对移动客户端则不执行任何操作。 Then to swap this custom middleware with original ActionDispatch::Cookies. 然后,将此自定义中间件与原始ActionDispatch :: Cookies交换。 Having it implemented this way no cookies would be created/generated at all for mobile clients. 以这种方式实施后,根本不会为移动客户端创建/生成任何cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM