package net.petrikainulainen.springdata.jpa.web.security; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * This authentication entry point returns the HTTP status code 401. * @author Petri Kainulainen */ public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { private static final Logger LOGGER = LoggerFactory.getLogger(RestAuthenticationEntryPoint.class); @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { LOGGER.info("Authentication required. Returning HTTP status code 401."); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } }