Jos*_*osh 4 javascript ajax spring-security
我有一些用弹簧固定的URL(通过xml配置).有用.但是,当我尝试使用ajax请求命中该端点时,我得到302(找到)响应.这会将我的ajax调用重定向到登录页面(所以我获取了html).但是我希望得到一个401(未经授权的)响应,其中登录页面的URL可用于客户端应用程序,因此我可以使用javascript将用户重定向到那里. 这个问题似乎与我想要的最接近,但是没有例子,它建议再次更换控制器.spring-security中是否没有配置会给我401和url(或其他一些明智的错误消息和登录页面的URL)?
arp*_*pad 16
您可以扩展LoginUrlAuthenticationEntryPoint.这是我的一个:
package hu.progos.springutils;
// imports omitted
public class AjaxAwareLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
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(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
} else {
super.commence(request, response, authException);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后配置spring以使用您的实现:
<beans:bean id="authEntryPoint" class="hu.progos.springutils.AjaxAwareLoginUrlAuthenticationEntryPoint" scope="singleton>
<beans:property name="loginFormUrl" value="/login.html" />
</beans:bean>
<http entry-point-ref="authEntryPoint">
<!-- your settings here -->
</http>
Run Code Online (Sandbox Code Playgroud)