构建API服务器1

本文来源于ruby-china
https://ruby-china.org/topics/25822

1. 新建项目

rails new build-an-api-rails-demo

2. 生成控制器

#不生成资源文件
rails g controller api/v1/base --no-assets

编辑 app/controller/api/v1/base_controller.rb

class Api::V1::BaseController < ApplicationController
  #disable the CSRF token
  protect_from_forgery with: :null_session
  #protect_form_forgery :with => :null_session

  #disable the cookies (no set-cookies header in response)
  before_action :destroy_session

  #disable the CSRF token
  skip_before_action :verify_authenticity_token

  def destroy_session
    request.session_options[:skip] = true
  end
end

在BaseController中禁止了CSRF token 和cookies⬆️

3.配置路由

namespace :api do
  namespace :v1 do
    resources :users, only: [:index, :create, :show, :update, :destroy]
    #⬆️⬇️两种写法是等价的
    #resources :users, :only => [:index, :create, :show, :update, :destroy]
  end
end

4. 生成控制器(UsersController)

rails g controller api/v1/users --no-assets

编辑app/controller/api/v1/users_controller.rb

class Api::V1::UsersController < Api::V1::BaseController
  def show
    @user = User.find(params[:id])
  end
end

编辑app/views/api/v1/users/show.json.jbuilder

json.user do
  json.(@user, :id, :email, :name, :activated, :admin, :created_at, :updated_at)
end

5. 建立User模型和users表

rails g model User

app/models/user.rb

class User < ActiveRecord::Base
end

db/migrate/****_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :name
      t.datetime :activated
      t.boolean :admin, default: false
      t.timestamps null: false
    end
  end
end

6. 生成测试数据

db/seeds.rb

users = User.create([
  {
    email: 'test01@example.com',
    name: 'test01',
    activated: DateTime.now,
    admin: false
  },
  {
    email: 'test02@example.com',
    name: 'test02',
    activated: DateTime.now,
    admin: false
  }  
])

创建种子数据

rake db:seed

7. 运行服务器并测试

rails s -b 0.0.0.0
#
curl -i http://localhost:3000/api/v1/users/1.json

8. 增加认证过程

上述的过程并没有用户认证,可以直接获取用户信息,这是不安全的.

认证过程是这样的,用户把用户名和密码通过HTTP POST请求发送到API,如果用户名和密码匹配,我们就会把token发送给用户。这个token就是用来证明用户身份的凭证。然后在以后的每个请求中,我们通过这个token来查找用户,如果没有找到就返回401错误

#给User模型增加authentication_token属性
rails g migration add_authentication_token_to_users

db/migrate/***_add_authentication_token_to_users.rb

class AddAuthenticationTokenToUsers < ActiveRecord::Migration
  def change
    add_column :users, :authentication_token, :string
  end
end
rake db:migrate

生成authentication_token
app/model/user.rb

class User < ActiveRecord::Base
  before_create :generate_authentication_token

  def generate_authentication_token
    loop do
      self.authentication_token = SecureRandom.base64(64)
      break if !User.find_by(authentication_token: authentication_token)
    end
  end

  def reset_auth_token!
    generate_authentication_token
    save
  end
end

9. 生成session控制器(用户认证用)

rails g controller api/v1/sessions --no-assets

app/controller/api/v1/sessions_controller.rb

class Api::V1::SessionsController < Api::V1::BaseController
  def create
    @user = User.find_by(email: create_params[:email])
    #authenticate是bcrypt这个Gem包中的方法
    if @user && @user.authenticate(create_params[:password])
      self.current_user = @user
    else
      return api_error(status: 401)
    end
  end
  private
  def create_params
    params.require(:user).permit(:email, :password)
  end
end

10. 给User模型增加password相关属性

在Gemfile里将gem 'bcrypt'

#Use ActiveModel has_secure_password
gem 'bcrypt'

app/models/user.rb

class User < ActiveRecord::Base
  #加这句话user有password属性,对应表字段password_digest
  #可以用user.password = "123",自动转化为加密字符串
  +has_secure_password
end

给User模型增加password_digest属性

rails g migration add_password_digest_to_users

db/migratie/***_add_password_to_users

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end
rake db:migrate

给数据库中已存在的测试用户增加密码和authentication token

#在rails console中执行,但是console不能执行多行。所以这个意思
User.all.each {|user| user.password = '123123' user.reset_auth_token!  
                user.save }

实现current_user方法

app/controller/api/v1/base_controller.rb

class Api::V1::BaseController < ApplicationController
  + attr_accessor :current_user
end

实现app/views/api/v1/sessions/create.json.jbuilder

#返回用户信息和access_token,后续根据access_token就可以访问
json.session do
  json.(@user, :id, :name, :admin)
  json.token @user.authentication_token
end

实现api_err

class Api::V1::BaseController < ApplicationController 
  + def api_error(opts = {}) 
    + render nothing: true, status: opts[:status] 
  + end
end

api_error(status: 401)

11. 验证用户token

class Api::V1::BaseController < ApplicationController
+  def authenticate_user!   
+    token, options = ActionController::HttpAuthentication::Token.token_an_options
+    user_email = options.blank?? nil : options[:email]
+    user = user_email && User.find_by(email: user_email)
    
+    if user && ActiveSupport::SecurityUtils.secure_compare(user.authentication_token, token)
+      self.current_user = user
+    else
+      return unauthenticated!
+    end    
+  end
end

英语小课堂

authenticity  n. 真实性,确实性;可靠性
authentication n. 证明;鉴定;证实
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,302评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,563评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,433评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,628评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,467评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,354评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,777评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,419评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,725评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,768评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,543评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,387评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,794评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,032评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,305评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,741评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,946评论 2 336

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,517评论 18 139
  • 前面两篇内容(RESTful Web Service 架构剖析和HTTP Methods 和 RESTful Se...
    JeffreyLi阅读 15,655评论 12 191
  • 移动应用开发中,令牌授权(token-based) 是一种常用的移动端与服务端的授权登录方式 ,但是使用它,需要面...
    falm阅读 7,174评论 5 22
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,146评论 1 23
  • 螺帽表面点点锈迹好似你心脏上的红斑 不堪重负的心脏努力拉扯凝滞的血 我耐心修补在上面留下点点红斑好似螺帽表面的锈迹...
    陈果_周绿阅读 234评论 0 1