I learned the following integration by parts method to prove Hardy inequalities from Luca Fanelli.
Assume that all functions are real-valued and define operators on $C^{\infty}_c(\mathbb R^d\setminus\{0\})$ by
$$
Su(x)=\frac{xu(x)}{|x|^2},\qquad Au(x)=\nabla u(x).$$
Notice that the sought inequality reads
$$
\int_{\mathbb R^d} \lvert Su(x)\rvert^2\,dx \le C\int_{\mathbb R^d} \lvert Au(x)\rvert^2\, dx.$$
So, for a $k\in \mathbb R$ yet to be determined, consider
$$\begin{split}
0&\le \int_{\mathbb R^d} \lvert Su+kAu\rvert^2\, dx\\&=\int_{\mathbb R^d} \lvert Su(x)\rvert^2\, dx +k^2\int_{\mathbb R^d} \lvert Au(x)\rvert^2\, dx+2k\int_{\mathbb R^d} Su(x)\cdot Au(x)\, dx.
\end{split}$$
The last summand is
$$
\int_{\mathbb R^d} Su\cdot Au\, dx=\int_{\mathbb R^d} \frac{x}{\lvert x\rvert^2}\cdot u\nabla u\, dx =\int_{\mathbb R^d} \frac{x}{\lvert x \rvert^2}\cdot \nabla \left(\frac{u^2}{2}\right)\, dx, $$
and since
$$\nabla \cdot \frac{x}{\lvert x\rvert^2}=\frac{d-2}{\lvert x \rvert^2}, $$
integration by parts reveals that
$$\tag{*}
\int_{\mathbb R^d} Su(x)\cdot Au(x)\, dx = -\frac{d-2}{2}\int_{\mathbb R^d} \lvert Su(x)\rvert^2\,dx.$$
Going back to the first inequality, we have that
$$
0\le k^2\int_{\mathbb R^d} \lvert Au(x)\rvert^2\, dx+(1-(d-2)k)\int_{\mathbb R^d} \lvert Su(x)\rvert^2\, dx, $$
which is a second-degree polynomial in $k$, and it attains its minimal value for $k=\frac{d-2}{2}\frac{\int \lvert Su\rvert^2}{\int \lvert Au\rvert^2}$. Plugging this into the last inequality finally yields
$$
\int_{\mathbb R^d} \lvert Su(x)\rvert^2\, dx \le \left( \frac{2}{d-2}\right)^2\int_{\mathbb R^d} \lvert Au(x)\rvert^2\, dx, $$
which is the result we wanted. $\Box$
Remark 1. The multiplicative constant $\left( \frac{2}{d-2}\right)^2$ turns out to be sharp, but proving this needs further work.
Remark 2. This method of proof is interesting because it has some robustness. Changing the definition of $S$ and $A$ may yield more inequalities, provided that some suitable replacement of the identity (*) holds. For example, taking
$$
Su(x)=\frac{u(x)}{\lvert x \rvert}, \qquad Au(x)=\frac{x}{\lvert x\rvert}\cdot \nabla u(x)$$
yields the following refinement of the Hardy inequality;
$$
\int_{\mathbb R^d} \frac{u^2(x)}{\lvert x \rvert^2}, dx \le \left( \frac{2}{d-2}\right)^2\int_{\mathbb R^d} \lvert \partial_r u(x)\rvert^2\, dx, $$
where $\partial_r$ stands for the radial derivative. This inequality is tighter than the previous one, because the right-hand side is smaller; indeed, here there are no angular derivatives.