President Obama made a final public defense of his counterterrorism record at a military base in Tampa on Tuesday just hours before President-elect Donald Trump introduced his choice of a vocal Obama critic to lead the Pentagon at a rally in North Carolina.

In his remarks, Obama took on virtually all of the criticisms of his policies in the Middle East over the past eight years, including the full withdrawal of American forces from Iraq in 2011. He insisted that the United States could protect itself from terrorists without betraying core American values. Obama has said that those values are under threat from his successor.

Obama never mentioned Trump by name, but his speech served as a stark rebuttal to the counterterrorism approach that Trump and other Republicans laid out during the presidential campaign. Obama rejected the president-electโ€™s contention that โ€œdropping more bombs, deploying more troopsโ€ or placing restrictions on Muslim immigrants would make America safer.

โ€œThe United States is not a country that imposes religious tests as a price for freedom,โ€ he said. โ€œThe United States is not a place where citizens have to carry an ID card.โ€

The timing of Obamaโ€™s speech, which came only hours before Trump introduced retired Marine Gen. James N. Mattis as his choice for defense secretary, raised questions about how much of Obamaโ€™s approach will survive his successor.

Obama is finishing his second term at a time of widespread bloodshed and unrest in the Middle East. The wars in Iraq and Afghanistan, which he had vowed to end, likely will grind on long after he leaves office. โ€œOn January 20th, I will become the first president of the United States to serve two full terms during a time of war,โ€ he mournfully noted.

Obamaโ€™s defense of his record is largely built around the crises he avoided, the large number of troops he brought home from Iraq and Afghanistan, and the terrorist attacks that did not happen on his watch.

โ€œNo foreign terrorist organization has successfully planned and executed an attack on our homeland, and it is not because they didnโ€™t try,โ€ Obama said. Today, he added, there are about 15,000 U.S. troops in Iraq, Afghanistan and Syria, down from about 180,000 when he took office.

Instead of deploying large battalions of American forces โ€” an approach Obama called โ€œunwise and unsustainableโ€ โ€” the United States has relied on small teams of Special Operation forces to fight alongside Iraq, Syrian and Afghan forces.

The critique from Trump and Mattis is that the president did not do enough to prevent the Islamic State from taking root in Iraq and Syria and has moved too slowly to destroy it.