为 Rails 中的路由添加安全性 [英] Adding security on routes in Rails

查看:36
本文介绍了为 Rails 中的路由添加安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 2 中,如何防止用户仅更改 id # 并访问其他对象?

In Rails 2, how can I prevent a user from just changing the id # and accessing other Objects?

例如:

website.com/users/1231/edit

如何防止用户更改 1231 并访问另一个帐户?

How do I prevent a user from changing the 1231 and accessing another account?

推荐答案

在控制器中使用 before_filter.

class Users < ApplicationController
  before_filter :require_user, :only => [:show]

  private

  def require_user
    @user = User.find_by_id(params[:id])
    redirect_to root_url if @user.nil?
  end

end

这篇关于为 Rails 中的路由添加安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆