A bill to allow student clubs at public universities to reject would-be members who do not share the group’s political or religious mission is headed to Gov. Robert F. McDonnell’s desk.

The Senate approved a House bill Monday that supporters say protects freedom of association. The measure, backed by the Family Foundation of Virginia, asserts the right of clubs not to take “all comers.”

Opponents say the measure was intended to help student groups circumvent campus anti-discrimination policies, allowing them to discriminate against gay students and others.

A spokesman for McDonnell (R) declined to say whether he would sign the bill.

“We will review the legislation when we receive it,” said Tucker Martin, the spokesman.

The bill permits religious or political student organizations to exclude from membership anyone not “committed to the organization’s mission.” The bill also prohibits public universities from punishing clubs that have rejected potential members, prohibiting the schools from “discriminating against a student organization that makes such a determination.”

The Senate approved the measure 21 to 18, with three Democratic senators, George Barker of Fairfax, Chap Petersen of Fairfax and Phillip P. Puckett of Russell joining all but two Republicans to vote for it. Sen. Charles J. Colgan Sr. (D-Prince William) was present but did not vote. Two Republican senators, Thomas K. Norment Jr. of James City and John C. Watkins of Powhatan, voted against it.

The bill had broader bipartisan support in the House, where it passed 80 to 19.

“Under this bill, student groups benefiting from our taxpayer dollars can deny membership to students for any reason, from religion to sexual orientation,” said Sen. Adam Ebbin (D-Alexandria), the Senate’s only openly gay member.

Sen. Mark Obenshain (R-Harrisonburg), who sponsored an identical Senate bill now before the House, said in debate last week that the measure is “viewpoint neutral.”

Without it, he said, “A vegan group couldn’t require that its members be vegans,” said Obenshain, a candidate for attorney general. . . . “It will protect the vegans, it will protect Republicans, it will protect Democrats. It’s an equal-opportunity bill.”

(Editor’s note: This story has been updated to clarify that three Democratic senators supported the bill. The original post reported incorrectly that only one Democrat had voted for the legislation.)