Security is an important problem in wireless sensor networks. Intrusion detection system is one of the most common methods of network security, for which more and more people have shown solicitude. In this paper, a dynamic random password authentication (DRPA) method is proposed for the identity authentication of communication nodes, which can detect and prevent malicious behavior at each stage of the network operation. This paper introduces the method of automatic generation of random passwords. When a user wants to communicate, the password will be verified to confirm whether it is a normal user or a malicious user. The passwords generation and verification process is very suitable for wireless sensor networks. By using NS2, the simulation experiments are carried out and the results show that this method is superior to the other methods.