|
|
@ -1,19 +1,10 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
import com.epmet.commons.tools.redis.RedisUtils; |
|
|
|
import com.epmet.dao.ExternalAppDao; |
|
|
|
import com.epmet.dao.ExternalAppSecretDao; |
|
|
|
import com.epmet.constant.ExtAppAuthTypeConstant; |
|
|
|
import com.epmet.dto.result.ExternalAppAuthResultDTO; |
|
|
|
import com.epmet.dto.result.ExternalAppResultDTO; |
|
|
|
import com.epmet.entity.ExternalAppEntity; |
|
|
|
import com.epmet.entity.ExternalAppSecretEntity; |
|
|
|
import com.epmet.service.ExternalAppAuthService; |
|
|
|
import com.epmet.utils.externalapp.ExtAppJwtTokenUtils; |
|
|
|
import io.jsonwebtoken.Claims; |
|
|
|
import com.epmet.utils.externalapp.ExtAppJwtAuthProcessor; |
|
|
|
import com.epmet.utils.externalapp.ExtAppMD5AuthProcessor; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -26,90 +17,23 @@ public class ExternalAppAuthServiceImpl implements ExternalAppAuthService { |
|
|
|
private static Logger logger = LoggerFactory.getLogger(ExternalAppAuthServiceImpl.class); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
private ExtAppJwtAuthProcessor jwtAuthProcessor; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ExtAppJwtTokenUtils jwtTokenUtils; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ExternalAppSecretDao externalAppSecretDao; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ExternalAppDao externalAppDao; |
|
|
|
|
|
|
|
private int diffMillins = 1000 * 60 * 5; |
|
|
|
private ExtAppMD5AuthProcessor md5AuthProcessor; |
|
|
|
|
|
|
|
@Override |
|
|
|
public ExternalAppAuthResultDTO auth(String appId, String token) { |
|
|
|
String secret; |
|
|
|
if (StringUtils.isBlank(secret = getTokenByAppId(appId))) { |
|
|
|
return fillAuthResult(false, String.format("根据AppId:%s没有找到对应的秘钥", appId), null); |
|
|
|
} |
|
|
|
|
|
|
|
Claims claim; |
|
|
|
try { |
|
|
|
claim = jwtTokenUtils.getClaimByToken(token, secret); |
|
|
|
} catch (Exception e) { |
|
|
|
String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
logger.error("解析token失败:{}", errorStackTrace); |
|
|
|
return fillAuthResult(false, "解析token失败", null); |
|
|
|
} |
|
|
|
|
|
|
|
String appIdIn = (String)claim.get("appId"); |
|
|
|
String customerId = (String)claim.get("customerId"); |
|
|
|
Long timestamp = (Long)claim.get("ts"); |
|
|
|
|
|
|
|
//校验时间戳,允许5分钟误差
|
|
|
|
if (StringUtils.isAnyBlank(appIdIn, customerId) || timestamp == null) { |
|
|
|
logger.error("access token不完整。{},{},{}", appIdIn, customerId, timestamp); |
|
|
|
return fillAuthResult(false, "access token不完整。", null); |
|
|
|
} |
|
|
|
|
|
|
|
// TODO
|
|
|
|
// if (!validTimeStamp(timestamp)) {
|
|
|
|
// logger.error("服务器存在时差过大,请求被拒绝", appId, appIdIn);
|
|
|
|
// return fillAuthResult(false, "服务器存在时差过大,请求被拒绝", null);
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (!appId.equals(appIdIn)) { |
|
|
|
logger.error("AppId不对应,token外部的:{}, token内部解析出来的:{}", appId, appIdIn); |
|
|
|
return fillAuthResult(false, "Header中的AppId不匹配", null); |
|
|
|
} |
|
|
|
return fillAuthResult(true, "解析成功", customerId); |
|
|
|
} |
|
|
|
|
|
|
|
private boolean validTimeStamp(Long timestamp) { |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
// System.out.println(new Date(timestamp));
|
|
|
|
if (Math.abs(now - timestamp) > diffMillins) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
public ExternalAppAuthResultDTO auth(String appId, String token, Long ts, String authType) { |
|
|
|
// 没传或者传的jwt都用jwtprocessor处理
|
|
|
|
if (StringUtils.isBlank(authType) || ExtAppAuthTypeConstant.JWT.equals(authType)) { |
|
|
|
return jwtAuthProcessor.auth(appId, token, ts); |
|
|
|
} else if (ExtAppAuthTypeConstant.MD5.equals(authType)) { |
|
|
|
return md5AuthProcessor.auth(appId, token, ts); |
|
|
|
} else { |
|
|
|
ExternalAppAuthResultDTO rst = new ExternalAppAuthResultDTO(); |
|
|
|
rst.setMessage("错误的认证类型"); |
|
|
|
rst.setSuccess(false); |
|
|
|
return rst; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通过APP ID查询对应的秘钥 |
|
|
|
* @param appId |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String getTokenByAppId(String appId) { |
|
|
|
String secret = (String)redisUtils.get(RedisKeys.getExternalAppSecretKey(appId)); |
|
|
|
if (StringUtils.isBlank(secret)) { |
|
|
|
ExternalAppSecretEntity secretEntity = externalAppSecretDao.getSecretsByAppId(appId); |
|
|
|
if (secretEntity == null) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
secret = secretEntity.getSecret(); |
|
|
|
redisUtils.set(RedisKeys.getExternalAppSecretKey(appId), secret); |
|
|
|
} |
|
|
|
return secret; |
|
|
|
} |
|
|
|
|
|
|
|
public ExternalAppAuthResultDTO fillAuthResult(Boolean result, String message, String customerId) { |
|
|
|
ExternalAppAuthResultDTO authResult = new ExternalAppAuthResultDTO(); |
|
|
|
authResult.setSuccess(result); |
|
|
|
authResult.setMessage(message); |
|
|
|
authResult.setCustomerId(customerId); |
|
|
|
return authResult; |
|
|
|
} |
|
|
|
} |
|
|
|