When hundreds of thousands of migrants poured into Europe from the war-torn Middle East and beyond, Germany stepped in with shelter and aid. Yet a new wave of violence is exposing the extent to which Western Europe’s most populous nation has also opened the door to risk.

The latest attack: A rejected Syrian asylum seeker who pledged allegiance to the Islamic State, and was known by authorities to be suicidal, detonated a backpack bomb rigged with metal projectiles in the Bavarian city of Ansbach late Sunday. After being turned away from a pop music fest for not having a ticket, the 27-year-old exploded the bomb near a wine bar, killing himself and wounding 15 bystanders — including three left in serious condition.

It marked the fourth bloody assault on German soil in a week, stoking the fears of Germans who have largely looked on as terrorism struck their neighbors. A disturbed Iranian German teenager carried out the most deadly of the attacks — the mall shooting Friday that left 10 dead including the assailant. Yet the three others involved asylum seekers from Syria and Afghanistan — two of them self-described supporters of the Islamic State.

Though the vast majority of migrants are peaceful and in genuine need of sanctuary, the violence is sparking a national debate here. How, Germans wonder, did a country with relatively weak intelligence services allow in hundreds of thousands of asylum seekers without a firm plan for managing the potential consequences?

An ambulance waits at the scene of an explosion in Ansbach, Germany, where a Syrian asylum seeker blew himself up outside a pop music festival. (Daniel Karmann/European Pressphoto Agency)

“I am absolutely pro-Syrian [migrants], but the German government has underestimated the problem,” said Guido Steinberg , a terrorism expert at the German Institute for International and Security Affairs. “The [asylum seekers] are traumatized, they are brutalized, and they react in ways that have become totally alien to our country.”

In the wake of the attacks, officials are growing alarmed at the prospect that some traumatized asylum seekers are easily recruited or self-radicalized. Sunday’s attack came only days after a 17-year-old ax-wielding Afghan asylum seeker — a video of whom was released by the Islamic State — wounded five people in an attack begun on a Bavarian commuter train.

On Monday, Germany beefed up security at airports and train stations. Interior Minister Thomas de Maizière said authorities are now conducting 59 investigations into refugees suspected of possible links to terrorist organizations. But authorities, critics say, have failed to recognize that a small number of migrants, severely traumatized by war and strife, can react in unpredictable ways.

Also on Sunday, for instance, a Syrian refugee in the city of Reutlingen hacked to death a Polish woman, and left two others wounded, in what officials called a crime of passion.

Together, the string of attacks has focused attention on migrants in a way not seen since January, when a spate of sexual assaults during New Year’s Eve celebrations sparked a national outcry.

Politicians and pundits are calling for new measures to more quickly vet asylum seekers, deport those rejected and prevent radicalization inside refugee centers. They are also calling for more local police officers.

Among the migrants coming to Germany “are also persons who are a considerable danger for internal security,” Wolfgang Bosbach, a member of Parliament from Chancellor Angela Merkel’s Christian Democratic Union, told the N24 broadcaster on Monday.

Refugee advocacy groups warned, however, that innocent refugees are beginning to feel targeted. Activists called for more assistance to aid refugees affected by wars, warning that discord between the newcomers and German society would only delight the Islamic State.

“Because of their circumstances, not only in their home countries, but also while fleeing, many new arrivals are traumatized to a high degree,” said Karl Kopp, spokesman for the advocacy group Pro Asyl. “They need to be reached . . . and that’s expensive.”

Last month, Parliament passed a series of new security measures — a notable departure in a country with a strong aversion to heavy state security, given the Nazi and Cold War eras. It is now mandatory, for instance, to show identification when buying a prepaid cellphone card, and the federal police will be able to deploy undercover agents. It is also now easier for the German domestic intelligence service to collect and store data from teenagers as young as 14.

Yet Germany, experts say, is still facing a huge challenge. It is possible, for instance, to board a flight at Berlin’s Tegel Airport to destinations inside the European Union without ever showing a passport or identification card. The German intelligence services are far more reliant than the British or the French on local police work and tips from foreign agencies for counterterrorism operations.

Officials are also facing a backlog for processing asylum seekers, leaving them blind to the backgrounds of many recently arrived migrants. The deportation system, meanwhile, is rife with loopholes, and rejected migrants do not necessarily leave.

The Ansbach suicide bomber, for instance, was issued deportation orders in 2014, after German officials discovered that he had previously applied for, and received, asylum in Bulgaria. He was supposed to be deported back to Bulgaria but was permitted to remain after twice trying to kill himself. He received psychiatric treatment. Then, a second deportation order was issued on July 15 — nine days before the attack.

He left behind a video on his cellphone pledging allegiance to the leader of the Islamic State, according to Joachim Herrmann, the top security official in Bavaria.

Citing a provisional translation of the video in Arabic, Herrmann said the bomber “then announces an act of revenge against the Germans, because they are getting in the way of Islam.”

The Islamic State, according to Amaq, a news agency that supports the group, claimed responsibility for the attack. But the claim came only after the German announcement about the video, and the Islamic State did not offer any fresh details about the attacker, raising questions about whether the claim was real or perhaps opportunistic.

“As of now, it doesn’t seem that there was any coordination between ISIS and the bomber,” said Rita Katz, director of the SITE Intelligence Group, which tracks jihadist organizations.

Herrmann said German investigators have not yet found evidence that the suspect was in direct contact with the Islamic State, also known as ISIS, though he may have been inspired by it. Investigators were still seeking to determine whether he was driven by extremism, mental incapacity or a combination of both.

“Our security authorities are well organized, but there can never be absolute security,” de Maizière said Monday.