To misquote Jurassic Park: you spend all that time wondering whether you can, you forget to ask whether you should.
Is it realistic?
In the realm of possibility.
Electronics manufacturers have forayed into biometric locks for a while now. There is no reason that couldn't be adapted on a gun.
Of course, the mechanisms you find commonly on phones and laptops have dubious degrees of reliability (at least my allegedly-smart phone does), so the technology most likely needs to mature more before it is reliable enough to be sold. But if it was a legal requirement, you can be certain the technology would catch-up.
The explosive charge itself would be more of a challenge. Many have noted there isn't much space to make a big enough boom to reliably kill someone. Still, you only need a charge big enough to send tiny pieces of metal flying. Even if it doesn't kill you, it's nothing to sneeze at and certainly not something you'd want to try your luck with. But if won't be nearly as cool as you envision it.
So while current technology may be lacking, it shouldn't be an unsurmontable obstacle.
Should it blow up?
I'd answer that with another question:
Are you out of your mind?
While there is some merit to disabling a gun, making it blow up is a very bad idea indeed.
Beyond the fact anybody can use the gun to blow you up if they're willing to blow up with it, or issues with false-negatives, I'm not sure that adding an explosive charge in a tool which uses explosions as its main propelling mechanism is a particularly sane idea to begin with. The worst case scenario for any malfunction (e.g. jam, misfire, overheating, current surge) now become "self-destruct charge triggers". You don't have to be a physics major to know this has bad news written all over it.
You also have to imagine that if there is any chance that it could be triggered remotely (e.g. an electromagnetic signal that induces current in the mechanism, or something funny like that), you know you positively don't want it. You should be less worried about a malicious actor exploiting such a weakness than finding it out when your troops start exploding when they microwave their dinner at the base.
In general, keep in mind that if you are ever considering putting a self-destruct mechanism in a device, you have to compare the probability and impact of the worst case scenario (i.e. the gun blowing up unprompted) vs the probability and impact of the device being intentionally misused (i.e. somebody stealing your gun). Here, it's very much more trouble than it can ever hope to be worth.
Should you even lock a gun?
No.
I said above there is some merit to it, and that's mostly for civilian uses. Actually, it's just for one case, to prevent kids from shooting themselves accidentally. I dare you to find a more useful and practical case for the technology. But that is of course 9000% negated if the gun blows up instead.
For law enforcement or military use however, it is supremely pointless. Assuming reasonable reliability, you cannot guarantee a false negative, which would be problematic in a shootout. Even if the gun doesn't explode, a gun that doesn't fire is the very definition of useless. And for all narrative purposes, this would obviously always happen at the worst possible time.
Even assuming perfect reliability, you have to ask why? If someone is in position to use your gun against you, there's a good chance either A) they have their own gun they can shoot you with, or B) they are in a position to beat you up with your biometrically-locked gun and that's arguably even more humiliating.
If a force can collect enough dead soldiers and unattended weapons, you also have to question whether they are really struggling for weapons to kill you with, or whether your troops are competent enough to win at all.
Ultimately, in your case, the only thing the lock prevents would be adding the insult of being shot by your own gun to the injury of being shot at all, which is of very little comfort. Even with an overblown budget it'll be a hard sell.