I'm so glad you asked this, and am planning to get a lot of the things recommended here. My daughter is 3 and 1/2, she's never been out of my sight other than a few brief stays with relatives. I was wondering this same thing as she is VERY friendly and trusting of everyone. She loves hugging etc.
One decision I made was that I was going to start having her not hug people other than relatives and supervised close friends. Whenever she looks a little shy or reluctant to hug (almost never!), I say, "That's OK, you don't have to hug" and tell the person what I'm teaching her-everyone understands.
Also, we were out recently in a big store, and she kept running just a little too far away, not really, but you know how it is with the racks. She's very well behaved and stays "near me" but not near enough and I couldn't get her to stop running when I was hugely pregnant and couldn't dash after her to enforce commands. I finally just told her calmly, "Look, there are a lot of people here, and some are not nice. You have to stay close to me, it's dangerous."
I hated to instill fear in her, but I figured, hey, she HAS to stay with me, so the stronger the message the better. I realize I could have said "Some people MAY not be nice" but I felt she is too young for differentiation games, no one can judge a book by it's cover, so assuming there are a couple of bad apples wherever you go never hurts. She stuck right with me the whole time and didn't seem upset, and I figured I can elaborate in talks as she gets older, since this will always be a real issue. I'll relieve fear later, but so far she seems to just "get it", and unfortunately, the world isn't kind, so fear as a constant reminder to be careful is necessary.
Basically, I don't think you should ever leave it up to your young child to try to know the difference between appropriate and inappropriate etc. It's not up to them. Hideous people are very sly and seem nice. I would set universal rules, like no hugging strangers or taking things from them and firm ways to smile (or maybe not smile) and say "NO thank you." Anyone who would have a problem with a child acting this way, is probably trouble, and not my concern anyway. I don't force myself on kids as far as hugging and being too friendly and no one should. Again, nice people you approve of can be approved by you under supervision. I've told all my male friends already I'm teaching her not to hug men, and they've been really nice about greeting her more like a young lady now that she's not such a cuddly baby, and they really respect the concept.
We were in a cafe and I was chatting with a man about art, and he was really nice and wanted to hug my daughter when we were leaving, and I said, "Oh, that's so nice, but I'm actually teaching her not to hug men" and he said, "Oh, I understand! Good thinking!" This way she saw me being nice but assertive, and him being gracious and us saying "No" so she can see how nice people act going forward when she says no.
Thanks for asking this-I'm looking forward to all your tips!