|
|
@@ -2,9 +2,11 @@ package com.uas.platform.b2bManage.web.filter;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.uas.platform.b2bManage.core.support.SystemSession;
|
|
|
+import com.uas.platform.b2bManage.model.Constant;
|
|
|
import com.uas.platform.b2bManage.model.User;
|
|
|
import com.uas.platform.b2bManage.support.SecurityConstant;
|
|
|
import com.uas.platform.core.util.AgentUtils;
|
|
|
+import com.uas.platform.core.util.encry.Md5Utils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
@@ -12,10 +14,14 @@ import org.springframework.web.servlet.ModelAndView;
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -38,23 +44,68 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
|
|
|
}
|
|
|
|
|
|
private final boolean authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- HttpServletRequest httpRequest = request;
|
|
|
- logSession(httpRequest);
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
+ List<Cookie> cookieList = Arrays.asList(cookies);
|
|
|
+ final boolean[] cookieExist = {false};
|
|
|
+ boolean cookieFlag = checkCookie(cookieExist, cookieList, request);
|
|
|
+ if (!cookieFlag) {
|
|
|
+ if (!this.onAuthenticateFailed(request, response)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logSession(request);
|
|
|
User user = SystemSession.getUser();
|
|
|
// 未登录则要求登录
|
|
|
if (user == null) {
|
|
|
logoutSession(request);
|
|
|
- if(!this.onAuthenticateFailed(request, response)) {
|
|
|
+ if (!this.onAuthenticateFailed(request, response)) {
|
|
|
return false;
|
|
|
} else {
|
|
|
return true;
|
|
|
}
|
|
|
} else {
|
|
|
- onAuthenticateSuccess(request);
|
|
|
+ checkLogin(response);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验cookie是否存在
|
|
|
+ *
|
|
|
+ * @param cookieExist 是否存在cookie
|
|
|
+ * @param cookieList cookie列表
|
|
|
+ * @param request HttpServletRequest
|
|
|
+ */
|
|
|
+ private boolean checkCookie(boolean[] cookieExist, List<Cookie> cookieList, HttpServletRequest request) {
|
|
|
+ String ipMd5 = Md5Utils.encode(AgentUtils.getIp(request), null);
|
|
|
+ cookieList.forEach(cookie -> {
|
|
|
+ switch (cookie.getName()) {
|
|
|
+ case (Constant.COOKIE_NAME):
|
|
|
+ if (cookie.getValue().equals(ipMd5)) {
|
|
|
+ cookieExist[0] = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return cookieExist[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验登录信息
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @throws IOException IO异常
|
|
|
+ */
|
|
|
+ protected static void checkLogin(HttpServletResponse response) throws IOException {
|
|
|
+ User user = SystemSession.getUser();
|
|
|
+ if (null == user) {
|
|
|
+ response.sendRedirect(SecurityConstant.LOGIN_URL);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将user信息放在当前线程里面
|
|
|
*
|
|
|
@@ -79,7 +130,7 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
|
|
|
/**
|
|
|
* 验证成功,获取用户信息
|
|
|
*
|
|
|
- * @param request
|
|
|
+ * @param request HttpServletRequest
|
|
|
*/
|
|
|
protected void onAuthenticateSuccess(HttpServletRequest request) {
|
|
|
User user = (User) request.getAttribute("user");
|
|
|
@@ -88,23 +139,35 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 验证失败重新登录
|
|
|
+ *
|
|
|
+ * @param request HttpServletRequest
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @return
|
|
|
+ * @throws IOException IO异常
|
|
|
+ */
|
|
|
private boolean onAuthenticateFailed(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- SystemSession.clear();
|
|
|
- User user = (User) request.getSession().getAttribute("user");
|
|
|
- if (user != null) {
|
|
|
- SystemSession.setUser(user);
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ removeLocalSession(session);
|
|
|
+ if (SecurityConstant.AUTHENTICATION_URL.equals(request.getRequestURI())) {
|
|
|
return true;
|
|
|
- } else {
|
|
|
- if (SecurityConstant.AUTHENTICATION_URL.equals(request.getRequestURI())) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- AntPathRequestMatcher matcher = new AntPathRequestMatcher("/account/enterprise/info/**");
|
|
|
- if (matcher.matches(request)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
- response.sendRedirect(SecurityConstant.LOGIN_URL);
|
|
|
- return false;
|
|
|
+ }
|
|
|
+ response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
+ response.sendRedirect(SecurityConstant.LOGIN_URL);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除登录信息
|
|
|
+ * @param session HttpSession
|
|
|
+ */
|
|
|
+ private void removeLocalSession(HttpSession session) {
|
|
|
+ SystemSession.clear();
|
|
|
+ session.invalidate();
|
|
|
+ User user = SystemSession.getUser();
|
|
|
+ if (null != user) {
|
|
|
+ removeLocalSession(session);
|
|
|
}
|
|
|
}
|
|
|
|