zoukankan      html  css  js  c++  java
  • 12月13日 什么是help_method,session的简单理解, find_by等finder method

    helper_method

        Declare a controller method as a helper. For example,

    helper_method :link_to

    def link_to(name, options) … end

    makes the link_to controller method available in the view.

    课程遇到的如current_cart,current_user. 声明后,就可以在view里面使用这个method了。


    finder method

    find_by: 属于ActiveRecord提供的finder methods 之一。通过传递argument来在database中查找。

           The find_by method finds the first record matching some conditions.

    find_by!: 和find_by一样,但nill的话,会报错!ActiveRecord::RecordNoFound 

    find_by_id: find_by_id(params[:id]) ,估计这个用法不再使用了。

    http://guides.rubyonrails.org/active_record_querying.html   有22以上中finder methods可用。

    (Finder methods that return a collection,such as where and group, return an instance of ActiveRecord::Relation. Methods that find a single entity实体,such as find and first, return a single instance of the model) 


    session[]:http://guides.rubyonrails.org/action_controller_overview.html 

    详细解释:有整整一章http://guides.rubyonrails.org/security.html 

    简单说:翻译“一段会话”我的理解就是用于储存的一小块数据,在controller和view中使用,默认是储存在cache缓存中,因此不要储存关键的数据,复杂的数据。 

      

     关键翻译:Session are lazily loaded. If youdon't access sessions in your action's code,they will not be loaded.Hence you will never to disable sessions, just not accessing them will do the job.

    session是惰性的,如果在action中不使用它,它不会自动加载,所以无需禁用它。 

    Session Guidlines

    1.Do not store large objects in a session.Instead store them in the database and save their id in the session.

    2.critical data should not be stored in session. 

    Session Storage 

    Rails provides several storage mechanisms(运行机制) for the session hashes. The most important is ActionDispatch::Session::CookieStore 

    (行为快速处理::一段会话::cookie存储) 

    retiever(a dog that to find and bring back sth for its master) ;retieve( to find sth and bring it back)

    什么是cookiestore:在client-side储存session hash,sever-side可以通过session-id来快速的调用数据,这样有不安全的可能。client可以看到session的内容,Rails4开始使用加密算法 cookie(secrets.secret_key_base)

    Replay Attacks for cookiestore sessions :(一种攻击方法。)

    解决办法:使用a nonce(a random value)放置在session中,每个nonce只能用一次 。最好的办法还是不要存敏感信心如credit在session中,只在session中保留登陆user_id。

    Session Fixation:(一种攻击方法。)

    我的理解:攻击者从server端获得一个合法的session并保持长期活跃,然后通过感染页面的方式让用户使用attacker的这个陷阱session,用户始终不知道自己的session被篡改了。

    Countermeasure: (应对措施) reset_session使用的Devise gem就有这个代码功能.

    每次登陆登出都会自动分配新的session给用户。 

    Session Expiry:(session到期结束,一个安全方法。)

    通过在服务器端设定session的 到期time_stamp 时间。

    1.登陆多久后重置session. 但如果attacker在允许的登陆时间内反复登陆可以避开这个办法,所以需要用到下面的2.

    2.设置这个session的最长寿命如2天。

    delete_all "updated_at < '#{time.ago.to_s(:db)}' OR
      created_at < '#{2.days.ago.to_s(:db)}'"

  • 相关阅读:
    Oracle死锁只会回滚跟死锁有关的那条SQL,而不会回滚整个事务
    Mysql安装过程(linux:2.6.18-194.el5,Mysql:)
    格式化分区,报/dev/sdb1 is apparently in use by the system; will not make a filesystem here!
    安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined
    mysql-5.5.25-winx64在win7 x64 免安装配置
    insert遭遇阻塞
    linux中轻松使用backspace和上下按键
    排序
    MySQL
    基于Boost的同步TCP通信
  • 原文地址:https://www.cnblogs.com/chentianwei/p/8031913.html
Copyright © 2011-2022 走看看