The city allowed Colin Kaepernick to make a surprise visit to Rikers Island on Tuesday to meet with inmates — enraging correction officers who feel the stunt will only endanger them.

The former San Francisco 49ers star spoke to groups of inmates during two sessions Tuesday morning at the jail’s George Motchan Detention Center, where he pontificated on social justice issues and talked about his decision to kneel during the national anthem, which sparked nationwide outrage.

“That’s crazy to me to have a person like Colin Kaepernick in prison talking about police brutality,” said an officer who attended the event. “It was insulting for me to be there.”

“In the inmates’ eyes, we are the police when they’re locked up.”

The morning started off with Kaepernick attending breakfast in the warden’s office before heading over to the “Peace Center,” where he conducted two 45-minute sessions with prisoners clad in gray jumpsuits.

The first group was 14 inmates — six adults and eight adolescents.

A volunteer at the jail introduced Kapernick, who gave a 10-minute talk before fielding questions and signing autographs.

“They were basic questions, like what’s it like to play in the NFL,” said the source.

“Then they asked him about taking a knee, why was he doing it. He said he was doing it to call attention to police brutality.”

“He said he felt that, being a man of means, he felt obligated to take a stance on what he believes in.”

In the second session, which consisted of four adults and 10 adolescents, Kaepernick was more direct on his feelings about police brutality, the source said.

“He came out of the gate with the police brutality … and he said the NFL was keeping him out of the game for speaking out.”

The source described the atmosphere in the room as calm and added that Kaepernick did not say anything derogatory about police or corrections officers.

“He was telling them how they could do better in life, that they’ve made mistakes, that this wasn’t the end for them, that they could go on to do good things,” he said.

The move drew the ire of corrections union officials, who said the cop-hating athlete’s visit will only serve to endanger their members by emboldening criminals who see themselves as victims of a corrupt criminal justice system.

“The inmates see a guy like this coming in, it’s almost like the administration is condoning being anti-law enforcement,” said Patrick Ferraiuolo, president of the Correction Captains’ Association. “His presence alone could incite these guys.”

Rikers Island has seen several instances of violence perpetrated against guards at the hands of inmates in recent years.

“We’ve got enough issues in the facility with inmates assaulting staff,” Ferraiuolo said. “His presence, what he stands for, certainly doesn’t help.”

Peter Thorne, a Department of Correction spokesman, said Kaepernick visited the jail to “share a message of hope and inspiration.”

In addition to speaking with inmates, Kaepernick visited with uniformed and non-uniformed Rikers employees, Thorne said.

“This will only encourage inmates to continue to attack correction officers at a time when we need more protection,” said Elias Husamudeen, president of the Correction Officers Benevolent Association.

Husamudeen pointed out that Kaepernick wore socks depicting cops as pigs while playing in a 2016 NFL game.

Husamudeen laid blame for the ill-conceived move squarely at the feet of the mayor.

“Once again, correction officers find themselves caught in Mayor de Blasio’s political con game,” Husamudeen said.

“This is yet another brazen display of the hypocrisy of this mayor, who pretends to support us in public, yet does everything possible to jeopardize our security in private.”

City Hall spokesman Eric Phillips defended the visit, saying: “This is a guy using his celebrity to help young adults turn their lives around, to his own significant professional cost. He should be applauded.”