President Obama gave a speech on foreign policy achievements after receiving a Central Command security briefing and thanked troops for their service in the Middle East. (Reuters)

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.

Retired U.S. Marine Corps General James Mattis has been chosen to be secretary of defense by President-elect Donald Trump, according to people familiar with the decision. (Jenny Starrs/The Washington Post)

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.

In Mattis, Trump has chosen a former military commander who earlier this year described Obama’s strategy to defeat the Islamic State as “unguided” and “replete with half measures.”

“The bottom line on the American situation is quite clear,” Mattis said in a speech at the Center for Strategic and International Studies this past spring. “The next president is going to inherit a mess. That’s probably the most diplomatic word you can use for it.”

What President Obama’s executive actions mean for President Trump

Mattis has argued for the United States to take a much tougher stance against Iran, which he described as the “single most belligerent actor in the Middle East.”

It isn’t clear exactly what Trump, despite his bellicose rhetoric, would change about the president’s approach to counterterrorism and the war against the Islamic State. On the campaign trail, Trump said he had a secret plan that would lead to a quick and effective “total victory.”

“I don’t want the enemy to know what I’m doing,” he said.

Mattis has suggested that the United States should devote more resources to fighting the Islamic State, but he has not pressed for a major change in the president’s central strategy of relying on local partners.

“I think what we’re doing right now in Iraq, while it may not be sufficient, is certainly on the right path,” he said.

Obama’s final speech in defense on his counterterrorism policy, meanwhile, focused on the dangers of overreach and the pitfalls over overstating the terrorist threat. At moments, he seemed to be speaking directly to Trump and retired Lt. Gen. Michael Flynn, the incoming national security adviser, who have cast the battle against Islamic extremism as a global fight for survival.

“These terrorists want to cast themselves as the vanguard of a new world order,” Obama said. “They are not. They are thugs and they are murderers and they should be treated that way.”

He denounced the use of torture, including waterboarding, which Trump has spoken of approvingly and, as he has since his first days in office, Obama pressed Congress to close the detention facility at Guantanamo Bay, Cuba.

“Until Congress changes course it will continue to be judged harshly by history,” Obama said. “I will do all I can to remove this blot on our national honor.”

On Monday, the White House introduced a 61-page compendium of its policies governing the use of force, in hopes that the summary of legal opinions, executive orders and military directives will guide his successor’s decisions and reduce “the risk of an ill-considered decision.”

Obama’s speech in Tampa also amounted to a defense of his past decisions, including his much-criticized move to pull all U.S. forces from Iraq in 2011. Obama said the move was driven as much by necessity as strategy. The agreement that President George W. Bush signed with the Iraqi government called for American troops to depart the country by the end of 2011 unless the Iraqis agreed to an extension.

“By 2011 the Iraqis wanted our military presence to end,” Obama said.

The president’s critics contend that Obama did not push hard enough to keep American troops in the country and that the vacuum created by their departure created an opening for the Islamic State. But Obama suggested that a small American force in the country would not have been enough to stop the chaos and unrest caused by poor Iraqi governance, a hollowed-out Iraqi military and Syrian dictator Bashar al-Assad’s brutal tactics.

Obama described his response to the rise of the Islamic State as a new and smarter way of waging war.

“The bottom line is, we are breaking the back of ISIL. We are taking away its safe havens,” Obama said, using an acronym for the Islamic State. “We’ve accomplished all this at a cost of $10 billion over two years, which is the same amount that we used to spend in one month at the height of the Iraq War.”