现在的位置: 首页 > 综合 > 正文

ActionController::InvalidAuthenticityToken 解决办法

2012年12月01日 ⁄ 综合 ⁄ 共 907字 ⁄ 字号 评论关闭

按照第三版书Agile Web Development上讲得例子做

操作步骤如下: 

rails2.0在environment.rb中打开 

config.action_controller.session_store = :active_record_store 

 

 # Use the database for sessions instead of the cookie-based default,
  # which shouldn't be used to store highly confidential information
  # (create the session table with "rake db:sessions:create")

主要是为了用数据库来保存session信息。所以要打开此开关。 

在做这个操作一定要同时修改下面的操作

 in your environement.rb you have : 

config.action_controller.session = { 
    :session_key => '_myapp_session', 
    :secret      => 'secretpass' 
  } 

 

当然书上也是这么指导的,但是我还是遇到这个问题。

原因如下:

这里涉及到rails2.0以后为了提高安全性,防止伪造当前web程序的链接,技术上是嵌入一个随机字符窜在会话中,这样攻击者无法知道,保证了不会有通过其他网站控制器从CSRF的攻击行动。如果想要了解更多CSRF(Cross-site Request Forgery)请参考如下网站

http://en.wikipedia.org/wiki/Cross-site_request_forgery

http://isc.sans.org/diary.html?storyid=1750

解决方法:

如果按照上面的步骤,那么只需 Turn on request forgery protection. 在控制器中对于公开的实例方法开启请求伪造的开关。 特别注意,只在非Get的Html/JavaScript请求,保证网站的数据安全性。

 protect_from_forgery :except => :index

 

 

 

抱歉!评论已关闭.