SEOUL, South Korea — Midway through his five-year term, President Moon Jae-in of South Korea appears more embattled than ever. His policy of improving ties with North Korea has stalled. His economy has slowed, and antigovernment protests in Seoul have grown.

Amid such setbacks, it might seem unwise for Mr. Moon to take on another huge external challenge: a trade war with Japan — his country’s third-largest trading partner and colonial ruler from 1910 to 1945 — that has stirred up historical animosities.

But Mr. Moon, who punched back against Japan’s trade restrictions and shocked American diplomats last Thursday by ending an intelligence-sharing deal with Tokyo, is borrowing from the time-honored playbook in South Korean politics: that it often pays to act tough against Japan.

With his presidency struggling, Mr. Moon is moving to rally his supporters by tapping into hostility toward Japan, refusing to back down in the trade fight and deploying the country’s military to assert its territorial claims, analysts say.