### Background

Recall that, given two commutative rings $A$ and $B$, the set of morphisms of rings $A\to B$ is in bijection with the set of morphisms of schemes $\mathrm{Spec}(B)\to\mathrm{Spec}(A)$. Furthermore, we know that Spec$(A)$ has a base of open affine subsets, the "basic" or "principal" open affines $D(f)$ for all $f\in A$. Furthermore, $D(f)\cong \mathrm{Spec}(A_f)$ as schemes, and the inclusion $D(f)\hookrightarrow\mathrm{Spec}(A)$ corresponds to the localization map $A\to A_f$.

But answers to a recent MathOverflow question show that open affine subschemes of affine schemes can arise in other ways.

### Question

In order to try to make sense of the situation above, I'd like to know the following.

Given a commutative ring $A$, is there a "ring-theoretic" characterization of the ring homomorphisms $A\to B$ that realize $\mathrm{Spec}(B)$ as an open affine subscheme of $\mathrm{Spec}(A)$ (more precisely, those morphisms such that the induced map $\mathrm{Spec}(B)\to\mathrm{Spec}(A)$ is an open immersion)?

Of course, "ring-theoretic" is a bit vague. Let's certainly avoid any tautological characterizations. I would prefer if an answer didn't make any reference to the Zariski topology (for instance, the morphisms $A\to A_f$ make perfect sense without the Zariski topology), but I'm not sure whether that's reasonable.

**Update:** I received two great answers, thank you both! I chose the one that was closer to the kind of condition that I had in mind. But Dan Petersen's answer was also very interesting and unexpected.