Authored by Xu

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

package com.ui.model;
/**
* 用户登录定向Url
* @author hui.xu
*
*/
public class Directional {
private Directional() {
}
/**
* 类初始化时就已经实例化,以后就不会变化
*/
private static final Directional singleton = new Directional();
/**
* 静态工厂方法
*
* @return
*/
public static Directional getInstance() {
return singleton;
}
private String loginTargetUrl;
public String getLoginTargetUrl() {
return loginTargetUrl;
}
public void setLoginTargetUrl(String loginTargetUrl) {
this.loginTargetUrl = loginTargetUrl;
}
}
\ No newline at end of file
... ...
... ... @@ -6,6 +6,7 @@ import com.ui.User.UserAuthLocal;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.Directional;
import com.ui.model.req.AuthModule;
import com.ui.model.req.User;
import org.apache.commons.lang.RandomStringUtils;
... ... @@ -18,9 +19,13 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
... ... @@ -32,7 +37,10 @@ public class UserCtrl {
Logger log = LoggerFactory.getLogger(UserCtrl.class);
@Autowired
HttpRestClient httpRestClient;
private RestTemplate restTemplate;
@Autowired
private HttpRestClient httpRestClient;
@Autowired
UserAuthLocal userAuthLocal;
... ... @@ -48,7 +56,7 @@ public class UserCtrl {
}
@RequestMapping("/login")
public ModelAndView toLogin(User user, HttpSession session, Model model) {
public ModelAndView toLogin(User user, HttpSession session, Model model, HttpServletResponse response) {
if (StringUtils.isBlank(user.getName())){
if (session.getAttribute("user") == null){
return new ModelAndView("user/login");
... ... @@ -65,7 +73,17 @@ public class UserCtrl {
}
if (u.getPwd().equals(MD5Util.encryption(user.getPwd()))) {
session.setAttribute("user", u);
return new ModelAndView("dashBoard/dashBoard");
if(StringUtils.isBlank(Directional.getInstance().getLoginTargetUrl())){
return new ModelAndView("dashBoard/dashBoard");
}else{
try {
//直接跳转到初始的请求页面
response.sendRedirect(Directional.getInstance().getLoginTargetUrl());
} catch (IOException e) {
return new ModelAndView("dashBoard/dashBoard");
}
return null;
}
} else {
model.addAttribute("message", "密码错误");
return new ModelAndView("user/login");
... ...
... ... @@ -2,6 +2,7 @@ package com.ui.interceptor;
import com.ui.User.UserAuthLocal;
import com.ui.User.UserOperateListener;
import com.ui.model.Directional;
import com.ui.model.req.User;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
... ... @@ -40,6 +41,8 @@ public class AuthInterceptor implements HandlerInterceptor {
return true;
}
}else{
//设置定向Url
Directional.getInstance().setLoginTargetUrl(request.getRequestURI());
response.sendRedirect("/user/toLogin");
return false;
}
... ...