ÁGUILAS, Spain — Six years ago, Justo Cruz Rodríguez, who runs a small business here designing signs, was looking for a way to generate a steady, if modest, pension for himself and his father.

So when the government passed a law offering attractive rates for solar energy — and guaranteed them for the next 25 years — he mortgaged his house, his father’s house and even his workshop to install half a dozen rows of solar panels in his father’s garden, with the idea of selling his excess electricity.

“It seemed so safe,” he said recently. “It was a government guarantee.”

But the Spanish government has changed its mind. It plans to pay less, a lot less. Under legislation that goes into effect this year, it will drop its per-kilowatt-hour payment system altogether and effectively impose retroactive cuts in payments. It also plans to make solar power producers pay a charge on electricity they generate and use themselves, a measure that angry protesters have named the “sun tax.”

Spain has good reason for wanting to take action. It is facing a growing deficit — about $40 billion now — because it has never passed on the true cost of producing energy to its consumers, a problem that has ballooned with the economic crisis. If it does not do something, that deficit will only grow, experts say.