通过 WebUtils.getSavedRequest(request) 来获取shiro保存在session登录之前的url
1:java Controller代码
1 @PostMapping("/login") 2 @ResponseBody 3 public R login(HttpServletRequest request, 4 String username, String password, String code) { 5 if (!StringUtils.isNotBlank(code)){ 6 return R.warn("请输入验证码!"); 7 } 8 if (!"VBK8".equalsIgnoreCase(code)){ 9 return R.warn("验证码输入错误,请重新输入验证码!"); 10 } 11 password = MD5Utils.encrypt(username, password); 12 UsernamePasswordToken token = new UsernamePasswordToken(username, password); 13 try { 14 super.login(token); 15 this.userService.updateLoginTime(username); 16 //登录成功之后跳转到原url 17 SavedRequest savedRequest = WebUtils.getSavedRequest(request); 18 String url = "/"; 19 if(savedRequest != null){ 20 url = savedRequest.getRequestUrl(); 21 } 22 return R.ok(url); 23 } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) { 24 return R.error(e.getMessage()); 25 } catch (AuthenticationException e) { 26 return R.error("认证失败!"); 27 } 28 }
js代码:
1 function login(){ 2 var username = $("#username").val(); 3 var password = $("#password").val(); 4 // var code = $("#code").val(); 5 var code = 'vbk8'; 6 7 if(username.trim().length < 5 || 8 password.trim().length < 5 || 9 code.trim().length != 4){ 10 layer.msg('输入非法,请重新输入', {icon: 2}); 11 return 12 } 13 14 $.ajax({ 15 type: "post", 16 url: ctx + "login", 17 data: { 18 "username": username, 19 "password": password, 20 "code": code, 21 }, 22 dataType: "json", 23 success: function (r) { 24 if (r.code === 0) { 25 if(r.msg.length != 0){ 26 location.href = r.msg; 27 }else{ 28 location.href = ctx; 29 } 30 } else { 31 layer.msg(r.msg, {icon: 2}); 32 } 33 } 34 }); 35 }