diff --git a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/form/GetJwtAccessTokenFormDTO.java b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/form/GetJwtAccessTokenFormDTO.java new file mode 100644 index 0000000000..47577b2787 --- /dev/null +++ b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/form/GetJwtAccessTokenFormDTO.java @@ -0,0 +1,13 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +@Data +public class GetJwtAccessTokenFormDTO { + + @NotBlank(message = "AppId不能为空") + private String appId; + +} diff --git a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/GetJwtAccessTokenResultDTO.java b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/GetJwtAccessTokenResultDTO.java new file mode 100644 index 0000000000..58cf9ecc6f --- /dev/null +++ b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/GetJwtAccessTokenResultDTO.java @@ -0,0 +1,16 @@ +package com.epmet.dto.result; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class GetJwtAccessTokenResultDTO { + + private String customerId; + private String token; + private Long ts; + +} diff --git a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/feign/EpmetCommonServiceOpenFeignClient.java b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/feign/EpmetCommonServiceOpenFeignClient.java index 7ed9ff3f18..cd93b5eca6 100644 --- a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/feign/EpmetCommonServiceOpenFeignClient.java +++ b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/feign/EpmetCommonServiceOpenFeignClient.java @@ -3,6 +3,7 @@ package com.epmet.feign; import com.epmet.commons.tools.constant.ServiceConstant; import com.epmet.commons.tools.utils.Result; import com.epmet.dto.form.ExternalAppAuthFormDTO; +import com.epmet.dto.form.GetJwtAccessTokenFormDTO; import com.epmet.dto.form.WorkDayFormDTO; import com.epmet.dto.result.ExternalAppAuthResultDTO; import com.epmet.dto.result.WorkDayResultDTO; @@ -53,4 +54,13 @@ public interface EpmetCommonServiceOpenFeignClient { */ @PostMapping("/commonservice/externalapp/get-secret") Result getSecret(@RequestBody String appId); + + /** + * @Description 获取AccessToken + * @return Result + * @author wxz + * @date 2020.10.22 10:19 + */ + @PostMapping("/commonservice/externalapp/get-jwt-accesstoken") + Result getAccessToken(@RequestBody GetJwtAccessTokenFormDTO form); } diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalAppController.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalAppController.java index f1229c7902..dc703b0c4c 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalAppController.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalAppController.java @@ -8,8 +8,10 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.ExternalAppAuthFormDTO; import com.epmet.dto.form.ExternalAppFormDTO; +import com.epmet.dto.form.GetJwtAccessTokenFormDTO; import com.epmet.dto.result.ExternalAppAuthResultDTO; import com.epmet.dto.result.ExternalAppResultDTO; +import com.epmet.dto.result.GetJwtAccessTokenResultDTO; import com.epmet.service.ExternalAppAuthService; import com.epmet.service.ExternalAppSecretService; import com.epmet.service.ExternalAppService; @@ -146,4 +148,18 @@ public class ExternalAppController { return new Result().ok(secret); } + /** + * @Description 获取AccessToken + * @return Result + * @author wxz + * @date 2020.10.22 10:19 + */ + @PostMapping("get-jwt-accesstoken") + public Result getAccessToken(@RequestBody GetJwtAccessTokenFormDTO form) { + ValidatorUtils.validateEntity(form); + String appId = form.getAppId(); + GetJwtAccessTokenResultDTO jwtAccessToken = externalAppService.getJwtAccessToken(appId); + return new Result().ok(jwtAccessToken); + } + } diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalAppService.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalAppService.java index 4d5d847e68..4590ae6863 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalAppService.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalAppService.java @@ -19,6 +19,7 @@ package com.epmet.service; import com.epmet.commons.tools.page.PageData; import com.epmet.dto.result.ExternalAppResultDTO; +import com.epmet.dto.result.GetJwtAccessTokenResultDTO; import java.util.List; @@ -38,4 +39,6 @@ public interface ExternalAppService { List getCustomerIds(); String resetSecret(String appId); + + GetJwtAccessTokenResultDTO getJwtAccessToken(String appId); } \ No newline at end of file diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalAppServiceImpl.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalAppServiceImpl.java index db3267c600..8bfeb7474f 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalAppServiceImpl.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalAppServiceImpl.java @@ -28,11 +28,13 @@ import com.epmet.dao.ExternalAppSecretDao; import com.epmet.dao.ExternalCustomerDao; import com.epmet.dto.CustomerDTO; import com.epmet.dto.result.ExternalAppResultDTO; +import com.epmet.dto.result.GetJwtAccessTokenResultDTO; import com.epmet.entity.ExternalAppEntity; import com.epmet.entity.ExternalAppSecretEntity; import com.epmet.enu.CustomerTypeEnum; import com.epmet.feign.OperCrmOpenFeignClient; import com.epmet.service.ExternalAppService; +import com.epmet.utils.externalapp.ExtAppJwtTokenUtils; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.slf4j.Logger; @@ -70,6 +72,9 @@ public class ExternalAppServiceImpl implements ExternalAppService { @Autowired private RedisUtils redisUtils; + @Autowired + private ExtAppJwtTokenUtils tokenUtils; + @Transactional @Override public ExternalAppResultDTO add(String appName, String customerId, String customerType) { @@ -197,4 +202,14 @@ public class ExternalAppServiceImpl implements ExternalAppService { return null; } + @Override + public GetJwtAccessTokenResultDTO getJwtAccessToken(String appId) { + ExternalAppEntity externalAppEntity = externalAppDao.selectById(appId); + String customerId = externalAppEntity.getCustomerId(); + String secret = externalAppSecretDao.getSecretByAppId(appId); + long ts = System.currentTimeMillis(); + + String token = tokenUtils.genToken(secret, appId, customerId, ts); + return new GetJwtAccessTokenResultDTO(customerId, token, ts); + } } \ No newline at end of file diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/utils/externalapp/ExtAppJwtTokenUtils.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/utils/externalapp/ExtAppJwtTokenUtils.java index b945889b7c..147c3b540c 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/utils/externalapp/ExtAppJwtTokenUtils.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/utils/externalapp/ExtAppJwtTokenUtils.java @@ -49,7 +49,7 @@ public class ExtAppJwtTokenUtils { } } - public String createToken(Map map, String secret) { + private String createToken(Map map, String secret) { return Jwts.builder() .setHeaderParam("typ", "JWT") .setClaims(map) @@ -59,21 +59,7 @@ public class ExtAppJwtTokenUtils { .compact(); } -// /** -// * token是否过期 -// * -// * @return true:过期 -// */ -// public boolean isTokenExpired(Date expiration) { -// return expiration.before(new Date()); -// } - - public static void main(String[] args) { - genToken(); -// getClaim(); - } - - public static void genToken() { + public static void genTestToken() { HashMap claim = new HashMap<>(); //市北 //String secret = "612d304095c50369c3ef06e490f05779eeb8f19ff16566c73aeafafc5fa01970"; @@ -89,21 +75,19 @@ public class ExtAppJwtTokenUtils { String appId = "f358d63a89f3670c197c62ca4c3a0366"; String customrId = "45687aa479955f9d06204d415238f7cc"; - claim.put("customerId", customrId); - claim.put("appId", appId); long ts = System.currentTimeMillis() + 1000 * 60 * 1; System.out.println("时间戳:" + ts); - claim.put("ts", ts); - - String abc = new ExtAppJwtTokenUtils().createToken(claim, secret); - System.out.println(abc); + String token = new ExtAppJwtTokenUtils().genToken(secret, appId, customrId, ts); + System.out.println(token); } - public static void getClaim() { - String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhcHBJZCI6IjEiLCJjdXN0b21lcklkIjoiYzEiLCJpYXQiOjE1OTc3NDI2NTB9.09Vop0Nobg3LENAJoAZaCUKtgAjADAK48BS11ky3YdAp6h-cXYtGeqUxbgvE_4F6239rc7UE2fjxtEvMuWEJuA"; + public String genToken(String secret, String appId, String customrId, Long ts) { + HashMap claim = new HashMap<>(); + claim.put("customerId", customrId); + claim.put("appId", appId); + claim.put("ts", ts); - Claims claimByToken = new ExtAppJwtTokenUtils().getClaimByToken(token, "4a762660254c57996343f8ee42fbc0a6"); - System.out.println(claimByToken); + return createToken(claim, secret); } }