How can I code a JSON REST API for creating/updating passwords whilst
being as secure as possible and avoiding the various problems with
using Strings?
Well API keys are not true passwords. You have control on how API keys are created so you can create some random string which will have a very low collision (ie double UUID) and very low common substring (in the case of dedup). After the client logs in through the REST API using the key you could use temporary tokens thus improving the likelihood of the API key getting garbage collected.
As for dealing with real passwords which is the case for a human logging in (perhaps to reset the API key) you don't really have much options given that almost every servlet container will turn request parameters into strings. One cheesy option is to have the client through Javascript (or whatever your clients are) Base64 encode the password and then add a separator and then add a randomly generated number or string to the password. This is not really for obfuscation but again to lower the probability of keeping the same string around. You'll have to be careful of course to decode into char or byte array and then remove the random suffix by manipulating the char or byte array (see CharBuffer).
Another complicated option is the microservice cloud approach. Just make an authentication service composed of a couple of tiny round robin instances that only do authentication. Have those JVM instances get restarted frequently (to flush memory). Or if they are small enough they will hopefully garbage collect more frequently.
I'll assume of course that your data repository has salted passwords (otherwise this safety precaution is pretty moot).
To be honest though there are so many other threats that I don't really think its worth the effort for most use cases in a HTTP server environment.
The reason why Java Swing uses char[]
for password because Swing is used for a desktop environment. Desktop environments are far more likely to have malicious programs such as virus/spyware that could do some memory probing for passwords.
With that in mind its really the clients you should worry about and not the server.