|
@@ -24,6 +24,7 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import edp.core.annotation.AuthIgnore;
|
|
|
import edp.core.annotation.AuthShare;
|
|
|
import edp.core.enums.HttpCodeEnum;
|
|
|
+import edp.core.utils.RedisUtils;
|
|
|
import edp.core.utils.TokenUtils;
|
|
|
import edp.davinci.core.common.Constants;
|
|
|
import edp.davinci.core.common.ErrorMsg;
|
|
@@ -31,7 +32,9 @@ import edp.davinci.core.common.ResultMap;
|
|
|
import edp.davinci.model.User;
|
|
|
import edp.davinci.service.UserService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.xmlbeans.impl.tool.XMLBean;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
import org.springframework.web.method.HandlerMethod;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
@@ -39,40 +42,95 @@ import org.springframework.web.servlet.ModelAndView;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
-public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
+public class AuthenticationInterceptor implements HandlerInterceptor
|
|
|
+{
|
|
|
|
|
|
+ private final static String GET_USER_INFO_URL = "http://taihu-auth/thirdLogin/getUserInfo";
|
|
|
@Autowired
|
|
|
private TokenUtils tokenUtils;
|
|
|
|
|
|
@Autowired
|
|
|
private UserService userService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisUtils redisUtils;
|
|
|
+
|
|
|
@Override
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
|
|
|
+ {
|
|
|
|
|
|
HandlerMethod handlerMethod = null;
|
|
|
- try {
|
|
|
+ try
|
|
|
+ {
|
|
|
handlerMethod = (HandlerMethod) handler;
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (Exception e)
|
|
|
+ {
|
|
|
response.setStatus(HttpCodeEnum.NOT_FOUND.getCode());
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+ // add by zhangheng
|
|
|
+ Method method = handlerMethod.getMethod();
|
|
|
+ AuthIgnore ignoreAuthMethod = method.getAnnotation(AuthIgnore.class);
|
|
|
+ //注解不需要验证token
|
|
|
+// if (handler instanceof HandlerMethod && null != ignoreAuthMethod)
|
|
|
+// {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+
|
|
|
+ String ticket = request.getParameter("ticket");
|
|
|
+ if (StringUtils.isEmpty(ticket))
|
|
|
+ {
|
|
|
+ if (!request.getServletPath().endsWith("/download/page"))
|
|
|
+ {
|
|
|
+ log.debug("{} : Unknown token", request.getServletPath());
|
|
|
+ }
|
|
|
+ response.setStatus(HttpCodeEnum.FORBIDDEN.getCode());
|
|
|
+ response.getWriter().print(ErrorMsg.ERR_MSG_AUTHENTICATION);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ System.out.println(">>>>>" + ticket);
|
|
|
+ Map<String,Object> redisMap = (Map<String, Object>) redisUtils.get(ticket);
|
|
|
+ Map<String,Object> userMap = (Map<String, Object>) redisMap.get("user");
|
|
|
+ System.out.println(userMap.get("username"));
|
|
|
+ String username = (String) userMap.get("username");
|
|
|
+ User user = userService.getByUsername(username);
|
|
|
+ if (null == user)
|
|
|
+ {
|
|
|
+ if (!request.getServletPath().endsWith("/download/page"))
|
|
|
+ {
|
|
|
+ log.debug("{} : token user not found", request.getServletPath());
|
|
|
+ }
|
|
|
+ response.setStatus(HttpCodeEnum.FORBIDDEN.getCode());
|
|
|
+ response.getWriter().print(ErrorMsg.ERR_MSG_PERMISSION);
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+ request.setAttribute(Constants.CURRENT_USER, user);
|
|
|
+ // add by zhangheng
|
|
|
+ /*
|
|
|
Method method = handlerMethod.getMethod();
|
|
|
|
|
|
AuthIgnore ignoreAuthMethod = method.getAnnotation(AuthIgnore.class);
|
|
|
//注解不需要验证token
|
|
|
- if (handler instanceof HandlerMethod && null != ignoreAuthMethod) {
|
|
|
+ if (handler instanceof HandlerMethod && null != ignoreAuthMethod)
|
|
|
+ {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
String token = request.getHeader(Constants.TOKEN_HEADER_STRING);
|
|
|
|
|
|
AuthShare authShareMethod = method.getAnnotation(AuthShare.class);
|
|
|
- if (null != authShareMethod) {
|
|
|
- if (!StringUtils.isEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
|
|
|
+ if (null != authShareMethod)
|
|
|
+ {
|
|
|
+ if (!StringUtils.isEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
|
|
+ {
|
|
|
String username = tokenUtils.getUsername(token);
|
|
|
User user = userService.getByUsername(username);
|
|
|
request.setAttribute(Constants.CURRENT_USER, user);
|
|
@@ -80,8 +138,10 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- if (StringUtils.isEmpty(token) || !token.startsWith(Constants.TOKEN_PREFIX)) {
|
|
|
- if (!request.getServletPath().endsWith("/download/page")) {
|
|
|
+ if (StringUtils.isEmpty(token) || !token.startsWith(Constants.TOKEN_PREFIX))
|
|
|
+ {
|
|
|
+ if (!request.getServletPath().endsWith("/download/page"))
|
|
|
+ {
|
|
|
log.debug("{} : Unknown token", request.getServletPath());
|
|
|
}
|
|
|
response.setStatus(HttpCodeEnum.FORBIDDEN.getCode());
|
|
@@ -90,8 +150,10 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
}
|
|
|
String username = tokenUtils.getUsername(token);
|
|
|
User user = userService.getByUsername(username);
|
|
|
- if (null == user) {
|
|
|
- if (!request.getServletPath().endsWith("/download/page")) {
|
|
|
+ if (null == user)
|
|
|
+ {
|
|
|
+ if (!request.getServletPath().endsWith("/download/page"))
|
|
|
+ {
|
|
|
log.debug("{} : token user not found", request.getServletPath());
|
|
|
}
|
|
|
response.setStatus(HttpCodeEnum.FORBIDDEN.getCode());
|
|
@@ -99,8 +161,10 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
- if (!tokenUtils.validateToken(token, user)) {
|
|
|
- if (!request.getServletPath().endsWith("/download/page")) {
|
|
|
+ if (!tokenUtils.validateToken(token, user))
|
|
|
+ {
|
|
|
+ if (!request.getServletPath().endsWith("/download/page"))
|
|
|
+ {
|
|
|
log.debug("{} : token validation fails", request.getServletPath());
|
|
|
}
|
|
|
response.setStatus(HttpCodeEnum.FORBIDDEN.getCode());
|
|
@@ -108,8 +172,10 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (!request.getServletPath().contains("/user/active") && !user.getActive()) {
|
|
|
- if (request.getServletPath().contains("/user/sendmail")) {
|
|
|
+ if (!request.getServletPath().contains("/user/active") && !user.getActive())
|
|
|
+ {
|
|
|
+ if (request.getServletPath().contains("/user/sendmail"))
|
|
|
+ {
|
|
|
request.setAttribute(Constants.CURRENT_USER, user);
|
|
|
return true;
|
|
|
}
|
|
@@ -120,16 +186,39 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
|
|
|
return false;
|
|
|
}
|
|
|
request.setAttribute(Constants.CURRENT_USER, user);
|
|
|
+ */
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ public String findUserInfo(String ticket)
|
|
|
+ {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("ticket", ticket);
|
|
|
+ String body = null;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ System.out.println(GET_USER_INFO_URL + "?ticket={ticket}");
|
|
|
+ body = restTemplate.getForEntity(GET_USER_INFO_URL + "?ticket={ticket}", String.class, params).getBody();
|
|
|
+ System.out.println(body);
|
|
|
+ } catch (Exception e)
|
|
|
+ {
|
|
|
+ log.error("调用接口异常,{}", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
|
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
|
|
|
+ {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
|
|
|
+ {
|
|
|
|
|
|
}
|
|
|
}
|