|
@@ -3,15 +3,16 @@ package com.zanxiang.sdk.common.resolver;
|
|
|
import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
import com.zanxiang.common.exception.CustomException;
|
|
|
import com.zanxiang.common.utils.StringUtils;
|
|
|
+import com.zanxiang.mybatis.entity.GameKey;
|
|
|
import com.zanxiang.sdk.common.annotation.ValidLogin;
|
|
|
import com.zanxiang.sdk.common.util.DeviceCheckUtil;
|
|
|
+import com.zanxiang.sdk.common.util.SignUtil;
|
|
|
import com.zanxiang.sdk.domain.dto.UserTokenDTO;
|
|
|
import com.zanxiang.sdk.domain.params.UserData;
|
|
|
-import com.zanxiang.sdk.service.Impl.pay.AliPayServiceImpl;
|
|
|
+import com.zanxiang.sdk.service.GameKeyService;
|
|
|
import com.zanxiang.sdk.service.UserTokenService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -22,6 +23,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* token转用户信息解析器
|
|
@@ -34,18 +36,20 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
@Slf4j
|
|
|
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
|
|
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(AliPayServiceImpl.class);
|
|
|
-
|
|
|
@Resource
|
|
|
private UserTokenService userTokenService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private GameKeyService gameKeyService;
|
|
|
+
|
|
|
@Override
|
|
|
public boolean supportsParameter(MethodParameter parameter) {
|
|
|
return parameter.getParameterType().equals(UserData.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
|
|
+ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
|
|
|
+ WebDataBinderFactory binderFactory) throws Exception {
|
|
|
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
|
|
if (request == null) {
|
|
|
throw new CustomException(HttpStatusEnum.UNKNOWN_ERROR);
|
|
@@ -56,30 +60,55 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
|
|
if (b && StringUtils.isEmpty(token)) {
|
|
|
throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
|
|
|
}
|
|
|
+ //签名验证
|
|
|
+ String gameId = this.signCheck(request);
|
|
|
+ //请求头处理
|
|
|
String userAgent = request.getHeader("user-agent");
|
|
|
- logger.info("user-agent:{}", userAgent);
|
|
|
Integer deviceType = DeviceCheckUtil.getType(userAgent);
|
|
|
UserData data = new UserData();
|
|
|
data.setDeviceType(deviceType);
|
|
|
data.setDeviceSystem(DeviceCheckUtil.getDeviceSystem(userAgent));
|
|
|
- logger.info("UserData:{}", data);
|
|
|
+ data.setGameId(Long.valueOf(gameId));
|
|
|
if (StringUtils.isEmpty(token)) {
|
|
|
if (b) {
|
|
|
throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
- UserTokenDTO tokenInfoByTokenDevice = userTokenService.getTokenInfoByTokenDevice(token, deviceType);
|
|
|
- if (tokenInfoByTokenDevice == null) {
|
|
|
+ UserTokenDTO userTokenDTO = userTokenService.getTokenInfoByTokenDevice(token, deviceType);
|
|
|
+ if (userTokenDTO == null) {
|
|
|
if (b) {
|
|
|
throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
- if (b && tokenInfoByTokenDevice.getUserId() == null) {
|
|
|
+ if (b && userTokenDTO.getUserId() == null) {
|
|
|
throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
|
|
|
}
|
|
|
- data.setUserId(tokenInfoByTokenDevice.getUserId());
|
|
|
+ data.setUserId(userTokenDTO.getUserId());
|
|
|
+ log.info("UserData:{}", data);
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
+ private String signCheck(HttpServletRequest request) throws Exception {
|
|
|
+ //游戏id和密钥
|
|
|
+ String gameId = request.getHeader("gameId");
|
|
|
+ String sign = request.getHeader("sign");
|
|
|
+ String timestamp = request.getHeader("timestamp");
|
|
|
+ if (Strings.isBlank(sign) || Strings.isBlank(timestamp) || Strings.isBlank(gameId)) {
|
|
|
+ throw new CustomException(HttpStatusEnum.INVALID_PARAMS);
|
|
|
+ }
|
|
|
+ //签名验证
|
|
|
+ GameKey gameKey = gameKeyService.getByGameId(Long.valueOf(gameId));
|
|
|
+ if (gameKey == null || Strings.isBlank(gameKey.getAppKey())) {
|
|
|
+ throw new CustomException(HttpStatusEnum.INVALID_PARAMS);
|
|
|
+ }
|
|
|
+ String str = "appKey=" + gameKey.getAppKey() + "&gameId=" + gameId + "×tamp=" + timestamp;
|
|
|
+ String mySign = SignUtil.MD5(str);
|
|
|
+ //签名对比
|
|
|
+ if (!Objects.equals(mySign, sign)) {
|
|
|
+ throw new CustomException(HttpStatusEnum.INVALID_PARAMS);
|
|
|
+ }
|
|
|
+ return gameId;
|
|
|
+ }
|
|
|
}
|