12月13日 什么是help_method,session的简单理解, find_by等finder 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)}'" |
?
?
?
?
轉載于:https://www.cnblogs.com/chentianwei/p/8031913.html
總結
以上是生活随笔為你收集整理的12月13日 什么是help_method,session的简单理解, find_by等finder method的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 踏实肯干的工作作风的句子93句
- 下一篇: 诸葛亮诫子书原文及翻译