Browse Source

登录拦截修改

master
wangchao 6 years ago
parent
commit
87c6165df0
  1. 4
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/enums/ErrorCode.java
  2. 49
      epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java
  3. 4
      epmet-gateway/src/main/resources/bootstrap.yml

4
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/enums/ErrorCode.java

@ -18,9 +18,13 @@ public enum ErrorCode implements IErrorCode {
ERR10003(10003, "token生成失败,请重试。"), ERR10003(10003, "token生成失败,请重试。"),
ERR10004(10004, "返回的Object类型不是EsuaResponse,无法添加token!"), ERR10004(10004, "返回的Object类型不是EsuaResponse,无法添加token!"),
ERR10005(10005, "token不能为空"), ERR10005(10005, "token不能为空"),
ERR10006(10006, "登录超时,请重新登录"),
ERR10007(10007, "当前帐号已在别处登录"),
ERR500(500, "Internal Server Error"), ERR500(500, "Internal Server Error"),
ERR501(501, "参数绑定异常"), ERR501(501, "参数绑定异常"),
ERR401(401, "未授权"),
ERR(ErrorCode.COMMON_ERR_CODE, "其他异常"); ERR(ErrorCode.COMMON_ERR_CODE, "其他异常");

49
epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java

@ -2,11 +2,12 @@
package com.epmet.filter; package com.epmet.filter;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.epmet.common.token.enums.ErrorCode;
import com.epmet.commons.tools.constant.Constant; import com.epmet.commons.tools.constant.Constant;
import com.epmet.commons.tools.exception.ErrorCode;
import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.RenException;
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.jwt.JwtTokenUtils; import com.epmet.jwt.JwtTokenUtils;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -68,7 +69,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
String requestUri = request.getPath().pathWithinApplication().value(); String requestUri = request.getPath().pathWithinApplication().value();
//请求放行,无需验证权限 //请求放行,无需验证权限
if (!pathMatcher(requestUri)) { if (pathMatcher(requestUri)) {
return chain.filter(exchange); return chain.filter(exchange);
} }
logger.info("CpAuthGatewayFilterFactory当前requestUri=[" + requestUri + "]CpAuthGatewayFilterFactory拦截成功"); logger.info("CpAuthGatewayFilterFactory当前requestUri=[" + requestUri + "]CpAuthGatewayFilterFactory拦截成功");
@ -85,16 +86,23 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
logger.info("params token:" + token); logger.info("params token:" + token);
} }
if (StringUtils.isBlank(token)) { if (StringUtils.isBlank(token)) {
return chain.filter(exchange); //return chain.filter(exchange);
//throw new RenException(com.epmet.common.token.enums.ErrorCode.ERR10005.getCode(), com.epmet.common.token.enums.ErrorCode.ERR10005.getMsg());
return response(exchange,new Result<>().error(ErrorCode.ERR10005.getCode(),ErrorCode.ERR10005.getMsg()));
} }
TokenDto user = this.getLoginUserInfo(token); try {
//当前登录用户userId,添加到header中 //当前登录用户userId,添加到header中
if (user != null) { TokenDto user = this.getLoginUserInfo(token);
String redisKey = user.getApp() + "-" + user.getClient() + "-" + user.getUserId(); if (user != null) {
logger.info("redisKey=" + redisKey); String redisKey = user.getApp() + "-" + user.getClient() + "-" + user.getUserId();
ServerHttpRequest build = exchange.getRequest().mutate().header(Constant.APP_USER_KEY, redisKey).build(); logger.info("redisKey=" + redisKey);
return chain.filter(exchange.mutate().request(build).build()); ServerHttpRequest build = exchange.getRequest().mutate().header(Constant.APP_USER_KEY, redisKey).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); return chain.filter(exchange);
}; };
} }
@ -103,7 +111,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
//是否过期 //是否过期
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.UNAUTHORIZED); throw new RenException(ErrorCode.ERR401.getCode(),ErrorCode.ERR401.getMsg());
} }
//获取用户ID //获取用户ID
String app = (String) claims.get("app"); String app = (String) claims.get("app");
@ -112,11 +120,22 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
//查询Redis,如果没数据,则保持用户信息到Redis //查询Redis,如果没数据,则保持用户信息到Redis
TokenDto tokenDto = cpUserDetailRedis.get(app, client, userId); TokenDto tokenDto = cpUserDetailRedis.get(app, client, userId);
if (null == tokenDto) { if (null == tokenDto) {
throw new RenException(ErrorCode.REGION_SUB_DELETE_ERROR, Constant.TOKEN_HEADER); //说明登录状态时效(超时)
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());
}
} }
//过期时间 //过期时间,不延期
long expire = (claims.getExpiration().getTime() - System.currentTimeMillis()) / 1000; //long expire = (claims.getExpiration().getTime() - System.currentTimeMillis()) / 1000;
cpUserDetailRedis.set(tokenDto, expire); //cpUserDetailRedis.set(tokenDto, expire);
return tokenDto; return tokenDto;
} }

4
epmet-gateway/src/main/resources/bootstrap.yml

@ -181,6 +181,7 @@ renren:
- /gov/org/** - /gov/org/**
- /oper/access/** - /oper/access/**
- /sys/** - /sys/**
- /resi/guide/**
management: management:
endpoints: endpoints:
web: web:
@ -241,7 +242,8 @@ epmet:
- /epmetuser/** - /epmetuser/**
- /gov/org/** - /gov/org/**
- /oper/access/** - /oper/access/**
- /resi/guide/** - /resi/guide/stranger/getselectcdcustomergridlist
- /resi/guide/stranger/getlocationcustomergridlist
swaggerUrls: swaggerUrls:
jwt: jwt:

Loading…
Cancel
Save