President Barack Obama declared an end to a fearful chapter in American history on Thursday and demanded more from the country and himself as it enters the next.

Obama’s speech at the National Defense University reflected an unusual ambivalence from a commander in chief over the morality of his administration’s counterterrorism policies.

In his wide-ranging discussion of the issues at stake, Obama also suggested the depth of the difficulty he has faced in fulfilling his pledge to bring America’s national security policies fully in line with its founding values.

He discussed a drone program that he acknowledged kills civilians, a far-reaching investigation into news leaks that he said demands review, and an offshore prison for terrorism suspects that he has been trying to close for years.

“We will have to keep working hard to strike the appropriate balance between our need for security and preserving those freedoms that make us who we are,” he said, adding that much of that work will fall to him.

After four years of alarming intelligence reports and attacks that were prevented and those that were not, Obama sounded like a former constitutional law lecturer who sees the nation and its security challenges in more shades of gray than he once did.

The speech was a mix of defensiveness and contrition over the choices he has made — all of which, he argued, have been preferable to the alternatives.

That includes the expansion of drone strikes well beyond America’s defined battlefields. He made the case that the program, which has killed four American citizens abroad, puts at risk far fewer civilians than more-direct military intervention.

As the post-Sept. 11, 2001, wars come to an end, Obama said, Congress should alter the law that has authorized the use of military force since then, providing firmer legal ground for such strikes.

It was one of several arguments for changes that would not, ultimately, restrain his ability to act in the way he chooses.

“It was an effort to align himself as publicly as possible with the critics of the positions his administration is taking without undermining his administration’s operational flexibility,” Benjamin Wittes, a senior fellow at the Brookings Institution, wrote on the website Lawfare. “To put it crassly, the president sought to rebuke his own administration for taking the positions it has — but also to make sure that it could continue to do so.”

In that respect, the address stood in contrast to the one Obama delivered four years ago at the National Archives, when he advocated an urgent change to the national security polices of a country he said had veered “off course” under President George W. Bush.

Obama condemned the Bush administration’s detention and interrogation polices as contrary to American values, and he banned the use of harsh interrogation techniques that human rights groups called torture.

But Obama has failed to make changes in other areas, most notably in closing the prison camp at Guantanamo Bay, Cuba.

More recently, the disclosure of a Justice Department investigation into national security leaks that secretly collected journalists’ phone records has put Obama at odds with his promises to run a more transparent government.

Although there is little political pressure on Obama to change counterterrorism policies largely supported by the public, his advisers say it is an area where he cares personally about how history judges him and what he leaves in place for his successor.

To his supporters, particularly on the civil-liberties left, his record has been more disappointing than inspirational. He appeared to accept that Thursday in a confrontation with the activist Medea Benjamin of the group Code Pink.

Interrupted by Benjamin several times with criticism of various elements of his counterterrorism policy, Obama resigned himself to pausing for nearly a minute to hear her out.

Benjamin complained about his failure to close the prison at Guantanamo Bay and about the drone program. When she had finished, Obama said, “The voice of that woman is worth paying attention to,” even though he admitted that he disagreed with much of what she had said.

“These are tough issues,” he said, straying from his prepared text. “And the idea that we can gloss over them is wrong.”

Obama argued Thursday that his use of drones was morally preferable to the deployment of troops.

But he also said the program must be more accountable as the American focus abroad shifts from two vast war zones to scattered smaller ones, often lawless frontiers where drones will likely be relied on even more than today.

Obama addressed the Justice Department investigation into national security leaks in much the same way — as a necessity to protect classified information, but one that should be more carefully monitored than it has been.