forked from luyan/epmet-cloud-lingshan
7 changed files with 186 additions and 47 deletions
@ -0,0 +1,33 @@ |
|||||
|
package com.epmet.commons.tools.security.user; |
||||
|
|
||||
|
import com.epmet.commons.tools.constant.Constant; |
||||
|
import com.epmet.commons.tools.utils.HttpContextUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.web.context.request.RequestAttributes; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
|
||||
|
/** |
||||
|
* 登录用户相关工具 |
||||
|
*/ |
||||
|
public class LoginUserUtil { |
||||
|
|
||||
|
/** |
||||
|
* 查询登录用户的id |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getLoginUserId() { |
||||
|
HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); |
||||
|
if (request == null) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
String userId = request.getHeader(Constant.USER_KEY); |
||||
|
if (StringUtils.isBlank(userId)) { |
||||
|
return null; |
||||
|
} |
||||
|
return userId; |
||||
|
} |
||||
|
} |
@ -0,0 +1,62 @@ |
|||||
|
package com.epmet.filter; |
||||
|
|
||||
|
import com.epmet.commons.tools.constant.Constant; |
||||
|
import com.epmet.commons.tools.security.dto.TokenDto; |
||||
|
import com.epmet.commons.tools.utils.CpUserDetailRedis; |
||||
|
import com.epmet.jwt.JwtTokenUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter; |
||||
|
import org.springframework.http.HttpHeaders; |
||||
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.server.ServerWebExchange; |
||||
|
import reactor.core.publisher.Mono; |
||||
|
|
||||
|
/** |
||||
|
* Feign调用发送请求的Filter |
||||
|
* 目前用于封装用户相关信息到request,供上游微服务使用 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class FeignRequestFilter implements GlobalFilter, UserTokenFilter { |
||||
|
|
||||
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
|
||||
|
@Autowired |
||||
|
private JwtTokenUtils jwtTokenUtils; |
||||
|
@Autowired |
||||
|
private CpUserDetailRedis cpUserDetailRedis; |
||||
|
|
||||
|
@Override |
||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
||||
|
ServerHttpRequest request = exchange.getRequest(); |
||||
|
HttpHeaders headers = request.getHeaders(); |
||||
|
String token = headers.getFirst(Constant.AUTHORIZATION_HEADER); |
||||
|
if (StringUtils.isBlank(token)) { |
||||
|
token = headers.getFirst(Constant.TOKEN_HEADER); |
||||
|
logger.info("token=" + token); |
||||
|
} else { |
||||
|
logger.info("authorization=" + token); |
||||
|
} |
||||
|
if (StringUtils.isBlank(token)) { |
||||
|
token = request.getQueryParams().getFirst(Constant.AUTHORIZATION_HEADER); |
||||
|
logger.info("params token:" + token); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isBlank(token)) { |
||||
|
return chain.filter(exchange); |
||||
|
} |
||||
|
|
||||
|
TokenDto loginUserInfo = getLoginUserInfoByToken(token, jwtTokenUtils, cpUserDetailRedis); |
||||
|
if (loginUserInfo != null) { |
||||
|
ServerHttpRequest build = exchange.getRequest().mutate() |
||||
|
.header(Constant.USER_KEY, new String[]{loginUserInfo.getUserId()}).build(); |
||||
|
return chain.filter(exchange.mutate().request(build).build()); |
||||
|
} |
||||
|
|
||||
|
return chain.filter(exchange); |
||||
|
} |
||||
|
} |
@ -0,0 +1,45 @@ |
|||||
|
package com.epmet.filter; |
||||
|
|
||||
|
import com.epmet.common.token.enums.ErrorCode; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.commons.tools.security.dto.TokenDto; |
||||
|
import com.epmet.commons.tools.utils.CpUserDetailRedis; |
||||
|
import com.epmet.jwt.JwtTokenUtils; |
||||
|
import io.jsonwebtoken.Claims; |
||||
|
|
||||
|
/** |
||||
|
* 用户token的过滤器接口,提供通用的默认方法 |
||||
|
*/ |
||||
|
public interface UserTokenFilter { |
||||
|
|
||||
|
default TokenDto getLoginUserInfoByToken(String token, JwtTokenUtils jwtTokenUtils, CpUserDetailRedis cpUserDetailRedis) { |
||||
|
//是否过期
|
||||
|
Claims claims = jwtTokenUtils.getClaimByToken(token); |
||||
|
if (claims == null || jwtTokenUtils.isTokenExpired(claims.getExpiration())) { |
||||
|
throw new RenException(ErrorCode.ERR401.getCode(),ErrorCode.ERR401.getMsg()); |
||||
|
} |
||||
|
//获取用户ID
|
||||
|
String app = (String) claims.get("app"); |
||||
|
String client = (String) claims.get("client"); |
||||
|
String userId = (String) claims.get("userId"); |
||||
|
//查询Redis
|
||||
|
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());
|
||||
|
// }
|
||||
|
//
|
||||
|
//}
|
||||
|
|
||||
|
return tokenDto; |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue