You can add a one-liner to redirect a non-logged-in user to anywhere in your website (usually home page though) inside a component, module, plugin, or template with the following code.

JFactory::getApplication()->redirect(JRoute::_(JURI::root().'index.php'), 'Please login first!');