Even by Boko Haram’s appalling standards, the raid on the small village of Dalori in northeastern Nigeria was horrific: Children were burned to death in their huts; residents fleeing flames and bullets were blown up by suicide bombers running with them. In the end scores were dead and much of the village was in ashes, and President Muhammadu Buhari’s claims of a “technical” victory against the Islamist militants seemed dubious. This is a war that will require far more than a few military successes to win.

Boko Haram, whose name is loosely translated as “Western education is a sin,” has been waging a ruthless Islamist terror campaign of bloody raids and suicide bombings for years in Nigeria’s northeast, the poorest region of the country. The militants achieved special notoriety in 2014 when they abducted more than 200 schoolgirls, most of whom are still missing. The former president, Goodluck Jonathan, did little to curb them, one reason he lost in elections last March to Mr. Buhari, a former general and onetime dictator who pledged to rout Boko Haram and end corruption at all levels of government.

Mr. Buhari has made a difference. The notoriously underequipped and undisciplined military has been receiving arms and supplies more regularly and has driven Boko Haram from several of its strongholds. But as the raid on Dalori and several other recent attacks have demonstrated, winning back territory is not the same as destroying the movement. When under attack, Boko Haram units disappear into the countryside, only to resurface when conditions allow. As with the Islamic State, its terror tactics also serve as a recruiting tool in the predominantly Muslim region, which harbors strong resentments toward the government, the Christian south and the Nigerian Army soldiers who stand accused of terrible human rights abuses against civilians while purportedly hunting down Boko Haram.

President Buhari is well aware that defeating the Islamic insurgency will require regaining people’s trust in their leaders and their soldiers. Fighting corruption and the abusive behavior of the army is also essential if he hopes to receive help from the West. Though the United States has provided help to the Nigerian military, it is legally prohibited by legislation from giving assistance to military units that have committed gross violations of human rights, like those in northeastern Nigeria. On his first official visit to Washington as president last summer, Mr. Buhari complained that the law aids and abets Boko Haram. On the contrary: The insurgency feeds on the abusive behavior of the security forces and the rampant corruption of Nigerian officials. Those are enemies only Nigeria can fight.