The Talmud Bavli Brachot (3b) says that when Jews needed financial assistance and the economy did not have enough money to go around, King David would advise the sages of Israel to confiscate money from (what is commonly translated as) bandits or troops -- "לכו ופשטו ידיכם בגדוד". Then, the leaders would create a military strategy and attack the גדוד (see the page for the process).
What is the legal justification for this measure?
Who were these bandits/troops? If they were dangerous and the king always had a right to do this, then why were גדוד hanging around Israel, and why did King David only confront them when the people needed money? If the גדוד was not a threat, then why would the King decide to hurt and rob other people for the sake of financial gain?