Q:
How to prevent accidentally targeting ally?
This would be a nice feature. But sadly no.
One workaround is to tab fast to the mob you want to target, then hit CTRL-T to put a target circle on it (this will appear for you and anyone else in your party/squad group). Then if you lose your target you can tap T to reacquire it.