我一直在使用REST API开发Rails应用程序,以便从移动应用程序访问.
它运作得很好.当用户从移动应用程序登录时,他会auth_token在未来的API请求中使用它.问题是,通过转到路径/ api/v1/...也可以从Web访问API,因此必须保护它不受CSRF的影响.
我有BaseApiController继承自" ApplicationController已protect_from_forgery启用"的类.这是一个例子:
class Api::V1::BaseApiController < ApplicationController
# ...
end
class ApplicationController < ActionController::Base
protect_from_forgery
# ...
end
Run Code Online (Sandbox Code Playgroud)
现在,当我对我的API执行非GET请求时auth_token,我的请求成功完成,但在日志中我可以看到着名的WARNING: Can't verify CSRF token authenticity.如果我protect_from_forgery从我的中删除BaseApiController,我没有得到任何警告(显然),但是我的API容易受到CSRF攻击(我制作了一个简单的HTML表单,当没有时,我成功地跨域更改了数据protect_from_forgery).
我的问题是:如何确保我的API保持安全,但在执行非GET请求时也会删除警告?
这是我提出的解决方案之一,但它看起来更像是一个黑客并执行一个额外的数据库查询:
class Api::V1::BaseApiController < ApplicationController
# ...
def verified_request?
super || User.where(authentication_token: params['auth_token']).count > 0
end
end
Run Code Online (Sandbox Code Playgroud)
关于该项目的更多细节:Rails 3.2.14,Devise,AngularJS.该项目的源代码可以在这里找到.
我将 ActiveAdmin gem 与 Pundit(和 Rolify)gem 一起使用。
这就是我编写策略的方式(取自: https: //github.com/activeadmin/activeadmin/blob/master/spec/support/templates/policies/application_policy.rb):
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def show?
scope.where(id: record.id).exists?
end
def create?
user.has_role?(:staff, record.company)
end
def update?
scope.where(id: record.id).exists?
end
def destroy?
scope.where(id: record.id).exists?
end
def destroy_all?
true
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
company_ids = Company.with_role(:staff, user).map(&:id) …Run Code Online (Sandbox Code Playgroud) 我试图让一个shell脚本在覆盆子pi上运行每分钟,使用crontab,如下所示:
crontab -e
Run Code Online (Sandbox Code Playgroud)
然后:
* * * * * /home/pi/job.sh
Run Code Online (Sandbox Code Playgroud)
工作是:
#!/bin/sh
echo "hello"
Run Code Online (Sandbox Code Playgroud)
我期待该消息确保脚本正在执行,但什么都没发生.是否有任何特殊技巧可以让代码在raspberry pi上每分钟运行一次?
是否有可能在最后使用爆炸来修补方法?
我想要猴子补丁String.upcase!,但我不知道如何实现.
问题是我想要更改原始字符串对象.
这是一个例子:
class String
def upcase!
self.mb_chars.upcase.to_s
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果我在控制台中输入并尝试它,它不起作用:
[1] pry(main)> asd="asd"
=> "asd"
[2] pry(main)> asd.upcase
=> "ASD"
[3] pry(main)> asd
=> "asd"
[4] pry(main)> asd.upcase!
=> "ASD"
[5] pry(main)> asd
=> "asd"
Run Code Online (Sandbox Code Playgroud)