President Obama and German Chancellor Angela Merkel pause before their tour of the Hannover Messe Trade Fair, the world's largest industrial technology trade fair, in Hanover, Germany. (Carolyn Kaster/AP)

President Obama on Monday urged the United States’ European allies to reject the isolationist and nationalist impulses that are taking hold on the continent and pressed Europe to remain open to refugees fleeing war and poverty.

“We have to uphold our values not just when it is easy, but when it’s hard,” Obama said. The European Union is facing a major crisis of confidence caused by historic flows of refugees, a slow economy and growing fears of terrorism. Obama acknowledged those problems, but his speech at a massive trade fair here seemed designed to rally the continent.

Throughout his tour of Europe, which included stops over the past week in the United Kingdom, Obama has repeatedly touted the value of the European Union and urged citizens of Europe to reject forces of anger and division that are now increasingly a part of their politics.

“I know that some will call it blind hope when I say that I am confident that forces that bind Europe together are ultimately much stronger than those trying to pull you apart,” the president said. “But hope is not blind when it is rooted in the memory of all that you have already overcome. . . . You are the heirs to a struggle for freedom.”

In a speech that stretched nearly an hour, Obama catalogued the biggest problems facing both Europe and the United States. He talked about growing income inequality on both sides of the Atlantic, the threat of Russian aggression in Ukraine and privacy fears surrounding U.S. intelligence-collection efforts. He also described his plans to bolster the fight against the Islamic State in Iraq and Syria.

Obama said he plans to nearly quadruple the size of U.S. Special Operations forces in Syria to up to 300 troops. “Given the success, I’ve approved the deployment of up to 250 additional U.S. personnel . . . to keep up this momentum,” he said. He emphasized that the new troops are “not going to be leading the fight on the ground,” but will work with local forces who will do the fighting.

The speech in Hanover carried intentional echoes of Obama’s historic 2008 address, delivered in Berlin when he was on the cusp of being elected to the White House, said White House aides. In that speech, Obama discussed his improbable political ascent and spoke in soaring language of humanity’s “common destiny.” He described an array of opportunities, such as a “new dawn in the Middle East” and “trade that is free and fair for all,” along with new dangers such as climate change and transnational terrorism.

Nearly eight years later, the president sounded the same themes, but his tone was more sober and bore a greater recognition of the problems that Europe, which has had its confidence shaken, currently faces.

Throughout the speech, Obama drew parallels to the bitter and divisive politics in the United States. He described the nation’s battles over immigration and refugees, which have included calls from Republican presidential candidates to ban Syrian refugees who are Muslim from entering the United States.

“I know the politics of immigration and refugees is hard. It is hard everywhere, in every country,” he said.

Because of Europe’s proximity to the wars in the Middle East and the historic influx of refugees, the downsides and dangers of globalization have been felt more quickly and acutely on the continent, Obama said throughout his travels in the United Kingdom and Germany. He returned to the theme in his Hanover speech.

“These are unsettling times,” he said. “And when the future is uncertain, there seems to be an instinct in our human nature to withdraw to the perceived comfort and security of our own tribe, our own sect, our nationality. People who look like us, sound like us. In today’s world, more than any time in human history, that is a false comfort.”

Such thinking, Obama warned, could lead to “oppression,” “segregation,” “internment camps” — a powerful message in Germany, where memories of the horrors of Nazism and the Holocaust are a major part of modern Germany’s identity.

To meet such threats, Obama called on his European allies to carry on with the great European experiment and to continue to reach out to the rest of the world. He made the case for a massive and controversial trade deal — the Transatlantic Trade and Investment Partnership — that is being negotiated between the United States and the European Union.

He urged American and European governments to do more to address the problem of income inequality, which has become the dominant issue in this year’s Democratic presidential primary.

In an explicit rejection of the rhetoric that has dominated the presidential race in the United States — especially Sen. Bernie Sanders’s campaign — the president insisted that carefully negotiated free trade deals are the best means to boost economies and close the gap between the super-rich and everyone else.

“If you’re really concerned about inequality, if you are really concerned about the plight of workers, if you are a progressive, it is my firm belief that you can’t turn inward,” Obama said.

He defended the National Security Agency’s massive intelligence-collection efforts, which have alarmed privacy advocates in Europe and created a stir when it was revealed that the United States had been listening in on German Chancellor Angela Merkel’s phone calls. Obama ended that program, but he insisted that a robust collection program was essential to U.S. and European security.

“The threat of terrorism is real,” he said. “Security and privacy don’t have to be a contradiction.”

He called on European nations to maintain support for sanctions designed to punish Russia for its aggression in Ukraine. “We must not allow borders to be drawn by brute force in the 21st century,” he said.

His speech, though, seemed designed primarily to boost Europe’s flagging confidence and to counter anti-democratic and xenophobic forces that, 70 years after the end of World War II, are once again finding a foothold on the continent.

“People starved on this continent. Families were separated on this continent,” Obama said. Now refugees were risking their lives to come to Europe.

“People desperately want to come here precisely because of what you’ve created,” he said. “You can’t take that for granted.”