Ответ 1
Использовать пользовательский AuthenticationEntryPoint:
package com.example.spring.security
// imports here
public class AjaxAwareAuthenticationEntryPoint
extends LoginUrlAuthenticationEntryPoint {
public AjaxAwareAuthenticationEntryPoint(final String loginFormUrl) {
super(loginFormUrl);
}
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException)
throws IOException, ServletException {
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
response.sendError(403, "Forbidden");
} else {
super.commence(request, response, authException);
}
}
}
Определите bean и используйте его как entry-point-ref
в <http>
element:
<http entry-point-ref="authenticationEntryPoint">
<!-- more configuration here -->
</http>
<bean id="authenticationEntryPoint"
class="com.example.spring.security.AjaxAwareAuthenticationEntryPoint">
<constructor-arg value="/login.jsp"/>
</bean>