标签:message win http index cat adc 用户登录 app 登录
从第3步,我们可以知道,用户的请求被中断了。
用户登录成功后(第7步),会被重定向到origin url,spring security通过使用缓存的请求,使得被中断的请求能够继续执行。
具体请看探究Spring Security缓存请求
SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest != null) {
// 逻辑代码
}
String url = savedRequest.getRedirectUrl();
Map json = new HashMap<String, Object>();
json.put("code", 0);
json.put("message", "操作成功");
json.put("url", url);
response.setContentType(FebsConstant.JSON_UTF8);
response.getWriter().write(mapper.writeValueAsString(ResponseBo.ok(messsage, url)));
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 不管请求哪个页面,登陆成功后仅打开指定页面index
// redirectStrategy.sendRedirect(request, response, "/index");
// 获取缓存
SavedRequest savedRequest = requestCache.getRequest(request, response);
// 设置响应格式和编码
response.setContentType(FebsConstant.JSON_UTF8);
// 缓存非空判断
if (savedRequest != null) {
// 跳转到之前引发跳转的url
String url = savedRequest.getRedirectUrl();
String messsage = "成功";
// 准备json
Map json = new HashMap<String, Object>();
json.put("code", 0);
json.put("message", "操作成功");
json.put("url", url);
Object jsons = ResponseBo.ok(messsage, url);
response.getWriter().write(mapper.writeValueAsString(ResponseBo.ok(messsage, url)));
} else {
// 这个是没有缓存,直接跳转到默认ajax默认的页面
response.getWriter().write(mapper.writeValueAsString(ResponseBo.ok()));
}
}
$.ajax(
{
type: "post",
url: "/login",
// 登陆表单数据序列化
data: $form.serialize(),
dataType: "json",
error: function (data, type, err) {
if (data.responseJSON != undefined) {
console.log(data.responseJSON.error != undefined);
console.log(JSON.stringify(data.responseJSON.error));
$MB.n_danger("error:" + JSON.stringify(data.responseJSON.error));
}
},
success: function (data) {
console.log(JSON.stringify(data));
alert(JSON.stringify(data));
if (data.code == 0) {
// 如果有url,则跳转该url
if (data.url != undefined) {
$form[0].reset();
window.location.href = data.url;
} else {
// 重置表单的输入框内容
$form[0].reset();
window.location.href = ‘/index‘;
// $form.attr("action", ‘/index‘);
}
} else {
// if (r.msg !== ‘验证码不能为空!‘) reloadCode();
console.log(data.message);
}
},
}
);
{"code":0,"message":"操作成功"}
Ajax登陆,使用Spring Security缓存跳转到登陆前的链接
标签:message win http index cat adc 用户登录 app 登录
原文地址:https://www.cnblogs.com/gobyte/p/10754273.html