How to make iPhone charge over USB cable when locked? iPhone 6s, running iOS 12.0.1

Plugging the phone into a computer to charge produces an "Unlock iPhone to use accessories" message and the phone refuses to charge.

How do I fix this nonsensical behavior? I know Apple added a privacy feature where the phone disables the data connection on lock to thwart hacktools that plug into the phone and brute-force the access code, but there's no reason to disable charging-only while it's locked. In this particular case, I only need to charge the phone over a computer; it doesn't need data access.

I'd like to avoid having to buy additional hardware such as a separate power-only limited USB cable.
Fixed.

Go into the security settings (Settings -> Touch ID and Passcode), and enable the "USB Accessories" switch (it should be green with the virtual switch-button to the right). This enables charging off any connection while locked.

Unfortunately this also disables the privacy/security feature that thwarts hacktools with physical access, so if you enable the USB Accessories option you will want to take extra precautions to prevent unauthorized physical device access and refrain from plugging into dodgy machines.