From 84ec95c23781c552e5fcb2afc4f75908bd41d652 Mon Sep 17 00:00:00 2001 From: jianjun Date: Mon, 10 Jan 2022 12:25:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=82=E5=8C=97=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- epmet-auth-client/pom.xml | 10 ++++- .../auth/feign/EpmetAuthOpenFeignClient.java | 32 ++++++++++++++ .../EpmetAuthOpenFeignClientFallback.java | 20 +++++++++ ...metAuthOpenFeignClientFallbackFactory.java | 20 +++++++++ .../epmet/controller/GovWebController.java | 24 +++++++++-- .../java/com/epmet/service/GovWebService.java | 8 ++++ .../epmet/service/impl/GovWebServiceImpl.java | 43 +++++++++++++++++++ .../epmet/commons/tools/redis/RedisKeys.java | 11 +++++ epmet-gateway/pom.xml | 14 ++++-- .../com/epmet/auth/InternalAuthProcessor.java | 22 ++++++++++ 10 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 epmet-auth-client/src/main/java/com/epmet/auth/feign/EpmetAuthOpenFeignClient.java create mode 100644 epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallback.java create mode 100644 epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallbackFactory.java diff --git a/epmet-auth-client/pom.xml b/epmet-auth-client/pom.xml index 1aee53c24e..cdb0519c0d 100644 --- a/epmet-auth-client/pom.xml +++ b/epmet-auth-client/pom.xml @@ -10,5 +10,13 @@ 4.0.0 epmet-auth-client + + + com.epmet + epmet-commons-tools + 2.0.0 + compile + + - \ No newline at end of file + diff --git a/epmet-auth-client/src/main/java/com/epmet/auth/feign/EpmetAuthOpenFeignClient.java b/epmet-auth-client/src/main/java/com/epmet/auth/feign/EpmetAuthOpenFeignClient.java new file mode 100644 index 0000000000..42ba7fb3bb --- /dev/null +++ b/epmet-auth-client/src/main/java/com/epmet/auth/feign/EpmetAuthOpenFeignClient.java @@ -0,0 +1,32 @@ +package com.epmet.auth.feign; + +import com.epmet.auth.feign.fallback.EpmetAuthOpenFeignClientFallbackFactory; +import com.epmet.commons.tools.constant.ServiceConstant; +import com.epmet.commons.tools.utils.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * 本服务对外开放的API,其他服务通过引用此client调用该服务 + * + * @author yinzuomei@elink-cn.com + * @date 2020/6/4 13:25 + */ +//@FeignClient(name = ServiceConstant.EPMET_AUTH_SERVER, fallbackFactory = EpmetAuthOpenFeignClientFallbackFactory.class) +@FeignClient(name = ServiceConstant.EPMET_AUTH_SERVER, fallbackFactory = EpmetAuthOpenFeignClientFallbackFactory.class, url = "http://localhost:8081") +public interface EpmetAuthOpenFeignClient { + + /** + * @Description 根据客户Id查询志愿者用户Id集合 + * @param uuid + * @param userId + * @param customerId + * @return + * @author wangc + * @date 2020.08.13 10:22 + **/ + @PostMapping("/auth/govweb/generateTokenBySSOKey/{uuid}/{userId}") + Result generateTokenBySSOKey(@PathVariable(value = "uuid")String uuid, @PathVariable String userId, @RequestParam String customerId); +} diff --git a/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallback.java b/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallback.java new file mode 100644 index 0000000000..8760185fb4 --- /dev/null +++ b/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallback.java @@ -0,0 +1,20 @@ +package com.epmet.auth.feign.fallback; + +import com.epmet.auth.feign.EpmetAuthOpenFeignClient; +import com.epmet.commons.tools.constant.ServiceConstant; +import com.epmet.commons.tools.utils.ModuleUtils; +import com.epmet.commons.tools.utils.Result; + +/** + * 本服务对外开放的API,其他服务通过引用此client调用该服务 + * + * @author yinzuomei@elink-cn.com + * @date 2020/6/4 13:26 + */ +public class EpmetAuthOpenFeignClientFallback implements EpmetAuthOpenFeignClient { + @Override + public Result generateTokenBySSOKey(String uuid, String userId, String customerId) { + return ModuleUtils.feignConError(ServiceConstant.EPMET_AUTH_SERVER, "generateTokenBySSOKey", uuid, userId,customerId); + } + +} diff --git a/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallbackFactory.java b/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallbackFactory.java new file mode 100644 index 0000000000..728ba4072b --- /dev/null +++ b/epmet-auth-client/src/main/java/com/epmet/auth/feign/fallback/EpmetAuthOpenFeignClientFallbackFactory.java @@ -0,0 +1,20 @@ +package com.epmet.auth.feign.fallback; + +import com.epmet.auth.feign.EpmetAuthOpenFeignClient; +import com.epmet.commons.tools.exception.ExceptionUtils; +import feign.hystrix.FallbackFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class EpmetAuthOpenFeignClientFallbackFactory implements FallbackFactory { + + private EpmetAuthOpenFeignClientFallback fallback = new EpmetAuthOpenFeignClientFallback(); + + @Override + public EpmetAuthOpenFeignClient create(Throwable cause) { + log.error(String.format("FeignClient调用发生异常,异常信息:%s", ExceptionUtils.getThrowableErrorStackTrace(cause))); + return fallback; + } +} diff --git a/epmet-auth/src/main/java/com/epmet/controller/GovWebController.java b/epmet-auth/src/main/java/com/epmet/controller/GovWebController.java index 8a85cb9da4..6ed1a5761f 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/GovWebController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/GovWebController.java @@ -6,18 +6,17 @@ import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.GovWebLoginFormDTO; import com.epmet.dto.result.UserTokenResultDTO; import com.epmet.service.GovWebService; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -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.RestController; +import org.springframework.web.bind.annotation.*; /** * @author sun * @Description PC工作端-登陆服务 */ +@Slf4j @RestController @RequestMapping("govweb") public class GovWebController { @@ -63,5 +62,22 @@ public class GovWebController { return new Result().ok(publicKey); } + /** + * desc: 根据用户id + * + * @return com.epmet.commons.tools.utils.Result + * @author LiuJanJun + * @date 2021/3/8 5:07 下午 + */ + @PostMapping("generateTokenBySSOKey/{uuid}/{userId}") + public Result generateTokenBySSOKey(@PathVariable(value = "uuid")String uuid,@PathVariable String userId,@RequestParam String customerId) { + //判断是否非法登陆 + /*if (!redisUtils.hasKey(RedisKeys.getIcLoginAuthKey(customerId,uuid))){ + log.error("有人使用userid:{} 非法登陆",userId); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode()); + }*/ + return new Result().ok(govWebService.generateTokenBySSOKey(customerId,userId)); + } + } diff --git a/epmet-auth/src/main/java/com/epmet/service/GovWebService.java b/epmet-auth/src/main/java/com/epmet/service/GovWebService.java index 30f8d8ae4c..79ee6d0adc 100644 --- a/epmet-auth/src/main/java/com/epmet/service/GovWebService.java +++ b/epmet-auth/src/main/java/com/epmet/service/GovWebService.java @@ -16,4 +16,12 @@ public interface GovWebService { * @Description PC工作端-工作人员登录 **/ UserTokenResultDTO login(GovWebLoginFormDTO formDTO); + + /** + * desc:根据用户Id 生成token + * @return + * @param customerId + * @param userId + */ + String generateTokenBySSOKey(String customerId, String userId); } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/GovWebServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/GovWebServiceImpl.java index 9d455af694..e7eea09292 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/GovWebServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/GovWebServiceImpl.java @@ -1,8 +1,11 @@ package com.epmet.service.impl; import com.epmet.common.token.constant.LoginConstant; +import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.EpmetException; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.redis.common.CustomerStaffRedis; import com.epmet.commons.tools.security.dto.GovTokenDto; import com.epmet.commons.tools.security.password.PasswordUtils; import com.epmet.commons.tools.utils.CpUserDetailRedis; @@ -94,6 +97,46 @@ public class GovWebServiceImpl implements GovWebService { } + @Override + public String generateTokenBySSOKey(String customerId, String userId) { + CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(customerId, userId); + if (staffInfo == null){ + log.error("工作人员信息不存在,customerId:{},userId:{}", customerId, userId); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode()); + } + + + // 生成token + Map map = new HashMap<>(); + map.put("app", "gov"); + map.put("client", "web"); + map.put("userId", userId); + String token = jwtTokenUtils.createToken(map); + int expire = jwtTokenProperties.getExpire(); + + String orgIdPath = thirdLoginService.getOrgIdPath(userId); + String[] orgIdPathParts = orgIdPath.split(":"); + + GovTokenDto tokenDto = new GovTokenDto(); + tokenDto.setCustomerId(customerId); + tokenDto.setApp("gov"); + tokenDto.setClient("web"); + tokenDto.setUserId(userId); + tokenDto.setToken(token); + tokenDto.setUpdateTime(System.currentTimeMillis()); + tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); + tokenDto.setAgencyId(staffInfo.getAgencyId()); + tokenDto.setRootAgencyId(staffInfo.getAgencyId()); + //tokenDto.setDeptIdList(thirdLoginService.getDeptartmentIdList(userId)); + //tokenDto.setGridIdList(thirdLoginService.getGridIdList(userId)); + //tokenDto.setRoleList(thirdLoginService.queryGovStaffRoles(userId, orgIdPathParts[orgIdPathParts.length - 1])); + //tokenDto.setOrgIdPath(orgIdPath); + + cpUserDetailRedis.set(tokenDto, expire); + logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); + return token; + } + /** * 生成PC工作端token * @author sun diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index f3c669d7cd..dd764b13fe 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java @@ -636,4 +636,15 @@ public class RedisKeys { public static String getOrgTreeCacheKey(String agencyId) { return rootPrefix.concat("org:temp:orgtree").concat(agencyId); } + + /** + * desc:获取第三方登陆跳转 授权key + * + * @param customerId + * @param uuid + * @return + */ + public static String getIcLoginAuthKey(String customerId, String uuid) { + return rootPrefix.concat("sys:iclogin:authkey:").concat(customerId).concat(StrConstant.COLON).concat(uuid); + } } diff --git a/epmet-gateway/pom.xml b/epmet-gateway/pom.xml index 1169c9ca84..f0833bc954 100644 --- a/epmet-gateway/pom.xml +++ b/epmet-gateway/pom.xml @@ -77,6 +77,12 @@ epmet-admin-client 2.0.0 + + com.epmet + epmet-auth-client + 2.0.0 + compile + @@ -129,8 +135,8 @@ - - lb://epmet-auth-server + http://localhost:8081 + lb://epmet-admin-server @@ -266,8 +272,8 @@ false - - lb://epmet-auth-server + http://localhost:8081 + lb://epmet-admin-server diff --git a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java index 305bf2b3a4..946c5d1907 100644 --- a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java +++ b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java @@ -1,11 +1,15 @@ package com.epmet.auth; +import com.alibaba.fastjson.JSON; +import com.epmet.auth.feign.EpmetAuthOpenFeignClient; import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.constant.Constant; +import com.epmet.commons.tools.constant.CustomerIdConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.security.dto.BaseTokenDto; import com.epmet.commons.tools.utils.CpUserDetailRedis; +import com.epmet.commons.tools.utils.Result; import com.epmet.filter.CpProperty; import com.epmet.jwt.JwtTokenUtils; import io.jsonwebtoken.Claims; @@ -40,6 +44,8 @@ public class InternalAuthProcessor extends AuthProcessor { @Autowired private CpProperty cpProperty; + @Autowired + private EpmetAuthOpenFeignClient epmetAuthOpenFeignClient; @Override public ServerWebExchange auth(ServerWebExchange exchange, GatewayFilterChain chain) { @@ -70,6 +76,22 @@ public class InternalAuthProcessor extends AuthProcessor { userId = (String) claims.get(AppClientConstant.USER_ID); expiration = claims.getExpiration(); baseTokenDto = cpUserDetailRedis.get(app, client, userId, BaseTokenDto.class); + //市北数字社区 如果redis里不存在 则自动登陆 生成token放入redis + if (baseTokenDto == null){ + Result stringResult = epmetAuthOpenFeignClient.generateTokenBySSOKey("123", userId, CustomerIdConstant.SHI_BEI_CUSTOMER_ID); + if (stringResult != null && stringResult.success() && StringUtils.isNotBlank(stringResult.getData())){ + baseTokenDto = new BaseTokenDto(); + baseTokenDto.setApp(app); + baseTokenDto.setClient(client); + baseTokenDto.setUserId(userId); + String tokenNew = stringResult.getData(); + //把传过来的token用新的token 替换 + token = tokenNew; + baseTokenDto.setToken(tokenNew); + baseTokenDto.setCustomerId(CustomerIdConstant.SHI_BEI_CUSTOMER_ID); + } + logger.info("stringResult"+ JSON.toJSONString(stringResult)); + } } }