[2.7.x] NEXUS-6213: Backported fix to 2.7.x

Squashed commits:
3f04733b41b5e759d056760f6634f3ce46fe6872
8be363a440ff20a5aa3a2682a67b0245d935833e
19f85dc6261c82f9fdf78b9ccfded7c32b17926f
d6846c3eed0cd0f21865c9bf9ca575d788e7b97f

Original PR
https://github.com/sonatype/nexus-oss/pull/313

CI
https://bamboo.zion.sonatype.com/browse/NX-OSSF18