A year after protesters in Hong Kong jubilantly defied Chinese rule, the national leader, Xi Jinping, has opened a long-term counteroffensive in the territory, signing a new security law on Tuesday that sets obedience to Beijing above the former British colony’s civil freedoms.

Conceived in secrecy and passed with intimidating speed, the law has ignited uncertainty about the future of Hong Kong even before any arrests under its sweeping new powers to quash political activity and speech that challenges Beijing. Chinese officials and policy advisers have described the security law as part of a “second return” for Hong Kong, one, they suggest, that will scrub away a dangerous residue of Western influence and liberal values.

By introducing such far-reaching legislation, Communist Party leaders in Beijing have faced down the pro-democracy movement in Hong Kong. They have also shrugged off opposition from the Trump administration and other governments, showing Mr. Xi’s determination to remake the territory on his authoritarian terms.

Some critics have described the law as a potentially fatal blow to the“One Country, Two Systems” political framework that preserved Hong Kong’s distinctive status, freedoms and laws after China resumed sovereignty in 1997. Even before taking effect, the law has already created a chill among once-defiant activists who defined the protest movement.

“Hong Kong people understand this means the end of the ‘One Country, Two Systems’ model for the territory, and we are now reduced to being a city like on the mainland, like Shenzhen or Shanghai,” said Joseph Cheng, a longtime political scientist at City University of Hong Kong. “We will have to behave like the people on the mainland.”

At the least, the new law complicates the delicate, often-convoluted game that Hong Kong officials and judges have played since China took back the territory. They have long tried to satisfy Beijing’s demands for loyalty while seeking to assure Hong Kong people that the territory’s legal system remained basically insulated from politics, guarding rights absent in mainland China.

That straddling act has become increasingly unsteady in recent years as China has applied growing pressure on the territory while protesters in Hong Kong have pressed back, demanding free elections and greater autonomy.

Credit…Lam Yik Fei for The New York Times

Now the security law — creating a murky realm of police agencies, crimes defined by Beijing and judges picked by Hong Kong’s pro-Beijing leader — is likely to make it harder to preserve the city’s nebulous status as a semiautonomous enclave under a Communist Party-run superpower.

“This will change Hong Kong. It’s a bridgehead,” said Danny Gittings, an expert on Hong Kong’s legal status. “It’s the most fundamental change since the handover. But that doesn’t mean that the changes will be immediately apparent.”

So far, many companies in Hong Kong appear confident that commerce and contracts will remain largely untouched by the law. Hong Kong officials have said that only a small number of people would be targeted by the rules, and the territory is likely to preserve some room for criticism of the Communist Party of the kind forbidden inside mainland China.

“The law will not affect Hong Kong’s renowned judicial independence,” Carrie Lam, the chief executive of Hong Kong, who serves with Beijing’s blessing, said in a video speech to the United Nations Human Rights Council on Tuesday. “It will not affect legitimate rights and freedoms of individuals.”

Still, the law may bite faster and sharper than some expect, including in education, where the party has warned against Western influence and dissenting ideas.

Hong Kong politicians loyal to Beijing and Chinese policy advisers have called for the rules to be enforced swiftly and vigorously, extinguishing any possible recurrence of the protests that hit Hong Kong last year.

Four leading members of Demosisto, a youth opposition organization at the forefront of protests last year, said on Tuesday that they had quit the group, citing risks from the law. Two pro-independence groups, Hong Kong National Front and Studentlocalism, said they would end activities in the territory.

The Standing Committee of the National People’s Congress, an elite arm of the party-controlled legislature, voted unanimously for the law a day before Hong Kong commemorates the 23rd anniversary of its handover to China. Mr. Xi then officially signed the law, leaving Hong Kong to take the final steps of putting the legislation into effect. The July 1 anniversary has usually been a day for big street protests in Hong Kong, which have been muted for months.

“If the new security law can succeed in doing what Beijing’s rhetoric anticipates, such protests will be a thing of the past,” said Suzanne Pepper, an independent political analyst who has long lived in Hong Kong. “Open political debate and dissent that Hong Kong has enjoyed for the past 20 years will fade into self-censorship.”

Credit…Lam Yik Fei for The New York Times

Under the law that defines Hong Kong’s special status in China, the territory’s authorities were supposed to create their own national security law. But successive leaders of Hong Kong never pushed the legislation through, and Chinese leaders have said they had no choice but to step in and impose a law.

The swift passage has reflected their fury at the pro-democracy protests that shook Hong Kong for months last year, as well as their shifting diagnosis of the causes of unrest.

During a recent seminar on the security legislation, Zhang Xiaoming, a top Chinese official who helps oversee Hong Kong policy, suggested that the territory’s basic problem was that its citizens had not been effectively immersed in party-blessed values, including acceptance that Hong Kong is an integral part of China, with Beijing setting the terms.

Chinese officials have said that discontent in Hong Kong had economic roots, and the priority cure lay in cheaper housing and better jobs.

“Hong Kong’s main problem is not an economic one,” Mr. Zhang said in the video speech in June. “It’s a political problem, and it’s focused on the fundamental question of what kind of Hong Kong we should build.”

Growing hostility between China and the United States has deepened Communist Party leaders’ worry about Western influence in Hong Kong.

Pro-Beijing politicians in Hong Kong and Chinese state media have described protests in the territory as the handiwork of Western intelligence operatives trying to topple the Communist Party. The new law bans “collusion” with foreign governments and groups deemed hostile to China.

Credit…Lam Yik Fei for The New York Times

“Beijing is particularly wary about Hong Kong being used by the U.S. and some of its Western allies as a pawn to contain China’s rise,” said Lau Siu-kai, a former senior Hong Kong government official who is now a prominent adviser to Beijing on Hong Kong policy.

The Trump administration did not even wait for Tuesday’s vote by Chinese lawmakers before taking steps. The administration on Monday extended to Hong Kong a ban it had long imposed on the sale to mainland China of advanced technology with potential military applications.

“Given Beijing now treats Hong Kong as ‘One Country, One System,’ so must we,” Secretary of State Mike Pompeo said in a statement.

A big test of the law lies in the Hong Kong courts, which have a long tradition of independent decisions. But the law is wired with provisions that appear designed to ward off attempts by courts and local lawmakers to hem in its powers. The Standing Committee of China’s National People’s Congress holds final power over how to interpret the rules.

“Beijing will be able to exert influence at every key stage of fortifying national security, both directly and indirectly through personnel accountable to it,” said Cora Chan, an associate professor of law at the University of Hong Kong who has studied China’s drive for security legislation. “When and how such jurisdiction will be exercised will be decided by Beijing, because it has final powers of interpreting the security law.”

Still, Hong Kong may not see a deluge of prosecutions under the new law. In mainland China, the police and prosecutors charge people under state security crimes relatively rarely, often preferring to imprison dissidents and other political foes under other, less prominent charges, such as fraud or stirring up trouble.

In the decade leading up to 2016, the last year for which detailed statistics are available, Chinese courts finished up cases on state security charges against 8,640 defendants, according the Dui Hua Foundation, a group based in San Francisco that monitors human rights in China.

The great majority of the defendants in these mainland Chinese security trials were members of ethnic minorities, mostly Uighurs and Tibetans, convicted of promoting ethnic separatism, a broad charge that can be used against anyone who questions Chinese rule, a forthcoming report from the foundation shows.

Credit…Kevin Frayer/Getty Images

Macau, a former Portuguese colony nearby that like Hong Kong is now a special administrative region of China, adopted a somewhat similar national security law 11 years ago but has yet to prosecute anyone under it.

The Hong Kong authorities may pursue a few state security prosecutions to serve as a lesson for the public, Tian Feilong, an associate professor of law at Beihang University in Beijing who specializes in Hong Kong affairs, said in an interview.

“Any law, especially any punitive law, is aimed at a minority in order to educate the majority,” he said. “Hong Kong society will have to make big adjustments in political and cultural life as this law takes effect.”