package won.owner.security;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.ELRequestMatcher;
import org.springframework.security.web.util.RequestMatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* User: t.kozel
* Date: 11/8/13
*/
public class AjaxLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
private static final RequestMatcher requestMatcher = new ELRequestMatcher(
"hasHeader('X-Requested-With','XMLHttpRequest')");
public AjaxLoginUrlAuthenticationEntryPoint(final String loginFormUrl) {
super(loginFormUrl);
}
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException
authException)
throws IOException, ServletException {
if(requestMatcher.matches(request)) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
} else {
super.commence(request, response, authException);
}
}
}