I've left uni ages ago and haven't had a History of Japan class since. The Amanto/aliens are clearly references to Americans, and more broadly, to the outside world/western world entering the tightly closed world that was Japan, but I can't pin point exactly which real life historical period the anti-foreigner war is supposed to reference: is it when Americans forced Japan out of the sakoku in the 1800s or after Americans installed a new government in Japan after WW2? I know that both events were resented by Japanese people, so the resentment alone present in Gintama makes it confusing.
I don't have a source on this but it definitely seems like it's inspired from America's occupation of Japan post WW2.