In general, tourists in Spain aren't very likely to face any legal problems. The most important thing to keep yourself out of trouble is not to drink in public in those cities and areas where it's not allowed (like Barcelona).
As for drugs, in Spain, consumption or minor personal possession in public places is considered a (non-criminal) order offence, punishable by fines of 600 to 30,000 EUR. For minors, the fine can be suspended if the offender voluntarily attends treatment, rehabilitation or counselling activities. Cannabis in Spain is illegal for commercial purposes, but decriminalised for personal cultivation and use, and other purposes other than sale or trade.
And if you get caught on public transport without a valid ticket, expec to get a fine of 50 EUR on the spot.