| | |
| | | package com.ruoyi.gateway.filter; |
| | | |
| | | 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.core.Ordered; |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.server.ServerWebExchange; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.constant.HttpStatus; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties; |
| | | import io.jsonwebtoken.Claims; |
| | | 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.core.Ordered; |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.server.ServerWebExchange; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | { |
| | | ServerHttpRequest request = exchange.getRequest(); |
| | | ServerHttpRequest.Builder mutate = request.mutate(); |
| | | |
| | | String url = request.getURI().getPath(); |
| | | log.info("requestUrl---"+url); |
| | | String token = getToken(request); |
| | | Claims claims = null; |
| | | String userid = null; |
| | | if(StringUtils.isNotBlank(token)){ |
| | | claims = JwtUtils.parseToken(token); |
| | | if(claims!=null){ |
| | | userid = JwtUtils.getUserId(claims); |
| | | addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid); |
| | | } |
| | | } |
| | | // 跳过不需要验证的路径 |
| | | if (StringUtils.matches(url, ignoreWhite.getWhites())) |
| | | { |
| | | //附近商户获取 |
| | | if(url.contains("getNearbyShop")||url.contains("pageRecommendGoods")||url.contains("pageActivityGoods")||url.contains("getGoodsInfo")||url.contains("getShopInfo")||url.contains("pageAppActivityGoods")){ |
| | | String token = getToken(request); |
| | | if(!StringUtils.isEmpty(token)){ |
| | | Claims claims = JwtUtils.parseToken(token); |
| | | if(claims!=null){ |
| | | String userid = JwtUtils.getUserId(claims); |
| | | String userkey = JwtUtils.getUserKey(claims); |
| | | String username = JwtUtils.getUserName(claims); |
| | | if(!StringUtils.isEmpty(userid)){ |
| | | addHeader(mutate, SecurityConstants.USER_KEY, userkey); |
| | | addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid); |
| | | addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | return chain.filter(exchange); |
| | | } |
| | | String token = getToken(request); |
| | | log.info("requestToken---"+token); |
| | | if (StringUtils.isEmpty(token)) |
| | | { |
| | | return unauthorizedResponse(exchange, "需要先登录才能使用该功能"); |
| | | } |
| | | Claims claims = JwtUtils.parseToken(token); |
| | | if (claims == null) |
| | | { |
| | | return unauthorizedResponse(exchange, "需要先登录才能使用该功能!"); |
| | |
| | | { |
| | | return unauthorizedResponse(exchange, "登录状态已过期"); |
| | | } |
| | | String userid = JwtUtils.getUserId(claims); |
| | | String username = JwtUtils.getUserName(claims); |
| | | if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username)) |
| | | { |
| | | return unauthorizedResponse(exchange, "登录验证失败"); |
| | | } |
| | | |
| | | // 设置用户信息到请求 |
| | | addHeader(mutate, SecurityConstants.USER_KEY, userkey); |
| | | addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid); |