我有一个配置了devise的漂亮的Rails API。
当我尝试通过curl注册新用户时,我得到401 router
.route('/cheques') //here the complete path would be companyId/createCheques/cheques
.post(createCheques)
,但是/ login可以正常工作。
{"error":"Invalid Email or password."}
但是,当我从Rails控制台运行时,效果很好:
curl -v -H "Accept: application/json" -H "Content-type: application/json" -d ' {"user":{"email":"test2@test.com","password":"test12345"}}' http://localhost:3000/signup
这是我的路线:
User.create! email: "test5@test.com", password: "test12345"
如何获得Rails以便给我更详细的调试信息?我不确定如何解决此问题。
答案 0 :(得分:2)
我相信您正在达到csrf保护,并且您已经添加了一些全局错误处理程序。 https://owasp.org/www-community/attacks/csrf
应用程序认为您是恶意的:D
这个想法是,如果您登录到该站点,则会得到一个cookie等等。如果有人诱骗您从其他网站发出请求(例如,伪造的通知弹出窗口或仅单击X即可关闭的广告)。
通常,它们可以重定向您,但由于拥有会话cookie,您仍然可以完全登录,因此..如果错误地对GET执行了一些破坏性操作,则它们可以触发任意多次。
为防止这种情况,所有表单在呈现时均具有csrf令牌。因此,当您提交表单时,令牌也会在服务器上提交并验证。
触发curl命令时,您没有传递csrf令牌。
当您转到http://localhost:3000/signup/sign_up并检查表单时,您会在顶部看到令牌,例如
<form class="new_user" id="new_user" action="/signup" accept-charset="UTF-8" method="post">
<input type="hidden" name="authenticity_token" value="ZrhdZbibdKzygUem67VxNrTmT5jOU/GTxgqYz3+rtErAdEKBKUrk2zMD0vu3qTh1jmlLVhlnfbYyH7/FZpKuqA==">
....
</form>
csrf令牌也位于html的开头。
所以..如果要使用curl调用控制器,则必须传递该令牌:
$ curl --cookie-jar my-cookie http://localhost:3000/signup/sign_up | grep csrf-token
<meta name="csrf-token" content="iVjcWccB8tO+ywG+CM9RD2NJQ4CkPjeyoYb9F6U0XUajabq8ZSHRoeoT1hAgxwKsqKDYP3gf4CHR6GjODLbt3w==" />
$ curl --cookie my-cookie -H "Content-type: application/json" --data-binary '{"authenticity_token": "iVjcWccB8tO+ywG+CM9RD2NJQ4CkPjeyoYb9F6U0XUajabq8ZSHRoeoT1hAgxwKsqKDYP3gf4CHR6GjODLbt3w==","user":{"email":"test1@test.com","password":"test12345","password_confirmation":"test12345"}}' localhost:3000/signup
<html><body>You are being <a href="http://localhost:3000/">redirected</a>.</body></html>* Closing connection 0
如果您确实干净地开始,这总体上会创建用户:
rails generate devise:install
rails generate devise User
# and you add the top part of the routing before the custom controllers
另一个令人惊奇的选择是,您可能已禁用了csrf保护,并以某种方式进入了登录页面:D /您已经搞砸了自定义的RegistrationsController / SessionsController
在运行应用程序的选项卡中,您可以看到在stdout上打印的日志,您将看到正在发生的事情以及正在应答的终结点。