The conservative majority on the Supreme Court seemed to agree Wednesday that President Trump has the authority to ban travelers from certain majority-Muslim countries if he thinks that it is necessary to protect the country.

Lower courts have struck down each of the three iterations of the president’s travel-ban proclamation, the first of which was issued just a week after he took office in January 2017. But the conservative-leaning Supreme Court may be Trump’s best hope, and it gave the administration a boost by allowing the ban to go into effect in December while considering the challenges to it.

Solicitor General Noel Francisco told the justices that the president was well within his power to issue the proclamation and that it came after a thorough, worldwide review of the vetting procedures of countries.

“The vast majority of the world, including the vast majority of the Muslim world, was just fine,” Francisco said, while a “tiny” number of countries were not.

If it were meant to be a Muslim ban, he said, “it would be the most ineffective Muslim ban that one could possibly imagine, since not only does it exclude the vast majority of the Muslim world, it also omits three Muslim-majority countries that were covered by past orders.”

Chief Justice John G. Roberts Jr. was most active in advancing the notion that the president is privy to national security information that courts are ill prepared to second-guess.

And Justice Anthony M. Kennedy, who always seems to occupy the pivotal position when conservative and liberal justices disagree, asked questions that mostly seemed supportive of the president’s authority.

It would seem almost impossible for challengers to prevail without one of those justices joining their colleagues on the left.

The high-profile hearing, the last of the court’s term as it turns now to writing opinions, called for the justices to balance their usual deference to the president on matters of national security with a never-before-seen barrage of campaign statements, tweets, retweets and comments from the president tying Muslims to terrorism.

Just hours after Francisco told the court the president had made it “crystal clear . . . he had no intention of imposing the Muslim ban,” White House press secretary Sarah Huckabee Sanders sidestepped a reporter’s question about whether the administration disavowed Trump’s campaign promise to enact one.

Sanders read a statement about the ban that said residents of many majority-Muslim countries could travel to the United States. “I think that alone in action answers your question clearly,” she said.

The court is considering the iteration of Trump’s travel ban, issued last fall, that barred various travelers from eight countries, six of them with Muslim majorities. They are Syria, Libya, Iran, Yemen, Chad, Somalia, North Korea and Venezuela. But restrictions on North Korea and Venezuela are not part of the challenge. Chad was removed from the list earlier this month.

The first questions for Francisco came from the two liberal justices, Ruth Bader Ginsburg and Sonia Sotomayor, who had noted their dissent from the high court’s earlier order to allow the ban to go into effect while challenges continued.

Sotomayor suggested that Congress had already taken steps to ensure national security by implementing a heightened vetting process for travelers from other countries.

“Where does a president get the authority to do more than Congress has already decided is adequate?” she asked.

Francisco pointed to the removal of Chad from the list as evidence that the review process was working as anticipated, by encouraging more cooperation from countries to help screen out those who might intend to harm the United States.

The challengers are led by the state of Hawaii, which said its citizens and educational institutions have suffered because of the ban.

Former Obama administration acting solicitor general Neal K. Katyal, representing Hawaii, said Trump had taken an “iron wrecking ball” to the law Congress had implemented to govern immigration and keep the nation safe.

“If you accept this order, you’re giving the president a power no president in 100 years has exercised, an executive proclamation that countermands Congress’s policy judgments,” Katyal said.

Conservative justices Samuel A. Alito Jr. and Neil M. Gorsuch, along with Roberts, peppered Katyal with questions on how the president had exceeded his lawful authority, given that Congress had granted the executive branch broad latitude to bar people’s entry into the United States.

Alito noted the law says that if the president found the entry of “any aliens” to be detrimental to U.S. interests, he could bar them. How, he asked, did the travel ban not fall “squarely” within that power?

Alito also said the ban affected only 8 percent of the world’s Muslim population.

“If you look at what was done, it does not look at all like a Muslim ban,” Alito said.

Roberts posited a hypothetical: If the intelligence agencies told the president that 20 Syrian nationals planned to enter the United States with biological weapons, could the president ban the entry of Syrians to stop them? Katyal conceded that he could, because in that instance — unlike this one — there was a true emergency.

Roberts asked whether there were a “statute of limitations” on a president’s campaign statements.

“Tomorrow, he issues a proclamation disavowing those statements, then the next day he could reenter this and your discrimination argument would not be applicable?” Roberts asked.

Katyal said that it probably would. But Trump and his administration have “rekindled” the comments.

Justice Elena Kagan similarly asked Francisco about a hypothetical anti-Semitic candidate for president who, once elected, put in place a proclamation blocking entry for citizens of Israel. She asked: Could the courts intervene in such a situation?

“This is an out-of-the-box kind of president in my hypothetical,” Kagan added, prompting laughter from the courtroom.

Francisco called it a “tough hypothetical” but said such a president could impose the measure if it came at the recommendation of staffers who had identified a genuine national security problem.

In contrast, Francisco said Trump’s travel ban is an “easy case” because it came after a multiagency review and on the advice of Cabinet officials. He conceded that if Cabinet officials knew the president was ordering a ban based on religious animus, because he told them as much, they would be “duty bound” to resign or refuse to comply with his order to come up with a justification.

“Is everything that the president said effectively that?” Kagan asked.

Kennedy picked up on Kagan’s questioning, asking Francisco to consider a local candidate for mayor who made “hateful’ comments and then upon being elected acted on those impulses. “You would say whatever he said in the campaign is irrelevant?” Kennedy asked.

Francisco said he would.

But Kennedy had more, and tougher, questions for Katyal.

When Katyal said Trump’s travel ban had no end, Kennedy said, “So you want the president to say, ‘I’m convinced that in six months we’re going to have a safe world?’ ”

And if there is a question about whether there is a threat to security, Kennedy asked Katyal, did he think “that’s for the courts to do, not the president?”

The justices are reviewing a unanimous ruling from a three-judge panel of the U.S. Court of Appeals for the 9th Circuit in San Francisco. That panel said the third version of the travel ban suffered from the deficiencies of the first two — that Trump had again exceeded his lawful authority and that he had not made a legally sufficient finding that entry of those blocked would be “detrimental to the interests of the United States.”

The U.S. Court of Appeals for the 4th Circuit in Richmond struck down the ban on the constitutional question. The 9-to-4 decision took a deep dive into Trump’s statements and tweets since he became president and concluded that the proclamation, like the first two, was motivated not by national security concerns but by antipathy toward Muslims.

The case is Trump v. Hawaii. The court will issue its decision some time before the conclusion of the term at the end of June.