Independent presidential candidate John B. Anderson was attacked twice here today by egg-throwing, slogan-shouting heckers.

A dark-haired heckler wearing a rust-colored corduroy jacket interrupted Anderson's speech to the National Governors Association, yelled at the candidate and threw two eggs that missed.

The second, uglier incident occurred as Anderson left the opening of his new Denver campaign headquarters in late afternoon.

He was shaking the hands of supporters outside the headquarters, located in an old renovated house in the Capitol Hill area, when a woman held up a baby for him to kiss.

Suddenly a shouting man lunged at him and threw an egg from a short distance. It splattered on the back of the Illinois congressman's dark blue suit as Secret Service men, their bodies huddled protectively over Anderson, rushed him to a car. As Denver police wrestled the egg-thrower to the ground, another man began shouting slogans and possibly threw a second egg.

Like the man who broke in on the speech, the two said they belonged to the Communist Workers Party, according to police. They were identified as Esmerejldado Guerrero, 26, of Greeley, Colo., and Carlos V. Romero, 27, of Albuquerque, N.M.

'They were shouting the same stuff as the guy was this morning," said Susan Wray, Anderson's state coordinator.

The morning incident began when a man strode unhindered toward the front of the room where most of the nation's govenors were gathered and shouted.

"Mr. Anderson, you represent World War II and fascism. Take that!"

His throw was off the mark, landing in a row of flags at Anderson's right. The heckler then reached into his coat pocket for another egg, shouting at North Carolina Gov. Jim Hunt, one of President Carter's staunchest supporters:

"Hunt, you killed the Communist Workers Party 5. Take that!" This egg splattered on the table in front of Hunt as the heckler went on:

"The Communist Workers Party 5 were killed by the government, killed by the FBI. And we are going to avenge the Communist Workers Party 5 . . . We're going to be at the Democratic convention."

This was a reference to party members fatally shot by a band of Nazis and Ku Klux Klan members at a "death to the Klan rally" in Greensboro, N.C., late last year. CWP members, complaining about the handling of the case and the arrest of four of their number on other charges, also disrupted a meeting of the Greensboro City Council and a Hunt press conference last week.

Hunt said, "Communists can't throw worth a damn."

It was that first time in Anderson's 14-month campaign as a Republican and then as an independent that he has been subject to such an attack.

He appeared unshaken. The governors gave him a prolonged standing ovation when he resumed his speech with a quip: "I think we all needed a break."

He received a second standing ovation from the governors, most of whom are unsympathetic, if not downright hostile to his independent candidacy, when he finished.

The assailant was wrestled into submission by Gray Davis, California Gov. Edmund G. Brown's top aide, and Ken Quinn, a Foreign Service officer on loan to Iowa Gov. Robert Ray. They thought he was about to draw a gun.

The attacker was identified as Jose Calderon, 32, of Greeley. He was charged with attempting to assault a federal candidate, a misdemeanor.

The incidents drew attention away from Anderson's primary mission here -- to woo support among governors, many of whom reported a fall-off of support for the independent candidate in their states.

Anderson met with Colorado Gov. Richard Lamm at the Democratic governor's mansion for breakfast, and later in his suite with Michigan gov. William Milliken, a Republican, and Arkansas Gov. Bill Clinton, a Democrat, the nation's youngest chief state executive.

Anderson, a Republican congressman from Illinois, told the press conference that these meetings were an attempt to bring "Democratics and Republicans as well as independents into my campaign." He also fueled speculation about his choice for a vice presidential running mate by offering effusive praise for Lamm and another Democrat, New York Gov. Hugh Carey, both of whom have called for President Carter to release his delegates from binding commitments so their party could have an "open convention."

Lamm, Anderson said, "obviously would bring a great deal to my campaign." Carey, he said, would have to "be included on any list" of vice presidential prospects.