@SuppressWarnings("unchecked")
    public static <SM extends SecurityManager> SM
    unwrapSecurityManager(SecurityManager securityManager, Class<SM> type,
                          Predicate<Class<? extends SecurityManager>> predicate) {
        while (securityManager instanceof WrappedSecurityManager && !predicate.test(securityManager.getClass())) {
            WrappedSecurityManager wrappedSecurityManager = (WrappedSecurityManager) securityManager;
            securityManager = wrappedSecurityManager.unwrap();
            if (securityManager == wrappedSecurityManager) {
                throw new IllegalStateException("SecurityManager implementation of type [" + type.getName()
                        + "] is wrapped by itself, which is an invalid configuration.");
            }
        }
        return (SM) securityManager;
    }
Unwraps wrapped SecurityManagers if necessary.
@param securityManager the SecurityManager to unwrap
@param type the expected type of the SecurityManager
@param predicate to determine if the SecurityManager is of the expected type
@return the unwrapped SecurityManager
@param <SM> Type of the SecurityManager
core/src/main/java/org/apache/shiro/SecurityUtils.java