Dealing with an unprofessional co-worker is one thing, but when your boss is crossing the line and making your career unpleasant, it's another issue entirely. Confrontation in the workplace is never easy, and the fact that you may have to lock horns with your boss can make it tempting to let unprofessional behavior slide. However, you deserve to be treated with dignity and respect, and there are indeed ways to go about handling the issue in a tactful manner.
Maintain a high level of performance on the job. If you see a confrontation or awkward talk on the horizon, you do not want to give your boss or his supervisors any complaint against you. How well you do your job is one thing you can control in the situation; make the best of it. An exemplary performance will give you more leverage. For example, your boss may respect your opinion more when considering that it's "you," the employee who always delivers and makes him look good.
Set boundaries as to how much you are willing to put up with. No one is perfect, and there are situations in gray areas no matter where you work. Remember, you don't want to be a whistle-blowing malcontent. On the flip side, you should not be a doormat, either. Simply decide what you are and aren't able to tolerate comfortably and effectively do your job.
Document all untoward incidents, as you may need the factual information in the future. A written record with as many details as possible can help your cause. For example, if things escalate to the point Human Resources needs to get involved, it will help you to have details recorded.
Confront your boss in a polite yet firm manner. Let him know what makes you uncomfortable and why, but avoid being emotional or accusatory. The more matter-of-fact you are, the better. The key is to not place any blame; use phrases like "from my point of view" and "in my opinion" and describe the behavior your find objectionable.
If the boss's unacceptable behavior continues after you discuss it with him, consider looking for a new job or weighing whether to report bad behavior to the boss's boss. In either case, do not let your performance suffer.