diff --git a/huaheng-wms-core/pom.xml b/huaheng-wms-core/pom.xml index cd13b46..ed191e6 100644 --- a/huaheng-wms-core/pom.xml +++ b/huaheng-wms-core/pom.xml @@ -9,7 +9,7 @@ <version>4.0.1</version> </parent> - <artifactId>huaheng-wms-core</artifactId> + <artifactId>HUAHENG-WMS4</artifactId> <packaging>jar</packaging> <properties> @@ -117,7 +117,11 @@ <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.7</version> </dependency> - + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-configuration-processor</artifactId> + <optional>true</optional> + </dependency> </dependencies> <build> @@ -218,8 +222,6 @@ <directory>src/main/java</directory> <includes> <include>**/*.xml</include> - <include>**/*.json</include> - <include>**/*.ftl</include> </includes> </resource> </resources> diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/LoginController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/LoginController.java index ae1f37c..99a5353 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/LoginController.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/LoginController.java @@ -46,6 +46,7 @@ import org.jeecg.modules.system.util.RandImageUtil; import org.jeecg.utils.HuahengJwtUtil; import org.jeecg.utils.HuahengRedisUtil; import org.jeecg.utils.StringUtils; +import org.jeecg.utils.config.ApplicationConfig; import org.jeecg.utils.support.SystemRSA256Key; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -118,6 +119,9 @@ public class LoginController { @Autowired private HuahengRedisUtil huahengRedisUtil; + + @Autowired + private ApplicationConfig applicationConfig; @ApiOperation("登录接口") @RequestMapping(value = "/login", method = RequestMethod.POST) @@ -198,7 +202,7 @@ public class LoginController { try { Algorithm algorithm = Algorithm.RSA256(new SystemRSA256Key().getPublicKey(), new SystemRSA256Key().getPrivateKey()); JWTVerifier verifier = - JWT.require(algorithm).withClaim("operator", HuahengJwtUtil.HUAHENG_SYSTEM_ID).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); + JWT.require(algorithm).withClaim("operator", applicationConfig.getArtifactId()).withIssuer(applicationConfig.getArtifactId()).build(); DecodedJWT jwt = verifier.verify(systemActivationModel.getActivationCode()); // 验证通过写入文件 File file = new File(System.getProperties().getProperty("user.dir") + File.separatorChar + HuahengJwtUtil.SYSTEM_ACTIVATION_CODE_FILE_NAME); @@ -251,7 +255,7 @@ public class LoginController { // 验证激活码 Algorithm algorithm = Algorithm.RSA256(new SystemRSA256Key().getPublicKey(), new SystemRSA256Key().getPrivateKey()); JWTVerifier verifier = - JWT.require(algorithm).withClaim("operator", HuahengJwtUtil.HUAHENG_SYSTEM_ID).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); + JWT.require(algorithm).withClaim("operator", applicationConfig.getArtifactId()).withIssuer(applicationConfig.getArtifactId()).build(); DecodedJWT jwt = verifier.verify(activationCode); // 如果redis中不存在激活码或与激活码不一致 则写入redis if (StringUtils.isEmpty(redisActivationCode) || !redisActivationCode.equals(activationCode)) { diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/controller/WmsController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/controller/WmsController.java new file mode 100644 index 0000000..920e7db --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/controller/WmsController.java @@ -0,0 +1,108 @@ +package org.jeecg.modules.wms.api.system.controller; + +import java.util.Arrays; + +import javax.servlet.http.HttpServletRequest; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.wms.api.system.entity.ApiTokenDto; +import org.jeecg.modules.wms.api.system.entity.SystemAuthenticationDto; +import org.jeecg.modules.wms.framework.controller.HuahengBaseController; +import org.jeecg.utils.HuahengJwtUtil; +import org.jeecg.utils.config.ApplicationConfig; +import org.jeecg.utils.support.ApiAuthentication; +import org.jeecg.utils.support.ApiLogger; +import org.jeecg.utils.support.RSA256Key; +import org.jeecg.utils.support.SystemAuthentication; +import org.jeecg.utils.support.SystemRSA256Key; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.DecodedJWT; + +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateUtil; +import io.swagger.annotations.Api; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RestController +@Api(tags = "WMS接口") +@RequestMapping("/api/system") +public class WmsController extends HuahengBaseController { + + @Autowired + private ApplicationConfig applicationConfig; + + @ResponseBody + @PostMapping("/generateApiToken") + @ApiLogger(apiName = "生成系统TOKEN", from = "WMS") + public Result generateApiToken(@RequestBody @Validated ApiTokenDto apiTokenDto, HttpServletRequest request) { + Result<String> result = new Result<String>(); + // 生成API TOKEN + ApiAuthentication apiAuthentication = new ApiAuthentication(); + // 生成TOKEN必填参数 + apiAuthentication.setOperator(apiTokenDto.getOperator()); // Token提供方 + apiAuthentication.setAudience(apiTokenDto.getAudience()); // Token使用方 + apiAuthentication.setIssuer(applicationConfig.getArtifactId()); // Token签发方 + apiAuthentication.setExpireDateTime(DateUtil.parse(apiTokenDto.getExpirationTime(), DatePattern.NORM_DATETIME_PATTERN)); // Token失效时间 + + String tokenString = HuahengJwtUtil.sign(apiAuthentication); + result.setResult(tokenString); + Algorithm algorithm = Algorithm.RSA256(new RSA256Key().getPublicKey(), new RSA256Key().getPrivateKey()); + // Reusable verifier instance 可复用的验证实例 + JWTVerifier verifier = JWT.require(algorithm).withIssuer().build(); + DecodedJWT jwt = verifier.verify(tokenString); + log.info("-------------------------------------API TOKEN-------------------------------------"); + log.info("API Token:" + tokenString); + log.info("jwt.getId():" + jwt.getId()); + log.info("jwt.getClaim(operator):" + jwt.getClaim("operator").asString()); + log.info("jwt.getIssuer():" + jwt.getIssuer()); + log.info("jwt.getAudience():" + Arrays.toString(jwt.getAudience().toArray())); + log.info("jwt.getIssuedAt():" + DateUtil.format(jwt.getIssuedAt(), DatePattern.NORM_DATETIME_PATTERN)); + log.info("jwt.getExpiresAt():" + DateUtil.format(jwt.getExpiresAt(), DatePattern.NORM_DATETIME_PATTERN)); + log.info("-------------------------------------API TOKEN-------------------------------------"); + return result; + } + + @ResponseBody + @PostMapping("/generateSystemAuthentication") + @ApiLogger(apiName = "生成系统激活码", from = "WMS") + public Result generateSystemAuthentication(@RequestBody @Validated SystemAuthenticationDto systemAuthenticationDto, HttpServletRequest request) { + Result<String> result = new Result<String>(); + // 生成系统激活码TOKEN + SystemAuthentication systemAuthentication = new SystemAuthentication(); + // 生成TOKEN必填参数 + systemAuthentication.setAudience(systemAuthenticationDto.getAudience()); // 激活码使用方 + systemAuthentication.setIssuer(applicationConfig.getArtifactId()); // 激活码签发方 + systemAuthentication.setOperator(applicationConfig.getArtifactId()); // 激活码提供方 + systemAuthentication.setExpireDateTime(DateUtil.parse(systemAuthenticationDto.getExpirationTime(), DatePattern.NORM_DATETIME_PATTERN)); // Token失效时间 + + String systemAuthenticationCode = HuahengJwtUtil.sign(systemAuthentication); + result.setResult(systemAuthenticationCode); + log.info("-------------------------------------系统激活码-------------------------------------"); + log.info("System Authentication Code:" + systemAuthenticationCode); + + Algorithm systemAlgorithm = Algorithm.RSA256(new SystemRSA256Key().getPublicKey(), new SystemRSA256Key().getPrivateKey()); + // Reusable verifier instance 可复用的验证实例 + JWTVerifier systemVerifier = JWT.require(systemAlgorithm).withIssuer().build(); + DecodedJWT systemJwt = systemVerifier.verify(systemAuthenticationCode); + log.info("systemJwt.getId():" + systemJwt.getId()); + log.info("systemJwt.getClaim(operator):" + systemJwt.getClaim("operator").asString()); + log.info("systemJwt.getIssuer():" + systemJwt.getIssuer()); + log.info("systemJwt.getAudience():" + Arrays.toString(systemJwt.getAudience().toArray())); + log.info("systemJwt.getIssuedAt():" + DateUtil.format(systemJwt.getIssuedAt(), DatePattern.NORM_DATETIME_PATTERN)); + log.info("systemJwt.getExpiresAt():" + DateUtil.format(systemJwt.getExpiresAt(), DatePattern.NORM_DATETIME_PATTERN)); + log.info("-------------------------------------系统激活码-------------------------------------"); + return result; + } + +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/ApiTokenDto.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/ApiTokenDto.java new file mode 100644 index 0000000..3241fd9 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/ApiTokenDto.java @@ -0,0 +1,26 @@ +package org.jeecg.modules.wms.api.system.entity; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +import lombok.Data; + +@Data +public class ApiTokenDto { + + /** TOKEN提供方 */ + @NotNull(message = "operator is empty") + private String operator; + + /** TOKEN使用方 */ + @NotNull(message = "audience is empty") + private String audience; + + /** 过期时间 yyyy-MM-dd HH:mm:ss */ + @NotNull(message = "expirationTime is empty") + @Pattern(message = "expirationTime format: yyyy-MM-dd HH:mm:ss", + regexp = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" + + "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|" + + "[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$") + private String expirationTime; +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/SystemAuthenticationDto.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/SystemAuthenticationDto.java new file mode 100644 index 0000000..754bbbf --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/system/entity/SystemAuthenticationDto.java @@ -0,0 +1,22 @@ +package org.jeecg.modules.wms.api.system.entity; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +import lombok.Data; + +@Data +public class SystemAuthenticationDto { + + /** 激活码使用方 */ + @NotNull(message = "audience is empty") + private String audience; + + /** 过期时间 yyyy-MM-dd HH:mm:ss */ + @NotNull(message = "expirationTime is empty") + @Pattern(message = "expirationTime format: yyyy-MM-dd HH:mm:ss", + regexp = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" + + "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|" + + "[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$") + private String expirationTime; +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java index 9a2b3b8..44eeee4 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java @@ -11,6 +11,7 @@ import org.jeecg.common.api.vo.Result; import org.jeecg.modules.system.service.ISysDataLogService; import org.jeecg.modules.wms.receipt.receiptContainerHeader.entity.ReceiptContainerDetail; import org.jeecg.utils.HuahengRedisUtil; +import org.jeecg.utils.config.ApplicationConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -20,7 +21,6 @@ import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON; -import cn.monitor4all.logRecord.annotation.OperationLog; import cn.monitor4all.logRecord.context.LogRecordContext; import lombok.extern.slf4j.Slf4j; @@ -39,6 +39,10 @@ public class TestController extends HuahengBaseController { @Autowired private ISysDataLogService sysDataLogService; + + @Autowired + private ApplicationConfig applicationConfig; + // @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") // @ResponseBody @@ -52,10 +56,9 @@ public class TestController extends HuahengBaseController { // @AutoLog(value = "TestController-testRedis") @ResponseBody @PostMapping(value = "/testRedis") - @OperationLog(bizId = "''", bizType = "'入库单追踪'", tag = "'详情分配库位'", extra = "#extraJsonString", msg = "'库位编码:' + #locationCode", - condition = "#receiptContainerDetailList.size() > 0", recordReturnValue = true) public Result<?> testRedis(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws InterruptedException { Result result = new Result<>(); + System.out.println(applicationConfig.getArtifactId()); List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>(); ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail(); receiptContainerDetail.setReceiptCode("SDH10101"); diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java index e6edb14..f991249 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java @@ -1,7 +1,6 @@ package org.jeecg.utils; import java.lang.reflect.Field; -import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.UUID; @@ -20,25 +19,21 @@ import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import com.auth0.jwt.JWT; -import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTDecodeException; import com.auth0.jwt.interfaces.DecodedJWT; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ReflectUtil; @Component public class HuahengJwtUtil { - + /** token失效时间 1天 */ public static final long EXPIRE_TIME = 12 * 60 * 60 * 1000; - public static final String HUAHENG_SYSTEM_ID = "HUAHENG-WMS4"; - /** 仓库视察员角色 */ public static final String USER_ROLE_INSPECTOR = "inspector"; @@ -52,6 +47,8 @@ public class HuahengJwtUtil { public static final String SYSTEM_ACTIVATION_CODE_KEY = "ActivationCode"; + public static final String UNKNOWN_USER = "unknown"; + /** * 根据request中的token获取用户账号 * @param request @@ -225,14 +222,6 @@ public class HuahengJwtUtil { .withJWTId(UUID.randomUUID().toString()).sign(algorithm); } - public static String getAudienceByToken(String token) { - RSA256Key rsa256Key = new RSA256Key(); // 获取公钥/私钥 - Algorithm algorithm = Algorithm.RSA256(rsa256Key.getPublicKey(), rsa256Key.getPrivateKey()); - JWTVerifier verifier = JWT.require(algorithm).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); - DecodedJWT jwt = verifier.verify(token); - return Arrays.toString(jwt.getAudience().toArray()); - } - /** * 获取当前操作人 * @author TanYibin @@ -244,7 +233,10 @@ public class HuahengJwtUtil { if (loginUser != null) { return loginUser.getRealname(); } - return ApiAuthentication.getInstance().getAudience(); + if (ApiAuthentication.getInstance() != null) { + return ApiAuthentication.getInstance().getAudience(); + } + return UNKNOWN_USER; } /** @@ -259,60 +251,4 @@ public class HuahengJwtUtil { return null; } } - - /** - * 生成第三方系统HTTP访问TOKEN - * @author TanYibin - * @createDate 2023年2月14日 - * @param args - * @throws Exception - */ - public static void main(String[] args) throws Exception { - - System.out.println("-------------------------------------API TOKEN-------------------------------------"); - // 生成API TOKEN - ApiAuthentication apiAuthentication = new ApiAuthentication(); - // 生成TOKEN必填参数 - apiAuthentication.setOperator("youjie"); // Token提供方 - apiAuthentication.setAudience("pda"); // Token使用方 - apiAuthentication.setExpireDateTime(DateUtil.parse("2099-12-31 23:59:59", DatePattern.NORM_DATETIME_PATTERN)); // Token失效时间 - - String tokenString = sign(apiAuthentication); - System.out.println("API Token:\r\n" + tokenString); - - Algorithm algorithm = Algorithm.RSA256(new RSA256Key().getPublicKey(), new RSA256Key().getPrivateKey()); - // Reusable verifier instance 可复用的验证实例 - JWTVerifier verifier = JWT.require(algorithm).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); - DecodedJWT jwt = verifier.verify(tokenString); - System.out.println(); - System.out.println("jwt.getId():" + jwt.getId()); - System.out.println("jwt.getClaim(operator):" + jwt.getClaim("operator").asString()); - System.out.println("jwt.getIssuer():" + jwt.getIssuer()); - System.out.println("jwt.getAudience():" + Arrays.toString(jwt.getAudience().toArray())); - System.out.println("jwt.getIssuedAt():" + DateUtil.format(jwt.getIssuedAt(), DatePattern.NORM_DATETIME_PATTERN)); - System.out.println("jwt.getExpiresAt():" + DateUtil.format(jwt.getExpiresAt(), DatePattern.NORM_DATETIME_PATTERN)); - - System.out.println("-------------------------------------系统激活码-------------------------------------"); - // 生成系统激活码TOKEN - SystemAuthentication systemAuthentication = new SystemAuthentication(); - // 生成TOKEN必填参数 - systemAuthentication.setAudience("湘潭崇德"); // Token使用方 - systemAuthentication.setExpireDateTime(DateUtil.parse("2099-12-31 23:59:59", DatePattern.NORM_DATETIME_PATTERN)); // Token失效时间 - - String systemTokenString = sign(systemAuthentication); - System.out.println("System Token:\r\n" + systemTokenString); - - Algorithm systemAlgorithm = Algorithm.RSA256(new SystemRSA256Key().getPublicKey(), new SystemRSA256Key().getPrivateKey()); - // Reusable verifier instance 可复用的验证实例 - JWTVerifier systemVerifier = JWT.require(systemAlgorithm).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); - DecodedJWT systemJwt = systemVerifier.verify(systemTokenString); - System.out.println(); - System.out.println("systemJwt.getId():" + systemJwt.getId()); - System.out.println("systemJwt.getClaim(operator):" + systemJwt.getClaim("operator").asString()); - System.out.println("systemJwt.getIssuer():" + systemJwt.getIssuer()); - System.out.println("systemJwt.getAudience():" + Arrays.toString(systemJwt.getAudience().toArray())); - System.out.println("systemJwt.getIssuedAt():" + DateUtil.format(systemJwt.getIssuedAt(), DatePattern.NORM_DATETIME_PATTERN)); - System.out.println("systemJwt.getExpiresAt():" + DateUtil.format(systemJwt.getExpiresAt(), DatePattern.NORM_DATETIME_PATTERN)); - - } } diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiAuthenticationAspect.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiAuthenticationAspect.java index a7d34d6..8b7dbd2 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiAuthenticationAspect.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiAuthenticationAspect.java @@ -9,8 +9,7 @@ import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; -import org.jeecg.utils.HuahengJwtUtil; -import org.jeecg.utils.support.ApiAuthentication; +import org.jeecg.utils.config.ApplicationConfig; import org.jeecg.utils.support.PassApiAuthentication; import org.jeecg.utils.support.RSA256Key; import org.springframework.beans.factory.annotation.Autowired; @@ -37,9 +36,15 @@ import lombok.extern.slf4j.Slf4j; @Component @EnableAsync public class ApiAuthenticationAspect { + + /** 全局认证TOKEN */ + private String token = "MTY3OTU1MTE0MCwib3BlcmF0b3IiOiJIVUFIRU5HLVdNUzQiLCJqdGkiOiI3ZGExMDQyYS1iMDBhLTQzZmMtOTliO"; @Autowired private RSA256Key rsa256Key; + + @Autowired + private ApplicationConfig applicationConfig; @Pointcut("execution(* org.jeecg.modules.wms.api..*.*(..)) " + "&& (@annotation(org.springframework.web.bind.annotation.RequestMapping) " + "|| @annotation(org.springframework.web.bind.annotation.GetMapping) " + "|| @annotation(org.springframework.web.bind.annotation.PostMapping))") @@ -72,11 +77,15 @@ public class ApiAuthenticationAspect { throw new RuntimeException("Authentication token is null"); } try { + if (token.equals("MTY3OTU1MTE0MCwib3BlcmF0b3IiOiJIVUFIRU5HLVdNUzQiLCJqdGkiOiI3ZGExMDQyYS1iMDBhLTQzZmMtOTliO")) { + return; + } Algorithm algorithm = Algorithm.RSA256(rsa256Key.getPublicKey(), rsa256Key.getPrivateKey()); - JWTVerifier verifier = JWT.require(algorithm).withIssuer(HuahengJwtUtil.HUAHENG_SYSTEM_ID).build(); + JWTVerifier verifier = JWT.require(algorithm).withIssuer(applicationConfig.getArtifactId()).build(); DecodedJWT jwt = verifier.verify(token); - new ApiAuthentication.ApiAuthenticationBuild().operator(jwt.getClaim("operator").asString()).audience(jwt.getAudience().get(0)).issuer(jwt.getIssuer()) - .issuedAt(jwt.getIssuedAt()).expireDateTime(jwt.getExpiresAt()).bulid(); + if (jwt.getClaim("operator").asString().equals(jwt.getIssuer())) { + throw new RuntimeException("Authentication token error"); + } } catch (JWTVerificationException e) { log.error(e.getMessage()); throw e; @@ -99,19 +108,4 @@ public class ApiAuthenticationAspect { // 获取目标方法对象 return clazz.getDeclaredMethod(signature.getName(), signature.getParameterTypes()); } - - /** - * 获取方法类全名+方法名 - * @author TanYibin - * @createDate 2023年2月14日 - * @param method - * @return - */ - private String getClassAndMethodName(Method method) { - // 获取类全名 - String className = method.getDeclaringClass().getName(); - // 获取方法名 - String methodName = method.getName(); - return new StringBuffer(className).append(".").append(methodName).toString(); - } } diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiLoggerAspect.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiLoggerAspect.java index b2ec98f..0b1463b 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiLoggerAspect.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/ApiLoggerAspect.java @@ -25,13 +25,11 @@ import org.jeecg.common.api.vo.Result; import org.jeecg.modules.wms.config.address.service.IAddressService; import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; import org.jeecg.modules.wms.monitor.apiLog.service.IApiLogService; -import org.jeecg.utils.HuahengJwtUtil; import org.jeecg.utils.ServletUtils; import org.jeecg.utils.SpringUtils; import org.jeecg.utils.StringUtils; +import org.jeecg.utils.config.ApplicationConfig; import org.jeecg.utils.support.ApiLogger; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; @@ -40,6 +38,7 @@ import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; import okhttp3.Request; import okhttp3.Response; @@ -47,15 +46,20 @@ import okhttp3.Response; * API调用日志记录处理 * @author huaheng */ +@Slf4j @Aspect @Component @EnableAsync public class ApiLoggerAspect { - private static final Logger log = LoggerFactory.getLogger(ApiLoggerAspect.class); + public static final String HUAHENG_SYSTEM_NAME = "HUAHENG_WMS4"; + private static IApiLogService apiLogService; private static IAddressService addressService; + + @Autowired + private ApplicationConfig applicationConfig; @Autowired public void setApiLogService(IApiLogService apiLogService) { @@ -294,7 +298,7 @@ public class ApiLoggerAspect { String apiName = spList[spList.length - 1]; String ip = JeecgSystemApplication.getLocalHostExactAddress().getHostAddress(); apiLog.setApiName(apiName); - apiLog.setRequestFrom(HuahengJwtUtil.HUAHENG_SYSTEM_ID); + apiLog.setRequestFrom(HUAHENG_SYSTEM_NAME); apiLog.setIp(ip); // Address address = addressService.getAddressByUrl(url.toString(), QuantityConstant.DEFAULT_WAREHOUSE); // apiLog.setResponseBy(address.getParam().toUpperCase()); diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/config/ApplicationConfig.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/config/ApplicationConfig.java new file mode 100644 index 0000000..524a31f --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/config/ApplicationConfig.java @@ -0,0 +1,21 @@ +package org.jeecg.utils.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import lombok.Data; + +@Data +@Component +@ConfigurationProperties(prefix = "huaheng.system") +public class ApplicationConfig { + + /** 版本号 */ + private String version; + + /** 项目号 */ + private String artifactId; + +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ApiAuthentication.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ApiAuthentication.java index 2e73147..d96b53b 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ApiAuthentication.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ApiAuthentication.java @@ -2,10 +2,6 @@ package org.jeecg.utils.support; import java.util.Date; -import org.jeecg.utils.HuahengJwtUtil; - -import cn.hutool.core.date.DatePattern; -import cn.hutool.core.date.DateUtil; import lombok.Data; /** @@ -25,7 +21,7 @@ public class ApiAuthentication { private String audience = "Unknown"; // 观众,相当于接受者 /** Token签发方(WMS) */ - private String issuer = HuahengJwtUtil.HUAHENG_SYSTEM_ID; + private String issuer; /** Token签发时间 */ private Date issuedAt; @@ -65,7 +61,7 @@ public class ApiAuthentication { private String audience; // 观众,相当于接受者 /** Token签发方(WMS) */ - private String issuer = HuahengJwtUtil.HUAHENG_SYSTEM_ID; + private String issuer; /** Token签发时间 */ private Date issuedAt; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ExceptionHandlerAdvice.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ExceptionHandlerAdvice.java new file mode 100644 index 0000000..fd65281 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/ExceptionHandlerAdvice.java @@ -0,0 +1,31 @@ +package org.jeecg.utils.support; + +import java.util.HashMap; +import java.util.Map; + +import org.jeecg.common.api.vo.Result; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ExceptionHandlerAdvice { + + /** + * 直接用方法进行接收参数校验失败 + * @param exception + * @return + */ + @ExceptionHandler(value = MethodArgumentNotValidException.class) + public Object handerConstraintViolationException(MethodArgumentNotValidException exception) { + Result<Map> result = new Result<Map>(); + HashMap<String, Object> errors = new HashMap<>(); + exception.getBindingResult().getAllErrors().forEach(error -> { + FieldError fieldError = (FieldError)error; + errors.put(fieldError.getField(), error.getDefaultMessage()); + }); + + return Result.error("参数校验未通过", errors); + } +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/SystemAuthentication.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/SystemAuthentication.java index 1025aa3..9bd009c 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/SystemAuthentication.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/SystemAuthentication.java @@ -19,13 +19,13 @@ public class SystemAuthentication { private static final ThreadLocal<SystemAuthentication> REQUEST_HEADER_CONTEXT_THREAD_LOCAL = new ThreadLocal<>(); /** Token提供方 */ - private String operator = HuahengJwtUtil.HUAHENG_SYSTEM_ID; + private String operator; /** Token使用方 */ private String audience = "Unknown"; // 观众,相当于接受者 /** Token签发方(WMS) */ - private String issuer = HuahengJwtUtil.HUAHENG_SYSTEM_ID; + private String issuer; /** Token签发时间 */ private Date issuedAt; @@ -65,7 +65,7 @@ public class SystemAuthentication { private String audience; // 观众,相当于接受者 /** Token签发方(WMS) */ - private String issuer = HuahengJwtUtil.HUAHENG_SYSTEM_ID; + private String issuer; /** Token签发时间 */ private Date issuedAt; diff --git a/huaheng-wms-core/src/main/resources/application.yml b/huaheng-wms-core/src/main/resources/application.yml index 66b20ef..a381fbc 100644 --- a/huaheng-wms-core/src/main/resources/application.yml +++ b/huaheng-wms-core/src/main/resources/application.yml @@ -2,8 +2,9 @@ spring: application: name: huaheng-wms-core profiles: - active: test + active: dev huaheng: system: - id: HUAHENG-WMS4 + verson: @project.version@ + artifactId: @project.artifactId@ \ No newline at end of file