A woman lights a candle near the site of the attack at the Christmas market at Breitscheidplatz in Berlin on Dec. 21. (Britta Pedersen/European Pressphoto Agency)

THROUGHOUT HISTORY, violent, authoritarian movements have tried to exploit the freedom, openness and diversity of democratic societies for undemocratic purposes, turning their most cherished strengths into potentially mortal vulnerabilities.

If any country understands this, it is Germany, where National Socialists first destabilized the Weimar Republic through demonstrations and propaganda and then took it over via the ballot box. During the Cold War, ultra-left-wing terrorist groups shook West Germany to its core — before effective law enforcement, their own blunders and, crucially, repudiation by mainstream citizens ultimately defeated them.

In the wake of Monday’s murderous attack on a Christmas market in Berlin, which left 12 innocent people dead, Germans, amid their necessary and justified mourning, must face the fact that they once again face such a threat. The threat is new and modern, in the sense that it apparently comes from the same Islamist extremist terrorist movement that has already claimed too many lives in the United States, Europe and around the world. The Islamic State took responsibility for the massacre, and the prime suspect is a Tunisian man whose application for asylum in Germany was recently rejected due to his alleged terrorism links. Yet this violent threat is fundamentally similar to those of the past, in that it can succeed only if it induces democracy to fail.

The question, then, for all Germans, starting with Chancellor Angela Merkel, is how to make sure today’s terrorists meet the same defeat that the Red Army Faction met during the 1970s. The first step, as Ms. Merkel had begun to recognize already in response to previous violent incidents, is to address the weaknesses of her open-door policy toward asylum seekers from the Middle East and elsewhere. This brought more than a million newcomers to the country last year amid abundant, almost giddy voluntary aid from many Germans — before the mood soured amid organizational chaos and well-publicized criminal acts by some newcomers.

Ms. Merkel’s response to the refugee crisis was admirable, but sustainability, especially political sustainability, of the policy must now be the objective. That means better, swifter screening of migrants, better integration of those who qualify to stay as refugees and the removal of those who lack an authentic asylum claim. Germany may need a more general attitude adjustment toward security; due to privacy concerns, for example, it has been slower than the United States and United Kingdom to adopt video surveillance of public spaces.

What Germans cannot and must not do is permit terrorists to sow internal division, much less succumb to the siren song of the anti-foreigner right wing, which has been gaining strength across Europe and moved immediately to exploit the attack ahead of the September 2017 national elections.

To be sure, striking these balances will be more difficult for Ms. Merkel and her people in a world where Donald Trump has ridden anti-foreigner demagoguery to the presidency of Germany’s most powerful ally. Unavoidably, though, the burden of proving that democratic values are not only consistent with society’s safety but also, in the long run, its best guarantee now belongs to the chancellor. She must bear it, no matter how lonely that job may sometimes feel.