We formulate an analytic method which accounts for the finite size of the nucleus by treating it as a boundary value problem. The method is used to obtain solutions of the Dirac equation for a central potential that is proportional to 1/r only for values of the radial coordinate greater than a given value R. Our results are applied to a non-perturbative calculation of the nuclear size corrections to the energy levels of single-electron and single-muon atoms. For values of the nuclear charge number Z greater than 40 in the case of electronic atoms, and greater than 1 in the case of muonic atoms, we find large discrepancies between our results for the atomic energy levels and those obtained from first-order relativistic perturbation theory.