duan35557593 2011-04-27 15:36
浏览 24
已采纳

使用验证码进行交叉(子)域ajax表单提交

I'm running into the following problem. A frontend website (www.domain.com) is used to fill in a form that belongs to the backend (backend.domain.com). This form is protected with a captcha, and the reference value for the captcha is saved in the user session (in PHP).

The submission should be Ajax based, which gives some problems with the cross domains. Therefore a wrote a little PHP proxy on www.domain.com. This proxy requests the form of the backend. When the user submits the form, an Ajax request is made to the proxy and the proxy sends a validation request to the backend and returns the result.

All of this works quite well, except for the captcha that saves the references in the user session. Since the frontend website submits the form to the backend, the backend will use a session for the frontend.

What would be the best way to fix this? I've came up with 2 methods. The first would be to include the reference of the captcha in the form (hashed), so that no sessions are needed. The other way would be to include the form directly from the backend, using an iframe. This second method will probably work fine, but it feels really ugly. What would you suggest for a situation like this?

Update: a sequence diagram describing the situation:

Client                  www.domain.com   backend.domain.com
  |                            |                   |
  |-------visit site---------->|                   |
  |                            |-----get form----->|
  |                            |<----return form---|
  |<------return form----------|                   |
  |                            |                   |
  |-------submit form--------->|                   |
  |                            |-----submit form-->|
  |                            |<----send reply----|
  |<------captcha failed-------|                   |
  v                            v                   v
  • 写回答

1条回答 默认 最新

  • dongxueji2838 2011-04-27 17:13
    关注

    It feels like a cookie (or session number) problem. The cookie set for the session is valid for the www site, but not for the backend site. But since all of the communication goes via your front-end, could you not use the front-end site to do the authorisation?

    Client                  www.domain.com   backend.domain.com
      |                            |                   |
      |-------visit site---------->|                   |
      |                            |--get login form-->|
      |                            |<----return form---|
      |                            |set cookie         |
      |<-return login form+cookie--|                   |
      |                            |                   |
      |----submit login form------>|                   |
      |                            |-----submit form-->|
      |                            |                   |check cookie
      |                            |<----send reply----|
      |<------captcha failed-------|                   |
      v                            v                   v
    

    The www site must be setting the cookie since it's communicating with the client, but the backend checks it - as the cookie is set for a different domain, backend can't access it. Solution: www should read the cookie and forward the cookie data to backend for processing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP