From d551b175f95005472d650bcd7be483d42fece00c Mon Sep 17 00:00:00 2001 From: wxz Date: Tue, 21 Apr 2020 18:47:42 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=B0=83=E6=95=B4=E6=8B=A6=E6=88=AATokenDto?= =?UTF-8?q?=20Filter=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?BaseTokenDto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/security/dto/BaseTokenDto.java | 42 ++++++++++++++++ .../tools/security/dto/GovTokenDto.java | 41 +++++----------- .../commons/tools/security/dto/TokenDto.java | 22 +-------- .../filter/CpAuthGatewayFilterFactory.java | 48 +++++++++++++------ .../com/epmet/filter/FeignRequestFilter.java | 14 ++++-- .../com/epmet/filter/UserTokenFilter.java | 27 ++++------- 6 files changed, 108 insertions(+), 86 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/BaseTokenDto.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/BaseTokenDto.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/BaseTokenDto.java new file mode 100644 index 0000000000..c3065c7200 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/BaseTokenDto.java @@ -0,0 +1,42 @@ +package com.epmet.commons.tools.security.dto; + +import lombok.Data; + +@Data +public class BaseTokenDto { + /** + * 政府端:gov、居民端:resi、运营端:oper + */ + private String app; + + /** + * PC端:web、微信小程序:wxmp + */ + private String client; + + /** + * 用户ID + */ + private String userId; + + /** + * token字符串 + */ + private String token; + + public BaseTokenDto() { + } + + public BaseTokenDto(String app, String client, String userId) { + this.app = app; + this.client = client; + this.userId = userId; + } + + public BaseTokenDto(String app, String client, String userId, String token) { + this.app = app; + this.client = client; + this.userId = userId; + this.token = token; + } +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/GovTokenDto.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/GovTokenDto.java index 182a6b0576..2a8fd7c05b 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/GovTokenDto.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/GovTokenDto.java @@ -11,21 +11,7 @@ import java.util.List; * @Date 2020/4/20 11:01 */ @Data -public class GovTokenDto implements Serializable { - /** - * 政府端:gov、居民端:resi、运营端:oper - */ - private String app; - - /** - * PC端:web、微信小程序:wxmp - */ - private String client; - - /** - * 用户ID - */ - private String userId; +public class GovTokenDto extends BaseTokenDto implements Serializable { /** * sessionKey @@ -42,21 +28,6 @@ public class GovTokenDto implements Serializable { */ private String unionId; - /** - * token字符串 - */ - private String token; - - /** - * 过期时间戳 - */ - private Long expireTime; - - /** - * 最后一次更新时间 - */ - private long updateTime; - /** * 当前工作人员进入的客户id */ @@ -81,5 +52,15 @@ public class GovTokenDto implements Serializable { * 部门id列表 */ private List deptIdList; + + /** + * 过期时间戳 + */ + private Long expireTime; + + /** + * 最后一次更新时间 + */ + private long updateTime; } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/TokenDto.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/TokenDto.java index fb8a5dd7ec..402dfc1b59 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/TokenDto.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/TokenDto.java @@ -11,22 +11,8 @@ import java.io.Serializable; * @Date 2020-03-14 */ @Data -public class TokenDto implements Serializable { +public class TokenDto extends BaseTokenDto implements Serializable { private static final long serialVersionUID = 8883581762088390769L; - /** - * 政府端:gov、居民端:resi、运营端:oper - */ - private String app; - - /** - * PC端:web、微信小程序:wxmp - */ - private String client; - - /** - * 用户ID - */ - private String userId; /** * sessionKey @@ -43,11 +29,6 @@ public class TokenDto implements Serializable { */ private String unionId; - /** - * token字符串 - */ - private String token; - /** * 过期时间戳 */ @@ -57,4 +38,5 @@ public class TokenDto implements Serializable { * 最后一次更新时间 */ private long updateTime; + } diff --git a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java index e398b5978a..c4687b2ea6 100644 --- a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java +++ b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java @@ -6,6 +6,7 @@ import com.epmet.common.token.enums.ErrorCode; import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.constant.Constant; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.security.dto.BaseTokenDto; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.CpUserDetailRedis; import com.epmet.commons.tools.utils.Result; @@ -91,24 +92,22 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory().error(ErrorCode.ERR10005.getCode(),ErrorCode.ERR10005.getMsg())); } try { + BaseTokenDto baseTokenDto = getBaseTokenDto(token, jwtTokenUtils); + validateTokenDto(baseTokenDto, token); + //当前登录用户userId,添加到header中 - TokenDto user = getLoginUserInfoByToken(token, jwtTokenUtils, cpUserDetailRedis); - if (user != null) { - String redisKey = user.getApp() + "-" + user.getClient() + "-" + user.getUserId(); - logger.info("redisKey=" + redisKey); - ServerHttpRequest build = exchange.getRequest().mutate() - .header(Constant.APP_USER_KEY, redisKey) - .header(AppClientConstant.APP,user.getApp()) - .header(AppClientConstant.CLIENT,user.getClient()) - .header(AppClientConstant.USER_ID,user.getUserId()) - .build(); - return chain.filter(exchange.mutate().request(build).build()); - } + String redisKey = baseTokenDto.getApp() + "-" + baseTokenDto.getClient() + "-" + baseTokenDto.getUserId(); + logger.info("redisKey=" + redisKey); + ServerHttpRequest build = exchange.getRequest().mutate() + .header(Constant.APP_USER_KEY, redisKey) + .header(AppClientConstant.APP,baseTokenDto.getApp()) + .header(AppClientConstant.CLIENT,baseTokenDto.getClient()) + .header(AppClientConstant.USER_ID,baseTokenDto.getUserId()) + .build(); + return chain.filter(exchange.mutate().request(build).build()); }catch(RenException e){ return response(exchange,new Result<>().error(e.getCode(),e.getMsg())); } - - return chain.filter(exchange); }; } @@ -182,4 +181,25 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory T getLoginUserInfoByToken(String token, JwtTokenUtils jwtTokenUtils, CpUserDetailRedis cpUserDetailRedis, Class clz) { + BaseTokenDto baseTokenDto = getBaseTokenDto(token, jwtTokenUtils); + //查询Redis + T tokenDto = (T)cpUserDetailRedis.get(baseTokenDto.getApp(), baseTokenDto.getClient(), baseTokenDto.getUserId()); return tokenDto; }