com.auth0.spring.security.mvc.Auth0AuthenticationEntryPoint Maven / Gradle / Ivy
package com.auth0.spring.security.mvc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.ExceptionTranslationFilter;
/**
* Used by {@link ExceptionTranslationFilter} to commence an authentication scheme.
*/
public class Auth0AuthenticationEntryPoint implements AuthenticationEntryPoint {
/**
* Commences an authentication scheme.
*
* ExceptionTranslationFilter
will populate the HttpSession
* attribute named
* AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY
* with the requested target URL before calling this method.
*
* Implementations should modify the headers on the ServletResponse
as
* necessary to commence the authentication process.
*
* @param request that resulted in an AuthenticationException
* @param response so that the user agent can begin authentication
* @param authException that caused the invocation
*/
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
final PrintWriter writer = response.getWriter();
if (isPreflight(request)) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else if (authException instanceof Auth0TokenException) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage());
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, authException.getMessage());
writer.println("HTTP Status " + HttpServletResponse.SC_FORBIDDEN + " - " + authException.getMessage());
}
}
/**
* Checks if this is a X-domain pre-flight request.
*/
private boolean isPreflight(final HttpServletRequest request) {
return "OPTIONS".equals(request.getMethod());
}
}