Nancy McWilliams wrote:

“Moralization is a close relative of [the defense of] rationalization… rationalization converts what the person already wants into reasonable language; moralization puts it in the realm of the justified or morally obligatory”

Moralizing is the way you defend yourself against the scary reality that you are selfish and that the fulfillment of your desires will often lead to someone else’s pain.