|
|
@ -1,13 +1,23 @@ |
|
|
|
package com.epmet.commons.extappauth.aspect; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
import com.epmet.commons.extappauth.annotation.ExternalAppRequestAuth; |
|
|
|
import com.epmet.commons.extappauth.annotation.InternalAppRequestAuth; |
|
|
|
import com.epmet.commons.extappauth.bean.ExternalAppRequestParam; |
|
|
|
import com.epmet.commons.extappauth.jwt.JwtTokenUtils; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
import com.epmet.commons.tools.redis.RedisUtils; |
|
|
|
import com.epmet.commons.tools.security.dto.BaseTokenDto; |
|
|
|
import com.epmet.commons.tools.security.dto.GovTokenDto; |
|
|
|
import com.epmet.commons.tools.security.dto.TokenDto; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.dto.form.ExternalAppAuthFormDTO; |
|
|
|
import com.epmet.dto.result.ExternalAppAuthResultDTO; |
|
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|
|
|
import io.jsonwebtoken.Claims; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.aspectj.lang.JoinPoint; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
@ -24,6 +34,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.lang.reflect.Parameter; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* 外部应用请求认证切面 |
|
|
@ -35,6 +46,7 @@ public class ExternalAppRequestAuthAspect { |
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ExternalAppRequestAuthAspect.class); |
|
|
|
|
|
|
|
public static final String AUTHORIZATION_TOKEN_HEADER_KEY = "Authorization"; |
|
|
|
public static final String ACCESS_TOKEN_HEADER_KEY = "AccessToken"; |
|
|
|
public static final String APP_ID_HEADER_KEY = "appId"; |
|
|
|
public static final String APP_ID_TIMESTAMP_KEY = "ts"; |
|
|
@ -44,15 +56,117 @@ public class ExternalAppRequestAuthAspect { |
|
|
|
@Autowired |
|
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private JwtTokenUtils jwtTokenUtils; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
|
|
|
|
/** |
|
|
|
* 拦截加了ExternalRequestAuth注解的方法 |
|
|
|
* |
|
|
|
* @param point |
|
|
|
* @throws Throwable |
|
|
|
*/ |
|
|
|
@Before("@annotation(com.epmet.commons.extappauth.annotation.ExternalAppRequestAuth)") |
|
|
|
@Before("@annotation(com.epmet.commons.extappauth.annotation.ExternalAppRequestAuth) " + |
|
|
|
"|| @annotation(com.epmet.commons.extappauth.annotation.InternalAppRequestAuth)") |
|
|
|
public void auth(JoinPoint point) throws Throwable { |
|
|
|
MethodSignature signature = (MethodSignature) point.getSignature(); |
|
|
|
HttpServletRequest request = getRequest(); |
|
|
|
|
|
|
|
if (signature.getMethod().getAnnotation(ExternalAppRequestAuth.class) != null |
|
|
|
&& StringUtils.isNotBlank(request.getHeader(ACCESS_TOKEN_HEADER_KEY))) { |
|
|
|
// 走外部应用认证
|
|
|
|
extAppAuth(signature, point, request); |
|
|
|
} else if (signature.getMethod().getAnnotation(InternalAppRequestAuth.class) != null |
|
|
|
&& StringUtils.isNotBlank(request.getHeader(AUTHORIZATION_TOKEN_HEADER_KEY))) { |
|
|
|
// 走内部应用认证
|
|
|
|
String customerId = null; |
|
|
|
internalAppAuth(signature, point, request); |
|
|
|
} else { |
|
|
|
throw new RenException(EpmetErrorCode.UNSUPPORT_AUTH_TYPE.getCode(), EpmetErrorCode.UNSUPPORT_AUTH_TYPE.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 内部应用认证 |
|
|
|
* @param signature |
|
|
|
* @param point |
|
|
|
* @param request |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private void internalAppAuth(MethodSignature signature, JoinPoint point, HttpServletRequest request) { |
|
|
|
String authorization = request.getHeader(AUTHORIZATION_TOKEN_HEADER_KEY); |
|
|
|
BaseTokenDto tokenDTO = getTokenDTO(authorization); |
|
|
|
|
|
|
|
Map<String, Object> tokenMap = redisUtils.hGetAll(RedisKeys.getCpUserKey(tokenDTO.getApp(), tokenDTO.getClient(), tokenDTO.getUserId())); |
|
|
|
BaseTokenDto baseTokenDto = null; |
|
|
|
String customerId; |
|
|
|
if ("gov".equals(tokenDTO.getApp())) { |
|
|
|
GovTokenDto govTokenDto = BeanUtil.mapToBean(tokenMap, GovTokenDto.class, true); |
|
|
|
customerId = govTokenDto.getCustomerId(); |
|
|
|
baseTokenDto = govTokenDto; |
|
|
|
} else { |
|
|
|
TokenDto tokenDto = BeanUtil.mapToBean(tokenMap, TokenDto.class, true); |
|
|
|
customerId = tokenDto.getCustomerId(); |
|
|
|
baseTokenDto = tokenDTO; |
|
|
|
} |
|
|
|
|
|
|
|
if (baseTokenDto == null) { |
|
|
|
logger.error("内部应用认证,redis中没有找到登录缓存信息"); |
|
|
|
throw new RenException(EpmetErrorCode.ERR10006.getCode(), EpmetErrorCode.ERR10006.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
if (!authorization.equals(baseTokenDto.getToken())) { |
|
|
|
logger.error("内部应用认证,redis中找到的token与header里面传入的不一致,可能发生了别处登录"); |
|
|
|
throw new RenException(EpmetErrorCode.ERR10007.getCode(), EpmetErrorCode.ERR10007.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
// header参数赋值
|
|
|
|
Parameter[] parameters = signature.getMethod().getParameters(); |
|
|
|
if (parameters != null && parameters.length != 0) { |
|
|
|
for (int i = 0; i < parameters.length; i++) { |
|
|
|
if (parameters[i].getType() == ExternalAppRequestParam.class) { |
|
|
|
ExternalAppRequestParam requestParam = (ExternalAppRequestParam) point.getArgs()[i]; |
|
|
|
requestParam.setCustomerId(customerId); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private BaseTokenDto getTokenDTO(String authorization) { |
|
|
|
if (StringUtils.isBlank(authorization)) { |
|
|
|
logger.error("内部应用认证,没有携带authorization头信息"); |
|
|
|
throw new RenException(EpmetErrorCode.ERR401.getCode(), EpmetErrorCode.ERR401.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
//是否过期
|
|
|
|
Claims claims = jwtTokenUtils.getClaimByToken(authorization); |
|
|
|
if (claims == null) { |
|
|
|
logger.error("内部应用认证,Claims为空"); |
|
|
|
throw new RenException(EpmetErrorCode.ERR401.getCode(), EpmetErrorCode.ERR401.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
if (jwtTokenUtils.isTokenExpired(claims.getExpiration())) { |
|
|
|
logger.error("内部应用认证,token过期"); |
|
|
|
throw new RenException(EpmetErrorCode.ERR10006.getCode(), EpmetErrorCode.ERR10006.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
//获取用户ID
|
|
|
|
String app = (String) claims.get("app"); |
|
|
|
String client = (String) claims.get("client"); |
|
|
|
String userId = (String) claims.get("userId"); |
|
|
|
return new BaseTokenDto(app, client, userId, authorization); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 外部应用认证 |
|
|
|
* |
|
|
|
* @param signature |
|
|
|
* @param point |
|
|
|
*/ |
|
|
|
public void extAppAuth(MethodSignature signature, JoinPoint point, HttpServletRequest request) { |
|
|
|
String token = request.getHeader(ACCESS_TOKEN_HEADER_KEY); |
|
|
|
String appId = request.getHeader(APP_ID_HEADER_KEY); |
|
|
|
String ts = request.getHeader(APP_ID_TIMESTAMP_KEY); |
|
|
@ -89,7 +203,6 @@ public class ExternalAppRequestAuthAspect { |
|
|
|
|
|
|
|
|
|
|
|
// header参数赋值
|
|
|
|
MethodSignature signature = (MethodSignature) point.getSignature(); |
|
|
|
Parameter[] parameters = signature.getMethod().getParameters(); |
|
|
|
if (parameters != null && parameters.length != 0) { |
|
|
|
for (int i = 0; i < parameters.length; i++) { |
|
|
|