Yes this is legal.
Under the Civil Rights Act of 1964, there is an exemption under Title VII - Equal Employment Opportunity that allows for discrimination based on a protected trait when there is a Bona Fide Occupational Qualification (BFOQ). The wording is such that it can cover a slew of jobs that have just cause to discriminate against someone in their hiring process including certain roles for actors. However, it does not allow for wholesale discrimination.
In order to qualify, an employer must prove three things:
- A direct relationship between the trait and the ability to perform the job
- the BFOQ's relation to the "essence" or "central mission of the employer's business"
- That there is no less restrictive or reasonable alternative
As an example, a film about the work of Dr. Martin Luther King Jr. would have a BFOQ in hiring a man of African descent to portray Dr. King, given that he is trying to accurately portray a historical person (direct relationship) to further educate the public about that historical person's work (Central Mission) and that a "White MLK" would offend audiences.
While many roles in the hypothetical film would also qualify for the BFOQ, this doesn't give the production a blank slate... there is no need to discriminate against a set designer or a camera man, so a studio deciding to hire an all black production crew would face some legal troubles. The restaurant chain Hooters, famous for it's amazing chicken wings they served, has a sometimes noticed habit of hiring very attractive women as their wait staff, almost to the point of exclusively. All joking aside, they do get away with this by making a distinction that they are "casting" not "hiring" waitresses and their brand identity is entirely attractive ladies and not really the wings they serve. This is a BFOQ (and they often come up in discussions about this matter of law). With that in mind however, only Hooter's hiring of wait staff has a BFOQ. The person in the kitchen cooking the wings could be a middle-aged balding man with a noticeable gut and managers at stores can be men or women who aren't as attractive as the standards used for wait staff (or were former wait staff but are not as young looking).
Works adapting books and comics may have a little more leeway as certain characters may be portrayed as one race but have had their race switched for various reasons and with different success. No one batted an eye when Lawrence Fishburne portrayed Perry White, Clark Kent's editor in Man of Steel or Samuel L. Jackson as Nick Fury in the various Marvel films (an unusual example as traditionally white Nick Fury was depicted as an African American in Marvel's Ultimate Marvel line). His first appearance in August of 2001 in Ultimate Marvel Team Up, and a second appearance in Ultimate X-Men were of a non-descript African American, before his appearance as a regular in The Ultimates in March 2002, where his look was not only modeled after Jackson, Jackson was specifically "cast" as the visual model for the character, which he leapt at the idea (reportedly his initial pay was quite low given his standing as an actor with the difference made up for by a request of some exclusive artwork and an opportunity to play the character in a then not planned film.). There are even some fictional works where the change in race might be the point of the work itself (for example, in William Shakespeare's play Othello, the titular character is explicitly of North African descent and many consider the work a very early rebuke of concepts of racial superiority (it's very much the subject of debate as to whether Othello being black was an important concept to the play or the Bard just added it for something a little different)). During a 1997 Shakespeare Theater Company's 1997 adaptation, to hammer the "it's about race" side of the argument home, cast very much white Patrick Stewart to play the part of Othello, while the rest of the cast played by African American actors/actresses, a flip of the traditional depiction to highlight that Othello's race separates him from the rest of the characters in the play, regardless of what his actual race was. This play would have a BFOQ to discriminate against white actors seeking to play the villain Iago (IMO, one of the best villains ever written.) because it's important that Iago and Othello not share racial traits for the entire story to work.
While acting is usually where this comes up a lot, there are other places where it might be useful to discriminate against a protected class. For example, when hiring a pastor for a church, it would be very important to make sure the Pastor or other spiritual leader was not only of the correct faith, but correct sect within that faith to minister to the flock. A Seventh Day Adventist would be just as successful preaching to Catholic Church as a devout Muslim. For start, all three differ on which day is "the sabbath": Catholics say Sunday (the day of the week Jesus rose from the dead), Seventh Day Adventists say Saturday, the day that those of Jewish faith like Jesus would have observed as the Sabbath, and Muslims say it's Friday (I don't know why off the top of my head).
More practically, one might have a BFOQ for not hiring male therapists if one is working at a women's shelter (while gender has no bearing on being an effective therapist, many of the patients at a women's shelter might have problems discussing their situation with a man because most women's shelter's clients were abused by men in their lives, which might cause a delay in healing if the therapist working with them was also a man.).