|
|
@@ -1,11 +1,15 @@
|
|
|
package com.uas.kanban.filter;
|
|
|
|
|
|
import com.uas.kanban.annotation.NotEmpty;
|
|
|
+import com.uas.kanban.dao.RememberKeyDao;
|
|
|
+import com.uas.kanban.dao.UserDao;
|
|
|
import com.uas.kanban.exception.OperationException;
|
|
|
+import com.uas.kanban.model.RememberKey;
|
|
|
import com.uas.kanban.model.User;
|
|
|
import com.uas.kanban.model.User.Role;
|
|
|
import com.uas.kanban.support.SessionHelper;
|
|
|
import com.uas.kanban.support.SystemSession;
|
|
|
+import com.uas.kanban.util.ArrayUtils;
|
|
|
import com.uas.kanban.util.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -15,13 +19,11 @@ import org.springframework.util.AntPathMatcher;
|
|
|
import org.springframework.util.PathMatcher;
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 安全验证
|
|
|
@@ -34,6 +36,12 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RememberKeyDao rememberKeyDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserDao userDao;
|
|
|
+
|
|
|
@Autowired
|
|
|
private SessionHelper sessionHelper;
|
|
|
|
|
|
@@ -61,24 +69,35 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
|
|
|
// session 中有登陆信息,重定向到首页
|
|
|
if (user != null) {
|
|
|
response.sendRedirect("");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 没有登陆信息,则尝试自动登陆
|
|
|
+ user = autoLogin(request);
|
|
|
+ if (user != null) {
|
|
|
+ response.sendRedirect("");
|
|
|
+ return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// session 中不存在登陆信息
|
|
|
if (user == null) {
|
|
|
- logger.info("No session for path: " + url + " , redirecting to page: login ...");
|
|
|
- // 如果是 XMLHttpRequest ,抛出异常,否则重定向
|
|
|
- if (Objects.equals("XMLHttpRequest", request.getHeader("X-Requested-With"))) {
|
|
|
- throw new SecurityException("未登录");
|
|
|
- }
|
|
|
- String returnUrl = request.getRequestURL().toString();
|
|
|
- String queryString = request.getQueryString();
|
|
|
- if (!StringUtils.isEmpty(queryString)) {
|
|
|
- returnUrl += "?" + queryString;
|
|
|
+ // 尝试自动登陆
|
|
|
+ user = autoLogin(request);
|
|
|
+ if (user == null) {
|
|
|
+ logger.info("No session for path: " + url + " , redirecting to page: login ...");
|
|
|
+ // 如果是 XMLHttpRequest ,抛出异常,否则重定向
|
|
|
+ if (Objects.equals("XMLHttpRequest", request.getHeader("X-Requested-With"))) {
|
|
|
+ throw new SecurityException("未登录");
|
|
|
+ }
|
|
|
+ String returnUrl = request.getRequestURL().toString();
|
|
|
+ String queryString = request.getQueryString();
|
|
|
+ if (!StringUtils.isEmpty(queryString)) {
|
|
|
+ returnUrl += "?" + queryString;
|
|
|
+ }
|
|
|
+ response.sendRedirect(contextPath + "/login?returnUrl=" + URLEncoder.encode(returnUrl, "UTF-8"));
|
|
|
+ return false;
|
|
|
}
|
|
|
- response.sendRedirect(contextPath + "/login?returnUrl=" + URLEncoder.encode(returnUrl, "UTF-8"));
|
|
|
- return false;
|
|
|
}
|
|
|
|
|
|
// 只允许管理员访问
|
|
|
@@ -93,6 +112,49 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 自动登陆
|
|
|
+ *
|
|
|
+ * @param request 请求
|
|
|
+ * @return 用户信息
|
|
|
+ */
|
|
|
+ private User autoLogin(HttpServletRequest request) {
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
+ String userCode = null;
|
|
|
+ String key = null;
|
|
|
+ // 获取 cookies 中的用户 code 和 key
|
|
|
+ if (!ArrayUtils.isEmpty(cookies)) {
|
|
|
+ for (Cookie cookie : cookies) {
|
|
|
+ switch (cookie.getName()) {
|
|
|
+ case "code":
|
|
|
+ userCode = cookie.getValue();
|
|
|
+ break;
|
|
|
+ case "key":
|
|
|
+ key = cookie.getValue();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果 cookies 中存有用户 code 和 key
|
|
|
+ if (!StringUtils.isEmpty(userCode) && !StringUtils.isEmpty(key)) {
|
|
|
+ RememberKey rememberKey = rememberKeyDao.findByUserCodeAndKey(userCode, key);
|
|
|
+ if (rememberKey != null) {
|
|
|
+ Date lastModified = rememberKey.getLastModified();
|
|
|
+ Long maxInactiveInterval = rememberKey.getMaxInactiveInterval();
|
|
|
+ Date now = new Date();
|
|
|
+ // 如果 key 未过有效期,自动创建 session
|
|
|
+ if (now.getTime() < lastModified.getTime() + maxInactiveInterval * 1000L) {
|
|
|
+ User user = userDao.findOne(userCode);
|
|
|
+ if (user != null) {
|
|
|
+ sessionHelper.saveSession(request, user);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
|
|
throws Exception {
|