The folks over at DIY Easy Crafts have an entire section devoted to secret compartments you can build at home. One of the more involved compartments is this DIY floating shelf with a hidden gun safe inside. They have a complete breakdown with illustrations of what it takes to build your own version.
The shelf mounts normally on the wall, but it surrounded by an extra tall molding around the edge and a separate bottom panel. The entire molding and bottom are hinged so they can swing down to reveal storage for a long gun or multiple smaller items. This version doesn’t include an actual lock, but one could be added easily with a little planning.