WASHINGTON— After four years of President Donald J. Trump’s raging against his intelligence services, posting classified information to Twitter and announcing that he took the word of President Vladimir V. Putin of Russia over that of his own spies, perhaps the least surprising thing he did during his final days in office was ship boxes of sensitive material from the White House to his oceanside palace in Florida.
The F.B.I. search of Mar-a-Lago on Monday was a dramatic coda to years of tumult between Mr. Trump and American intelligence and law enforcement agencies. From Mr. Trump’s frequent rants against a “deep state” bent on undermining his presidency to his cavalier attitude toward highly classified information that he viewed as his personal property and would occasionally use to advance his political agenda, the relationship between the keepers of American secrets and the erratic president they served was the most poisoned of the modern era.
Mr. Trump’s behavior led to such mistrust within intelligence agencies that officials who gave him classified briefings occasionally erred on the side of withholding some sensitive details from him.
It has long been common practice for the C.I.A. not to provide presidents with some of the most sensitive information, such as the names of the agency’s human sources. But Douglas London, who served as a top C.I.A. counterterrorism official during the Trump administration, said that officials were even more cautious about what information they provided Mr. Trump because some saw the president himself as a security risk.
“We certainly took into account ‘what damage could he do if he blurts this out?’” said Mr. London, who wrote a book about his time in the agency called “The Recruiter.”
During an Oval Office meeting with top Russian officials just months into his presidency, Mr. Trump revealed highly classified information about an Islamic State plot that the government of Israel had provided to the United States, which put Israeli sources at risk and angered American intelligence officials. Months later, the C.I.A. decided to pull a highly placed Kremlin agent it had cultivated over years out of Moscow, in part out of concerns that the Trump White House was a leaky ship.
In August 2019, Mr. Trump received a briefing about an explosion at a space launch facility in Iran. He was so taken by a classified satellite photo of the explosion that he wanted to post it on Twitter immediately. Aides pushed back, saying that making the high resolution photo public could give adversaries insight into America’s sophisticated surveillance capabilities.
He posted the photo anyway, adding a message that the United States had no role in the explosion but wished Iran “best wishes and good luck” in discovering what caused it. As he told one American official about his decision: “I have declassification authority. I can do anything I want.”
Two years earlier, Mr. Trump used Twitter to defend himself against media reports that he had ended a C.I.A. program to arm Syrian rebels — effectively disclosing a classified program to what were then his more than 33 million Twitter followers.
If there is not one origin story that explains Mr. Trump’s antipathy toward spy agencies, the 2017 American intelligence assessment about the Kremlin’s efforts to sabotage the 2016 presidential election — and Russia’s preference for Mr. Trump — played perhaps the biggest role. Mr. Trump saw the document as an insult, written by his “deep state” enemies to challenge the legitimacy of his election and his presidency.
Mr. Trump’s efforts to undermine the assessment became a motif in the early years of his presidency, culminating in a July 2018 summit in Helsinki with Mr. Putin. During a joint news conference, Mr. Putin denied that Russia had any role in election sabotage, and Mr. Trump came to his defense. “They think it’s Russia,” Mr. Trump said, speaking of American intelligence officials and adding, “I don’t see any reason it would be.”
Mr. Trump often took aim at intelligence officials for public statements he thought undermined his foreign policy goals. In January 2019, top officials testified to Congress that the Islamic State remained a persistent threat, that North Korea would still pursue nuclear weapons and that Iran showed no signs of actively trying to build a bomb — essentially contradicting things the president had said publicly. Mr. Trump lashed out, saying on Twitter that “The Intelligence people seem to be extremely passive and naive when it comes to the dangers of Iran. They are wrong!”
“Perhaps Intelligence should go back to school!” he wrote.
Mr. Trump was hardly the first American president to view his own intelligence services as enemy territory. In 1973, Richard M. Nixon fired Richard Helms, his spy chief, after he refused to go along with the Watergate cover-up, and installed James Schlesinger in the job with the mission of bringing the C.I.A. in line.
Speaking with a group of senior analysts on his first day, Mr. Schlesinger made a lewd comment about what the C.I.A. had been doing to Mr. Nixon, and demanded that it stop.
Chris Whipple, an author who cites the Schlesinger anecdote in his book “The Spymasters,” said there is a long history of tension between presidents and their intelligence chiefs, but that “Trump really was in a league of his own in thinking the C.I.A. and the agencies were out to get him.”
The exact nature of the documents that Mr. Trump left the White House with remains a mystery, and some former officials said that Mr. Trump generally was not given paper copies of classified reports. This had less to do with security concerns than with the way Mr. Trump preferred to get his security briefings. Unlike some of his predecessors, who would read and digest voluminous intelligence reports each day, Mr. Trump generally received oral briefings.
But for those charged with protecting secrets, there may have been no bigger challenge than the seaside resort where Mr. Trump spent so much of his time as president — and where so many boxes of classified material were stored after he left office. Besides its members, Mar-a-Lago is also open to members’ guests, who would often interact with Mr. Trump during his frequent trips to the club. Security professionals saw this arrangement as ripe to be exploited by a foreign spy service eager for access to the epicenter of American power.
One night during his first weeks in office, Mr. Trump was at Mar-a-Lago hosting Shinzo Abe, the Japanese prime minister, when North Korea test-fired a ballistic missile in the direction of Japan that landed in the sea.
Almost immediately, at least one Mar-a-Lago patron posted photos on social media of Mr. Trump and Mr. Abe coordinating their response over dinner in the resort’s dining room. Photos showed White House aides huddled over their laptops and Mr. Trump speaking on his cellphone.
The patron also published a photo of himself standing next to a person he described as Mr. Trump’s military aide who carries the nuclear “football” — the briefcase that contains codes for launching nuclear weapons.
Just two world leaders responding to a major security crisis — live for the members of Mr. Trump’s resort to watch in real time.