尽管控制器代码设置状态为:未授权,JRuby on Rails Production仍以200 OK响应



我的服务器在Tomcat上运行JRuby on Rails,它的GWT前端通过GWT::RequestBuilder连接。当我在Tomcat上运行WEBrick和开发模式时,一切似乎都很好,但在生产模式中,发生了一件奇怪的事情。我的登录控制器有一个在你第一次点击页面时运行的操作:

def is_logged_in
  if session[:current_user]
    # do some stuff and render response XML
  else
    puts "nothing status unauthorized"
    render :nothing => true, :status => :unauthorized
  end
end

在开发过程中,它工作得很好,我得到了一个401未经授权的错误,GWT会接收它并显示登录屏幕。在生产中,我得到了一个200 OK,没有XML响应体,即使服务器日志中显示了"未经授权的无状态"。清除Tomcat临时文件夹和webapp文件夹没有帮助。什么东西?谢谢你的帮助,希望这是我忽略的一件简单的事情。

供参考:我在Windows 7 x64中使用JRuby 1.6.6和Tomcat7。

Gemfile

source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'haml'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
platforms :ruby do
  gem 'mysql2'
end
platforms :jruby do
  gem 'activerecord-jdbc-adapter'
  gem 'jdbc-mysql', :require => false
end
group :development do
  gem 'rspec-rails'
end
group :test do
  gem 'rspec'
  gem 'webrat'
end
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
#   gem 'webrat'
# end

生产.rb

Eiserver::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb
  # The production environment is meant for finished, "live" apps.
  # Code is not reloaded between requests
  config.cache_classes = true
  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  # Specifies the header that your server uses for sending files
  config.action_dispatch.x_sendfile_header = "X-Sendfile"
  # For nginx:
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
  # If you have no front-end server that supports something like X-Sendfile,
  # just comment this out and Rails will serve the files
  # See everything in the log (default is :info)
  # config.log_level = :debug
  # Use a different logger for distributed setups
  # config.logger = SyslogLogger.new
  # Use a different cache store in production
  # config.cache_store = :mem_cache_store
  # Disable Rails's static asset server
  # In production, Apache or nginx will already do this
  config.serve_static_assets = true
  # Enable serving of images, stylesheets, and javascripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"
  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false
  # Enable threaded mode
  # config.threadsafe!
  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true
  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify
end

更新1

为了进一步阐述,希望有人能给我指明正确的方向,我98%确信问题出在Rails代码中,而不是GWT,因为对is_loged_in路由执行CURL也会得到200 OK。

更新2

这是我发现的另一个花絮,它至少让我回到了工作状态,但仍然留下了很多悬而未决的问题。我最近更新了jruby-1.6.6版本,但保留了jruby 1.6.0版本以备不时之需。当我使用旧的jruby时,事情又开始工作了。但这只是指向jruby版本和一堆宝石,我不知道如何开始找到罪魁祸首。有没有人足够熟悉一连串的插件,为我指明正确的方向?

更新3

搁置和重新访问之后。我几乎可以肯定,这与rails宝石的版本令人烦恼有关。我用我拥有的JRuby的两个版本1.6.0和1.6.6生成了WAR。代码是相同的,但1.6.0没有问题,而1.6.6存在上述问题,事实证明,无论我明确设置了什么状态代码,所有控制器都只在生产中输出200个OK响应。希望有人能找到解决方案,并在较新的JRuby-gem版本中支持它。

使用logger.warn而不是puts。此外,仅使用head返回标头。

logger.warn "nothing status unauthorized"
head :unauthorized

最新更新