Jquery 使用spring安全性和ajax登录处理无效用户/密码
我使用的是spring安全登录+ajax登录。如果用户提供了错误的凭证,我需要显示一条消息,说明“无效的用户/密码” 这是ajax登录jquery代码Jquery 使用spring安全性和ajax登录处理无效用户/密码,jquery,spring-security,Jquery,Spring Security,我使用的是spring安全登录+ajax登录。如果用户提供了错误的凭证,我需要显示一条消息,说明“无效的用户/密码” 这是ajax登录jquery代码 $(document).ready(function(){ $('#login_btn').click(function(){ //alert("aaa"+validateLoginForm()); if(validateLoginForm()){ sprpst = 'j_username='+$('#j_use
$(document).ready(function(){
$('#login_btn').click(function(){
//alert("aaa"+validateLoginForm());
if(validateLoginForm()){
sprpst = 'j_username='+$('#j_username').val()+'&j_password='+$('#j_password').val();
$.post('j_spring_security_check',sprpst,function(data1) {
window.location.reload();
});
$.blockUI({ message : '<h1><img src="images/busy.gif" /> Loading...</h1>' });
}
});
});
$(文档).ready(函数(){
$('#login_btn')。单击(函数(){
//警报(“aaa”+validateLoginForm());
if(validateLoginForm()){
sprpst='j#u username='+$('j#u username').val()+'&j#u password='+$('j#u password').val();
$.post('j_spring_security_check',sprpst,函数(数据1){
window.location.reload();
});
$.blockUI({消息:'正在加载…});
}
});
});
这是spring安全登录
<form-login login-page="/login" default-target-url="/deals" authentication-failure-url="/deals?login_error=1"/>
任何帮助都将不胜感激
谢谢:)您需要:
AuthenticationEntryPoint.start(…)
方法中,检查这是否是AJAX调用,并返回403代码,而不是重定向到登录页面public class CustomAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
String header = request.getHeader("X-Requested-With");
if (StringUtils.hasText(header) && header.equals("XMLHttpRequest") && authException != null) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} else {
super.commence(request, response, authException);
}
}
}
您能否提供自定义AuthenticationEntryPoint的链接/示例代码?我不确定,您是否也需要将此代码放入自定义AuthenticationFailureHandler中。