In a nutshell yes, but only if the harbour authority considers there is greater danger in entering.
I have had to enter a private harbour as a yacht in distress (broken steering) they weren't happy but had to allow us entry.
On the Portuguese coasts subject to Atlantic swells ports are often closed, trouble is you are advised to contact them by radio before entry but I know of at least one incident where there was no clear English information available to an English skipper. He entered and encountered dangerous breaking seas in the entrance channel (the swell was in excess of 10m at sea) he made it through and was greeted by the police and arrested. For some time he was given the impression the boat would be impounded.