Authored by Xu

用户登录后,默认定向Url为初始请求页面

  1 +package com.ui.model;
  2 +
  3 +/**
  4 + * 用户登录定向Url
  5 + * @author hui.xu
  6 + *
  7 + */
  8 +public class Directional {
  9 + private Directional() {
  10 +
  11 + }
  12 +
  13 + /**
  14 + * 类初始化时就已经实例化,以后就不会变化
  15 + */
  16 + private static final Directional singleton = new Directional();
  17 +
  18 + /**
  19 + * 静态工厂方法
  20 + *
  21 + * @return
  22 + */
  23 + public static Directional getInstance() {
  24 + return singleton;
  25 + }
  26 +
  27 + private String loginTargetUrl;
  28 +
  29 + public String getLoginTargetUrl() {
  30 + return loginTargetUrl;
  31 + }
  32 +
  33 + public void setLoginTargetUrl(String loginTargetUrl) {
  34 + this.loginTargetUrl = loginTargetUrl;
  35 + }
  36 +}
@@ -6,6 +6,7 @@ import com.ui.User.UserAuthLocal; @@ -6,6 +6,7 @@ import com.ui.User.UserAuthLocal;
6 import com.ui.contants.HttpUriContants; 6 import com.ui.contants.HttpUriContants;
7 import com.ui.http.HttpRestClient; 7 import com.ui.http.HttpRestClient;
8 import com.ui.model.BaseResponse; 8 import com.ui.model.BaseResponse;
  9 +import com.ui.model.Directional;
9 import com.ui.model.req.AuthModule; 10 import com.ui.model.req.AuthModule;
10 import com.ui.model.req.User; 11 import com.ui.model.req.User;
11 import org.apache.commons.lang.RandomStringUtils; 12 import org.apache.commons.lang.RandomStringUtils;
@@ -18,9 +19,13 @@ import org.springframework.stereotype.Controller; @@ -18,9 +19,13 @@ import org.springframework.stereotype.Controller;
18 import org.springframework.ui.Model; 19 import org.springframework.ui.Model;
19 import org.springframework.web.bind.annotation.RequestMapping; 20 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.ResponseBody; 21 import org.springframework.web.bind.annotation.ResponseBody;
  22 +import org.springframework.web.client.RestTemplate;
21 import org.springframework.web.servlet.ModelAndView; 23 import org.springframework.web.servlet.ModelAndView;
22 24
  25 +import javax.servlet.http.HttpServletResponse;
23 import javax.servlet.http.HttpSession; 26 import javax.servlet.http.HttpSession;
  27 +
  28 +import java.io.IOException;
24 import java.util.HashMap; 29 import java.util.HashMap;
25 import java.util.List; 30 import java.util.List;
26 import java.util.Map; 31 import java.util.Map;
@@ -32,7 +37,10 @@ public class UserCtrl { @@ -32,7 +37,10 @@ public class UserCtrl {
32 Logger log = LoggerFactory.getLogger(UserCtrl.class); 37 Logger log = LoggerFactory.getLogger(UserCtrl.class);
33 38
34 @Autowired 39 @Autowired
35 - HttpRestClient httpRestClient; 40 + private RestTemplate restTemplate;
  41 +
  42 + @Autowired
  43 + private HttpRestClient httpRestClient;
36 44
37 @Autowired 45 @Autowired
38 UserAuthLocal userAuthLocal; 46 UserAuthLocal userAuthLocal;
@@ -48,7 +56,7 @@ public class UserCtrl { @@ -48,7 +56,7 @@ public class UserCtrl {
48 } 56 }
49 57
50 @RequestMapping("/login") 58 @RequestMapping("/login")
51 - public ModelAndView toLogin(User user, HttpSession session, Model model) { 59 + public ModelAndView toLogin(User user, HttpSession session, Model model, HttpServletResponse response) {
52 if (StringUtils.isBlank(user.getName())){ 60 if (StringUtils.isBlank(user.getName())){
53 if (session.getAttribute("user") == null){ 61 if (session.getAttribute("user") == null){
54 return new ModelAndView("user/login"); 62 return new ModelAndView("user/login");
@@ -65,7 +73,17 @@ public class UserCtrl { @@ -65,7 +73,17 @@ public class UserCtrl {
65 } 73 }
66 if (u.getPwd().equals(MD5Util.encryption(user.getPwd()))) { 74 if (u.getPwd().equals(MD5Util.encryption(user.getPwd()))) {
67 session.setAttribute("user", u); 75 session.setAttribute("user", u);
68 - return new ModelAndView("dashBoard/dashBoard"); 76 + if(StringUtils.isBlank(Directional.getInstance().getLoginTargetUrl())){
  77 + return new ModelAndView("dashBoard/dashBoard");
  78 + }else{
  79 + try {
  80 + //直接跳转到初始的请求页面
  81 + response.sendRedirect(Directional.getInstance().getLoginTargetUrl());
  82 + } catch (IOException e) {
  83 + return new ModelAndView("dashBoard/dashBoard");
  84 + }
  85 + return null;
  86 + }
69 } else { 87 } else {
70 model.addAttribute("message", "密码错误"); 88 model.addAttribute("message", "密码错误");
71 return new ModelAndView("user/login"); 89 return new ModelAndView("user/login");
@@ -2,6 +2,7 @@ package com.ui.interceptor; @@ -2,6 +2,7 @@ package com.ui.interceptor;
2 2
3 import com.ui.User.UserAuthLocal; 3 import com.ui.User.UserAuthLocal;
4 import com.ui.User.UserOperateListener; 4 import com.ui.User.UserOperateListener;
  5 +import com.ui.model.Directional;
5 import com.ui.model.req.User; 6 import com.ui.model.req.User;
6 import org.apache.commons.lang.StringUtils; 7 import org.apache.commons.lang.StringUtils;
7 import org.slf4j.Logger; 8 import org.slf4j.Logger;
@@ -40,6 +41,8 @@ public class AuthInterceptor implements HandlerInterceptor { @@ -40,6 +41,8 @@ public class AuthInterceptor implements HandlerInterceptor {
40 return true; 41 return true;
41 } 42 }
42 }else{ 43 }else{
  44 + //设置定向Url
  45 + Directional.getInstance().setLoginTargetUrl(request.getRequestURI());
43 response.sendRedirect("/user/toLogin"); 46 response.sendRedirect("/user/toLogin");
44 return false; 47 return false;
45 } 48 }