401未经授权使用rails 4.1.4并设计3.2.4

时间:2014-08-22 13:00:58

标签: ruby-on-rails ruby devise mongoid

使用rails 4.1.4,设计3.2.4和mongoid 4.0,

我登录时遇到此问题;我在登录到商店/仪表板后重定向用户; 出于某种原因,我得到了401 Unauthorized。此外,我必须包含用户模型的补丁。 由于一个原因,authenticate_user无法正常工作

控制器

class StoresController < ApplicationController

  before_action :authenticate_user!, :except => [:index]

  def index
  end

  def dashboard
  end
end

的routes.rb

get 'stores/dashboard' => 'stores#dashboard', :as => 'user_root'
root :to => 'stores#index' , :as => 'stores'

在user.rb上打补丁

class << self
    def serialize_from_session(key,salt)
      #record = to_adapter.get(key[0].to_param)                                                                                                                                 
      record = to_adapter.get(key.to_s)
      record if record && record.authenticatable_salt == salt
    end
end

日志

Started POST "/users/sign_in" for 127.0.0.1 at 2014-08-21 09:48:59 -0400
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"n7t7mLISBOGMYWxJWzw8qNHwP3y9HGR+79P0vmEodEg=", "user"=>{"email"=>"victornsosa@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.7452ms
  MOPED: 127.0.0.1:27017 QUERY        database=prometheus_development collection=users selector={"$query"=>{"email"=>"victornsosa@gmail.com"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.6844ms
  MOPED: 127.0.0.1:27017 UPDATE       database=prometheus_development collection=users selector={"_id"=>BSON::ObjectId('53f5eded6265743c0a010000')} update={"$set"=>{"last_sign_in_at"=>2014-08-21 13:32:55 UTC, "current_sign_in_at"=>2014-08-21 13:48:59 UTC, "sign_in_count"=>9}} flags=[]
                         COMMAND      database=prometheus_development command={:getlasterror=>1, :w=>1} runtime: 0.5836ms
Redirected to /stores/dashboard
Completed 302 Found in 88ms


Started GET "/stores/dashboard" for 127.0.0.1 at 2014-08-21 09:48:59 -0400
Processing by StoresController#dashboard as HTML
  MOPED: 127.0.0.1:27017 QUERY        database=prometheus_development collection=users selector={"$query"=>{"_id"=>"[{\"$oid\"=>\"53f5eded6265743c0a010000\"}]"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3786ms
**Completed 401 Unauthorized in 2ms**


Started GET "/users/sign_in" for 127.0.0.1 at 2014-08-21 09:48:59 -0400
Processing by Devise::SessionsController#new as HTML
  Rendered devise/shared/_links.erb (1.0ms)
  Rendered devise/sessions/new.html.erb within layouts/application (5.3ms)
Completed 200 OK in 20ms (Views: 18.9ms)

另外我发现:如果 user_signed_in?也不能正常工作。我猜这个问题与使用mongoid有关。

注册正在运行,包括发送电子邮件;登录看到正在工作,并按日志说明重定向到正确的页面;但是过滤器存在问题,因为不允许用户进入。

1 个答案:

答案 0 :(得分:3)

从cookies_serializer.rb

中注释掉以下一行

Rails.application.config.action_dispatch.cookies_serializer =:json

删除Cookie。 重启服务器。

发现它: https://github.com/plataformatec/devise/issues/2949