duanji1056 2013-04-20 15:10
浏览 28
已采纳

通过PHP从java到mySQL的安全三层连接[关闭]

First of all, I know this has been asked a few times now, and I have done lots of research on the subject, but I can't seem to find a definitive answer. I am either missing something vital here or this isn't possible and I'm just wasting your time. Here goes:

Imagine a database, which stores let's say only a 1000 rows, consisting of an id, a boolean value (0 or 1) that must be at some point changed and a constant string value with some information. The information can be accessed and the value may be changed ONLY if the user has commited a required action in the client java app. That only there is vital.

During my world-wide-web search, I came across two options that could suit my needs.

  1. Set permission for each user on the database itself, and have users log in from the app using their own username and password. The client app can then communicate with the database as the user wishes, performing the tasks of reading and writing to the database as required.

  2. Use the three-tier approach, having the java app communicate with a PHP interface that executes the query and returns an encoded json string of a sorts, which the java app can parse. I get the basic concept, you send the username and password of the client to the PHP interface which checks if the client is eligible and executes the query accordingly. No biggie.

What I can't seem to figure out is how to let only the java client change and retrieve the data. For example, should someone want to know what the contents of the strings are in approach 1, they could simply use their username and password and query the database directly. The queries are easy enough to get from the java app after decompililng. As for #2, if the app is decompiled, the PHP interface can also be reverse-engineered, allowing a user to query the database by exploiting the PHP script, even without knowing the actual password and username for the database.

I have to point out that the users are not trusted at all, and the contents of the database should remain a secret until certain conditions are met in the java app. Is there a way to acomplish that? And if there is a way I am unaware of, I am most certainly not stuck on these two options. Thank you for all your help in advance.

  • 写回答

1条回答 默认 最新

  • dpbvpgvrhwxen3222 2013-04-20 15:55
    关注

    We are implementing something similar, HTML5/Javscript SPA with PHP backend, but then it makes SOAP requests to a Java server API to handle all the MySQL transactions. The benefit of this is that the client only sees HTML and minified Javascript, no calls ever to the Java server, but they are still able to see the AJAX calls to my PHP server. Since the Java functions are called via SOAP directly from the server and not the client, I focus 90% of the security on the HTML5/PHP server. The Java server just has to restrict access to my server IP and only accept properly formatted SOAP requests.

    That being said, I have checks in place to discourage this, and hopefully prevent 99% of the users from accomplishing "hacking" the system: sessions, timers to ensure clients don't make too many calls too quickly or without a call prior to that, minifying code, setting specific headers in my calls, etc etc. Is it fallible? Yes. But there are logging systems in place to keep track of attempts. We can then block access to those who make invalid requests too often or whatever.

    Ultimately a similar setup would work for you, but keep in mind, logging invalid calls and routinely going through those or setting up a notification system would reduce attempts. If you are using MySQL calls directly in your Java portion that users could decompile, I would discourage that, but instead just call a function on the PHP server that handles those calls. This will at least hide the database description from the user to discourage hacking.

    With every app, just monitor it closely. When you see a successful hack, figure out a way to thwart it the next time. That way it will never work the same way twice.

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

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题