how to add a custom lock for the recovery and/or bootloader? Unlocking the bootloader and/or the recovery is very popular discussion across many devices. But doing so exposes a very obvious and inevitable issue with security!

An unlocked recovery means that no matter how secure you make your android lock screen, someone can easily reset phone to restore the phone to a fully functional (and lockless/insecure) state and the worst part, they don't have to erase your data(meaning they can access all your personal files and folders)!

It is almost hilarious! In fact if the phone supports insertion of external SD cards, you can even flash your own software into the phone and maybe bypass the lock screen without wiping any data.

For example: One possible loophole is using the 'Tasker' app which has a feature that can temporarily disable the keyguard. And the best part is that Tasker can be flashed using the recovery!

So is there any method by which I could add a security measure that prevents complete access to my recovery and/or boot loader that will make my phone less insecure?!

Expected answer:
Method by which a custom pin/password must be entered before entering the bootloader/recovery. So that my bootloader/recovery is still technically unlocked but simply requires authentication to use (this also ensures that conventional unlocking methods won't work as the phone is not really locked!). This also means that one does not have to lose root access to ensure security!
Encrypt your filesystem. You'll need to enter a password or PIN on every boot to decrypt it, so as soon as the device is rebooted none of your personal data will be readable. TWRP even supports encrypted filesystems (it prompts you for your password when it starts as well, I think). If your device is at least 4.2.2 then you can even leave ADB on; they'd need to unlock the screen to add their computer to the whitelist, so they can't push apps or read data there either. You'll be able to have root, a custom bootloader, ADB enabled, and still have a relatively secure device.