The exactness of sensed data transmission is essential for wireless sensor networks(WSN) applications. Based on clustering WSN architecture, a secure routing protocol with intrusion detection is hereby proposed, wherein an energy prediction model for nodes is used to detect attacks in cluster head election phase, and the key management technique is applied to stage of cluster formation to ensure security of nodes, and a flow prediction model for nodes is used to prevent attacks related with traffic in routing phase. This study aims to improve security of WSN routing protocol. Using NS2 tool, the simulation environment is given. Simulation results show that the proposed protocol can detect and defend against several sophisticated routing attacks such as Sybil, Wormhole, Selective Forwarding and Hello flood attacks.