当我尝试运行以下命令的
时curl -u a@aa.com:a http://localhost:3002/home/test_json.json
和
curl http://localhost:3002/home/test_json.json -X GET -d 'user[email]=a@aa.com&user[password]=a'
我收到了以下回复
{"error":"You need to sign in or sign up before continuing."}
状态401未经授权。
我不知道为什么会出现这个错误,即使我已经使用这些命令提供了用户电子邮件和密码。
以下是我的代码。
应用程序控制器
class ApplicationController < ActionController::Base
#skip_before_filter :verify_authenticity_token
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :authentication_token) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :authentication_token) }
end
protect_from_forgery with: :exception
end
家庭控制器
class HomeController < ApplicationController
#skip_before_filter :verify_authenticity_token
before_filter :authenticate_user!#, except: [:test_json]
def index
end
def test_json
msg = {success: true, data: 'abcd' }
render json: msg
end
end
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable
end
配置/初始化/ devise.rb
# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/active_record'
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :email ]
# Configure which authentication keys should have whitespace stripped.
# These keys will have whitespace before and after removed upon creating or
# modifying a user and when used to authenticate or find a user. Default is :email.
config.strip_whitespace_keys = [ :email ]
# By default Devise will store the user in session. You can skip storage for
# particular strategies by setting this option.
# Notice that if you are skipping storage for all authentication paths, you
# may want to disable generating routes to Devise's sessions controller by
# passing :skip => :sessions to `devise_for` in your config/routes.rb
config.skip_session_storage = [:http_auth]
# ==> Configuration for :database_authenticatable
# For bcrypt, this is the cost for hashing the password and defaults to 10. If
# using other encryptors, it sets how many times you want the password re-encrypted.
#
# Limiting the stretches to just one in testing will increase the performance of
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
# a value less than 10 in other environments.
config.stretches = Rails.env.test? ? 1 : 10
# If true, requires any email changes to be confirmed (exactly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed new email is stored in
# unconfirmed email column, and copied to email column on successful confirmation.
config.reconfirmable = true
# ==> Configuration for :validatable
# Range for password length.
config.password_length = 1..128
# Time interval you can reset your password with a reset password key.
# Don't put a too small interval or your users won't have the time to
# change their passwords.
config.reset_password_within = 6.hours
config.secret_key = '1a06c85c7aaaf467562cbeb150f152b44c7a26de252fca87a1db604489bfaa4b40da4f06f8a9e59f150ee0d6e07ace86aab33d5ec8a159fdbe652ffa8745e4dc'
end
的routes.rb
DeviseTest::Application.routes.draw do
devise_for :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root "home#index"
get 'home/test_json' => 'home#test_json'
end
对不起我的英语和语法,请帮助。
提前致谢。