Content

Ajax登陆使用Spring Security缓存跳转到登陆前的链接(ajax实现登录)一篇读懂

文章摘要

这篇文章介绍了如何利用Spring Security缓存机制实现登录后跳转到登录前的链接。当用户访问网站时,Spring Security会缓存请求,使得在登录成功后能够直接跳转到登录前的链接。文章详细讲解了如何获取缓存、跳转到指定URL以及处理响应数据的步骤,包括前端JavaScript的处理逻辑。通过代码示例展示了从服务器到前端的完整流程,确保登录后用户能够直接访问登录前的资源。文章还提供了具体的Java代码和前端Ajax处理示例,帮助读者更好地理解和实现这一功能。



Spring Security缓存的应用之登陆后跳转到登录前源地址

什么意思?

用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源。 由于用户没有登录,服务器重定向到登录页面:/login 填写表单,点击登录 浏览器将用户名密码以表单形式发送给服务器 服务器验证用户名密码。成功,进入到下一步。否则要求用户重新认证(第三步) 服务器对用户拥有的权限(角色)判定。有权限,重定向到origin url; 权限不足,返回状态码403( “禁止”)。

从第3步,我们可以知道,用户的请求被中断了。

用户登录成功后(第7步),会被重定向到origin url,spring security通过使用缓存的请求,使得被中断的请求能够继续执行。

具体请看 探究Spring Security缓存请求

我这里仅讲解如何在ajax登陆后跳转到登录前的链接。

1. 首先,如果想跳转到登陆之前的链接,我们需要拿到缓存:

注意!若用户是直接访问没有权限限制的登陆页面,是不会有缓存的,也就是说savedRequest=null ,所以在使用缓存之前,我们需要做一个非null判断,也就是:

if (savedRequest !=null) { // 逻辑代码 }

2. 取到登录前访问的url

String url=savedRequest.getRedirectUrl();

3. 使用hashMap建立一个对象,这是为了后续向浏览器返回json数据

Map json=new HashMap<String, Object>(); json.put(“code”, 0); json.put(“message”, “操作成功”); json.put(“url”, url);

可以看到这个json 对象比较简单,其中url属性是为了让浏览器端的js跳转的

4.设置响应体编码和格式

5.向浏览器进行响应数据,这里的数据是json格式,是使用jackson工具包完成的,Maven地址: JacksonMaven地址

response.getWriter().write(mapper.writeValueAsString(ResponseBo.ok(messsage, url)));

下面是完整的Java代码:

@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())); } }

6. 前端页面Ajax代码:

$.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=https://www.jb51.net/article/data.url; } else { // 重置表单的输入框内容 $form[0].reset(); window.location.href=’https://www.jb51.net/index’; // $form.attr(“action”, ‘/index’); } } else { // if (r.msg !==’验证码不能为空!’) reloadCode(); console.log(data.message); } }, } );

7.不出意外的话,浏览器会收到下面的数据:

如果你也收到了这条数据,说明已经成功了。

总结

以上所述是小编给大家介绍的Ajax登陆使用Spring Security缓存跳转到登陆前的链接,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
您可能感兴趣的文章:Spring security实现登陆和权限角色控制基于Spring Security的Oauth2授权实现方法浅谈Spring Security 对于静态资源的拦截与放行SpringBoot2.0 整合 SpringSecurity 框架实现用户权限安全管理方法使用Spring Security OAuth2实现单点登录SpringBoot+Spring Security+JWT实现RESTful Api权限控制的方法spring security CSRF防护的示例代码Spring Security实现禁止用户重复登陆的配置原理