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里面使用這個(gè)method了。
?
finder method
find_by: 屬于ActiveRecord提供的finder methods 之一。通過傳遞argument來(lái)在database中查找。
? ? ? ?The find_by method finds the first record matching some conditions.
find_by!: 和find_by一樣,但nill的話,會(huì)報(bào)錯(cuò)!ActiveRecord::RecordNoFound?
find_by_id: find_by_id(params[:id]) ,估計(jì)這個(gè)用法不再使用了。
?
?
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實(shí)體,such as find and first, return a single instance of the model)?
?
session[]: 見http://guides.rubyonrails.org/action_controller_overview.html?
詳細(xì)解釋:有整整一章http://guides.rubyonrails.org/security.html?
簡(jiǎn)單說(shuō):翻譯“一段會(huì)話”我的理解就是用于儲(chǔ)存的一小塊數(shù)據(jù),在controller和view中使用,默認(rèn)是儲(chǔ)存在cache緩存中,因此不要儲(chǔ)存關(guān)鍵的數(shù)據(jù),復(fù)雜的數(shù)據(jù)。?
??
?關(guān)鍵翻譯: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中不使用它,它不會(huì)自動(dòng)加載,所以無(wú)需禁用它。?
?
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(運(yùn)行機(jī)制) for the session hashes. The most important is?ActionDispatch::Session::CookieStore?
(行為快速處理::一段會(huì)話::cookie存儲(chǔ))?
retiever(a dog that?to find and bring back sth for its master) ;retieve( to find sth and bring it back)
什么是cookiestore:在client-side儲(chǔ)存session hash,sever-side可以通過session-id來(lái)快速的調(diào)用數(shù)據(jù),這樣有不安全的可能。client可以看到session的內(nèi)容,Rails4開始使用加密算法 cookie(secrets.secret_key_base)
?
Replay Attacks for cookiestore sessions :(一種攻擊方法。)
解決辦法:使用a nonce(a random value)放置在session中,每個(gè)nonce只能用一次 。最好的辦法還是不要存敏感信心如credit在session中,只在session中保留登陸user_id。
?
Session Fixation:(一種攻擊方法。)
我的理解:攻擊者從server端獲得一個(gè)合法的session并保持長(zhǎng)期活躍,然后通過感染頁(yè)面的方式讓用戶使用attacker的這個(gè)陷阱session,用戶始終不知道自己的session被篡改了。
Countermeasure: (應(yīng)對(duì)措施)?reset_session使用的Devise gem就有這個(gè)代碼功能.
每次登陸登出都會(huì)自動(dòng)分配新的session給用戶。?
?
Session Expiry:(session到期結(jié)束,一個(gè)安全方法。)
通過在服務(wù)器端設(shè)定session的 到期time_stamp 時(shí)間。
1.登陸多久后重置session. 但如果attacker在允許的登陸時(shí)間內(nèi)反復(fù)登陸可以避開這個(gè)辦法,所以需要用到下面的2.
2.設(shè)置這個(gè)session的最長(zhǎng)壽命如2天。
| delete_all "updated_at < '#{time.ago.to_s(:db)}' OR??created_at < '#{2.days.ago.to_s(:db)}'" |
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/chentianwei/p/8031913.html
總結(jié)
以上是生活随笔為你收集整理的12月13日 什么是help_method,session的简单理解, find_by等finder method的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 踏实肯干的工作作风的句子93句
- 下一篇: 来呀,快活呀。iOS 超级码农群:538