The solution to all of this? No idea. There are some things that could help, however.
I think having every doctor around set up a pain management contract with each patient would help. This is a document that the doctor and patient sign, saying that the patient will only go to that doctor for pain meds and will go to only one pharmacy to fill prescriptions. One copy is for the doctor's office, one for the pharmacy, one for the patient. In it the patient promises not to pester the pharmacy or doctor's office for unneeded refills, and if they do, the pharmacy will stop filling their prescriptions and the doctor may stop seeing them and will cancel all refills on all medications, plus report them to law enforcement as a potential abuser. Usually there's some kind of schedule of how often the patient needs to be seen and if they need to see some other doctor, some kind of permission system. Yes, it's kind of a pain in the neck. Yes it seems limiting and cruel to patients. But for patients who are really in pain it makes sure they get the medication they need without the addicts getting in the way.
The other solution is having a prescription database that collects information about each prescription for a controlled substance so if someone turns up at the pharmacy and doesn't seem like they're in pain but demands a vast quantity of morphine without billing insurance, the pharmacy can look to see if that person has had a similar prescription filled elsewhere recently. The looking part is key however. If pharmacies are only required to submit data to the registry and aren't required to look at it to verify no prescription abuse, it doesn't really do all that much good.
Of course, I also like to blame doctors, because hey. Someone has to pick on them. Some doctors are far too free with their pain prescriptions for people and they make no effort to wean them off pain pills (ok, ok, tablets) after an incident where they needed pain relief. I disapprove. If every person who walks in the door gets a 30 day supply of vicodin taking 1 every 4 hours for "back pain,", that's a little bit much. Bad patient care. Perhaps laziness. Perhaps they also hate being pestered by the junkies (probably yes).
Whatever we do, we need to do more.