Browse Source

1.调整拦截TokenDto Filter的逻辑,增加BaseTokenDto

dev_shibei_match
wxz 5 years ago
parent
commit
d551b175f9
  1. 42
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/BaseTokenDto.java
  2. 41
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/GovTokenDto.java
  3. 22
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/security/dto/TokenDto.java
  4. 48
      epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java
  5. 14
      epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java
  6. 27
      epmet-gateway/src/main/java/com/epmet/filter/UserTokenFilter.java

42
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;
}
}

41
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 * @Date 2020/4/20 11:01
*/ */
@Data @Data
public class GovTokenDto implements Serializable { public class GovTokenDto extends BaseTokenDto implements Serializable {
/**
* 政府端:gov居民端:resi运营端:oper
*/
private String app;
/**
* PC端:web微信小程序:wxmp
*/
private String client;
/**
* 用户ID
*/
private String userId;
/** /**
* sessionKey * sessionKey
@ -42,21 +28,6 @@ public class GovTokenDto implements Serializable {
*/ */
private String unionId; private String unionId;
/**
* token字符串
*/
private String token;
/**
* 过期时间戳
*/
private Long expireTime;
/**
* 最后一次更新时间
*/
private long updateTime;
/** /**
* 当前工作人员进入的客户id * 当前工作人员进入的客户id
*/ */
@ -81,5 +52,15 @@ public class GovTokenDto implements Serializable {
* 部门id列表 * 部门id列表
*/ */
private List<String> deptIdList; private List<String> deptIdList;
/**
* 过期时间戳
*/
private Long expireTime;
/**
* 最后一次更新时间
*/
private long updateTime;
} }

22
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 * @Date 2020-03-14
*/ */
@Data @Data
public class TokenDto implements Serializable { public class TokenDto extends BaseTokenDto implements Serializable {
private static final long serialVersionUID = 8883581762088390769L; private static final long serialVersionUID = 8883581762088390769L;
/**
* 政府端:gov居民端:resi运营端:oper
*/
private String app;
/**
* PC端:web微信小程序:wxmp
*/
private String client;
/**
* 用户ID
*/
private String userId;
/** /**
* sessionKey * sessionKey
@ -43,11 +29,6 @@ public class TokenDto implements Serializable {
*/ */
private String unionId; private String unionId;
/**
* token字符串
*/
private String token;
/** /**
* 过期时间戳 * 过期时间戳
*/ */
@ -57,4 +38,5 @@ public class TokenDto implements Serializable {
* 最后一次更新时间 * 最后一次更新时间
*/ */
private long updateTime; private long updateTime;
} }

48
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.AppClientConstant;
import com.epmet.commons.tools.constant.Constant; import com.epmet.commons.tools.constant.Constant;
import com.epmet.commons.tools.exception.RenException; 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.security.dto.TokenDto;
import com.epmet.commons.tools.utils.CpUserDetailRedis; import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.Result;
@ -91,24 +92,22 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
return response(exchange,new Result<>().error(ErrorCode.ERR10005.getCode(),ErrorCode.ERR10005.getMsg())); return response(exchange,new Result<>().error(ErrorCode.ERR10005.getCode(),ErrorCode.ERR10005.getMsg()));
} }
try { try {
BaseTokenDto baseTokenDto = getBaseTokenDto(token, jwtTokenUtils);
validateTokenDto(baseTokenDto, token);
//当前登录用户userId,添加到header中 //当前登录用户userId,添加到header中
TokenDto user = getLoginUserInfoByToken(token, jwtTokenUtils, cpUserDetailRedis); String redisKey = baseTokenDto.getApp() + "-" + baseTokenDto.getClient() + "-" + baseTokenDto.getUserId();
if (user != null) { logger.info("redisKey=" + redisKey);
String redisKey = user.getApp() + "-" + user.getClient() + "-" + user.getUserId(); ServerHttpRequest build = exchange.getRequest().mutate()
logger.info("redisKey=" + redisKey); .header(Constant.APP_USER_KEY, redisKey)
ServerHttpRequest build = exchange.getRequest().mutate() .header(AppClientConstant.APP,baseTokenDto.getApp())
.header(Constant.APP_USER_KEY, redisKey) .header(AppClientConstant.CLIENT,baseTokenDto.getClient())
.header(AppClientConstant.APP,user.getApp()) .header(AppClientConstant.USER_ID,baseTokenDto.getUserId())
.header(AppClientConstant.CLIENT,user.getClient()) .build();
.header(AppClientConstant.USER_ID,user.getUserId()) return chain.filter(exchange.mutate().request(build).build());
.build();
return chain.filter(exchange.mutate().request(build).build());
}
}catch(RenException e){ }catch(RenException e){
return response(exchange,new Result<>().error(e.getCode(),e.getMsg())); return response(exchange,new Result<>().error(e.getCode(),e.getMsg()));
} }
return chain.filter(exchange);
}; };
} }
@ -182,4 +181,25 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
this.enabled = enabled; this.enabled = enabled;
} }
} }
/**
* 校验Token是否异常
* @param tokenDto
* @param tokenStr
*/
public void validateTokenDto(BaseTokenDto tokenDto, String tokenStr) {
if (null == tokenDto) {
//说明登录状态时效(超时)
throw new RenException(ErrorCode.ERR10006.getCode(),ErrorCode.ERR10006.getMsg());
}else{
//Redis中存在数据,取出token,进行比对
if(StringUtils.equals(tokenDto.getToken(),tokenStr)){
//用户携带token与Redis中一致
}else{
//用户携带token与Redis中不一致,说明当前用户此次会话失效,提示重新登陆
throw new RenException(ErrorCode.ERR10007.getCode(),ErrorCode.ERR10007.getMsg());
}
}
}
} }

14
epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java

@ -1,9 +1,14 @@
package com.epmet.filter; package com.epmet.filter;
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.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.security.dto.TokenDto;
import com.epmet.commons.tools.utils.CpUserDetailRedis; import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.jwt.JwtTokenUtils; import com.epmet.jwt.JwtTokenUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -18,7 +23,7 @@ import reactor.core.publisher.Mono;
/** /**
* Feign调用发送请求的Filter * Feign调用发送请求的Filter
* 目前用于封装用户相关信息到request供上游微服务使用 * 目前用于封装用户相关信息到request供上游微服务使用
*/ */
@Component @Component
public class FeignRequestFilter implements GlobalFilter, UserTokenFilter { public class FeignRequestFilter implements GlobalFilter, UserTokenFilter {
@ -50,10 +55,11 @@ public class FeignRequestFilter implements GlobalFilter, UserTokenFilter {
return chain.filter(exchange); return chain.filter(exchange);
} }
TokenDto loginUserInfo = getLoginUserInfoByToken(token, jwtTokenUtils, cpUserDetailRedis); BaseTokenDto baseTokenDto = getBaseTokenDto(token, jwtTokenUtils);
if (loginUserInfo != null) {
if (baseTokenDto != null) {
ServerHttpRequest build = exchange.getRequest().mutate() ServerHttpRequest build = exchange.getRequest().mutate()
.header(Constant.USER_KEY, new String[]{loginUserInfo.getUserId()}).build(); .header(Constant.USER_KEY, new String[]{baseTokenDto.getUserId()}).build();
return chain.filter(exchange.mutate().request(build).build()); return chain.filter(exchange.mutate().request(build).build());
} }

27
epmet-gateway/src/main/java/com/epmet/filter/UserTokenFilter.java

@ -2,6 +2,7 @@ package com.epmet.filter;
import com.epmet.common.token.enums.ErrorCode; import com.epmet.common.token.enums.ErrorCode;
import com.epmet.commons.tools.exception.RenException; 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.security.dto.TokenDto;
import com.epmet.commons.tools.utils.CpUserDetailRedis; import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.jwt.JwtTokenUtils; import com.epmet.jwt.JwtTokenUtils;
@ -12,33 +13,23 @@ import io.jsonwebtoken.Claims;
*/ */
public interface UserTokenFilter { public interface UserTokenFilter {
default TokenDto getLoginUserInfoByToken(String token, JwtTokenUtils jwtTokenUtils, CpUserDetailRedis cpUserDetailRedis) { default BaseTokenDto getBaseTokenDto(String token, JwtTokenUtils jwtTokenUtils) {
//是否过期 //是否过期
Claims claims = jwtTokenUtils.getClaimByToken(token); Claims claims = jwtTokenUtils.getClaimByToken(token);
if (claims == null || jwtTokenUtils.isTokenExpired(claims.getExpiration())) { if (claims == null || jwtTokenUtils.isTokenExpired(claims.getExpiration())) {
throw new RenException(ErrorCode.ERR401.getCode(),ErrorCode.ERR401.getMsg()); throw new RenException(ErrorCode.ERR401.getCode(), ErrorCode.ERR401.getMsg());
} }
//获取用户ID //获取用户ID
String app = (String) claims.get("app"); String app = (String) claims.get("app");
String client = (String) claims.get("client"); String client = (String) claims.get("client");
String userId = (String) claims.get("userId"); String userId = (String) claims.get("userId");
//查询Redis return new BaseTokenDto(app, client, userId, token);
TokenDto tokenDto = cpUserDetailRedis.get(app, client, userId); }
//if (null == tokenDto) {
// //说明登录状态时效(超时)
// throw new RenException(ErrorCode.ERR10006.getCode(),ErrorCode.ERR10006.getMsg());
//}else{
// //Redis中存在数据,取出token,进行比对
// if(StringUtils.equals(tokenDto.getToken(),token)){
// //用户携带token与Redis中一致
//
// }else{
// //用户携带token与Redis中不一致,说明当前用户此次会话失效,提示重新登陆
// throw new RenException(ErrorCode.ERR10007.getCode(),ErrorCode.ERR10007.getMsg());
// }
//
//}
default <T> T getLoginUserInfoByToken(String token, JwtTokenUtils jwtTokenUtils, CpUserDetailRedis cpUserDetailRedis, Class<T> clz) {
BaseTokenDto baseTokenDto = getBaseTokenDto(token, jwtTokenUtils);
//查询Redis
T tokenDto = (T)cpUserDetailRedis.get(baseTokenDto.getApp(), baseTokenDto.getClient(), baseTokenDto.getUserId());
return tokenDto; return tokenDto;
} }

Loading…
Cancel
Save