0

In my Android app, I make requests to my backend API and add a auth header value so that only my app can access my API data. I'm using OKHttp which makes it simple .addHeader("name", "value")

However, right now I'm simply hardcoding this header name and value in my Java file. It seems that people are able to decompile Android apps and will be able to see my auth header value.

Is there a way I can prevent this from happening?

JK140
  • 775
  • 2
  • 10
  • 19

1 Answers1

0

This is a very discussed topic and it's always a tradeoff.

Some strategies are

  • Hardcoded in Java
  • In shared preferences, assets or resources folders
  • Using the NDK
  • Public/private API key exchange

Article's conclusion

What option you choose is probably going to be determined by how much control you have over the backend server. If you don’t have any control then you’re probably going to have to hide the API key using the NDK. If you do then we recommend the Public/Private encryption of the API key using nonces to prevent any replay attacks. In the next article we’ll look at the security implications of supporting earlier Android OS versions, as well as how some Android phones are more secure than others.

Subscribe to our Android Developer Newsletter Join our Android Developers newsletter to get all the top developer news, tips & links once a week in your inbox

Full article

Possible duplicated question/answer

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64