Well, Thoreau believed that if a law is unjust, it's our duty to disobey it, even if it means getting in trouble. He thought that by doing so, we can bring attention to the problem and maybe even change the law. It's kind of like if you saw someone being bullied, and you decided to stand up for them, even if it means going against the crowd.
Thoreau was inspired by his own experiences, like when he refused to pay taxes to support the Mexican-American War, which he thought was unfair. He ended up in jail for a night, but it didn't stop him from speaking out. In fact, it made him even more determined to share his ideas with the world. He wrote an essay called Civil Disobedience, which has become a classic and is still widely read today.