@SuppressWarnings({"unchecked"})
    protected SubjectContext ensureSecurityManager(SubjectContext context) {
        if (context.resolveSecurityManager() != null) {
            LOGGER.trace("Context already contains a SecurityManager instance.  Returning.");
            return context;
        }
        LOGGER.trace("No SecurityManager found in context.  Adding self reference.");
        context.setSecurityManager(this);
        return context;
    }
Determines if there is a {@code SecurityManager} instance in the context, and if not, adds 'this' to the
context.  This ensures the SubjectFactory instance will have access to a SecurityManager during Subject
construction if necessary.

@param context the subject context data that may contain a SecurityManager instance.
@return The SubjectContext to use to pass to a {@link SubjectFactory} for subject creation.
@since 1.0
core/src/main/java/org/apache/shiro/mgt/DefaultSecurityManager.java