A lot of it is not security related. If you go into most courtrooms wearing a hat the judge will tell you to take it off FAST.
That’s not security it’s a hold over from when all men wore hats and you removed them when you entered a building and when outside removed them when you were in the presence of someone who was substantially your better (Lord, Judge, etc). In the 50’s and earlier when you were going between clients in the same building you put on your hat so you could be seen removing it when you entered the new clients office. Strange but true. While women covered their hair when entering a church men removed their caps. Same idea.