weixin_33698043 2018-11-06 13:31 采纳率: 0%
浏览 353

如何保护主API密钥?

I created a Restful API, a website (ReactJS/Ruby on Rails), and a mobile app(React Native).

I am using the API to show and process data on the website and the mobile app.

In the website, I am using jQuery AJAX requests that somehow looks like this:

...some other code
componentDidMount () {
  $.getJSON('https://example.com/api/v1/accounts?key=MASTER-API-KEY', (data) => {
    this.setState({
      accounts: data.accounts
    });
  });
}
...some other code

In the mobile app, I am using fetch that somehow looks like this:

...some other code
fetch('https://example.com/api/v1/accounts?key=MASTER-API-KEY', {
  method: 'GET',
...some other code

The users also have their own API keys with limited privilege based on their user level.

I already have the validation to process the request if only they have sent a valid API key. But on the website and the app, I am using a master API key that has access to all.

I believe that this can be seen in the source file on the website and it can be reverse engineered in the mobile app.

The possible solution that I have for the website is to make the process in the server instead of using AJAX, but how can I access it on my ReactJS components?

For the mobile app, should I switch to using Swift/Java and make the request there instead of fetch?

  • 写回答

3条回答 默认 最新

  • weixin_33716941 2018-11-06 13:43
    关注

    This should work if you are developing on node (as i guess you are):

    using process.env by hackernoon

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器