Like Hawkeye2 says, use Loctite.
Remove any oil from the threads on the shaft and in the nut. Use the Blue, medium strength Loctite, Never use the Red (permanent) Loctite in this application because some day you might want to remove the hammer.
It doesn't take much. Just a small drop on the external threads, then screw the nut on and fully tighten it.
Loctite isn't an epoxy so it won't harden on the parts until it is squished by tightening the nut. Once the nut is tight, it reacts with the steel and begins to harden.
It sometimes takes a full 24 hours to harden so give it time before you "fire" the lock.
It won't work on stainless steel, rusty steel or iron, or aluminum threads unless they are first coated with "Primer T" but because all of the parts on your guns lock are steel, it will work fine.