diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml
index 89c7962..1e16f6e 100644
--- a/.idea/dataSources.xml
+++ b/.idea/dataSources.xml
@@ -14,12 +14,12 @@
$ProjectFileDir$
-
+
redis
true
true
jdbc.RedisDriver
- jdbc:redis://106.15.251.123:6379/2
+ jdbc:redis://47.120.65.66:6379/2
diff --git a/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java b/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java
index b1c4e45..9a2d0e5 100644
--- a/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java
+++ b/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java
@@ -39,11 +39,12 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
log.info("WebMvcConfiguration===>开始注册自定义拦截器...");
+ // 拦截后台管理内容
String[] loginAuthInterceptors = {"/admin/system/index/login", "/admin/system/index/generateValidateCode", "/admin/product/category/exportData"};
- // 需要拦截的
registry.addInterceptor(loginAuthInterceptor).addPathPatterns("/admin/**")
.excludePathPatterns(loginAuthInterceptors);
- registry.addInterceptor(userLoginAuthInterceptor).addPathPatterns("/api/**/auth/**");
+ // 拦截前台内容
+ registry.addInterceptor(userLoginAuthInterceptor).addPathPatterns("/api/**");
}
}
\ No newline at end of file
diff --git a/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java
index f3e50bc..ea4059c 100644
--- a/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java
+++ b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java
@@ -1,19 +1,19 @@
package com.atguigu.interceptor;
import com.alibaba.fastjson.JSON;
-import com.atguigu.constant.MessageConstant;
import com.atguigu.context.BaseContext;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.utils.EmptyUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
-import org.springframework.web.servlet.ModelAndView;
@Configuration
@Slf4j
@@ -24,25 +24,19 @@ public class UserLoginAuthInterceptor implements HandlerInterceptor {
private EmptyUtil emptyUtil;
@Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- log.info("UserLoginAuthInterceptor===>preHandle拦截请求前");
-
- // 判断token是否为空
+ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
String token = request.getHeader("token");
- emptyUtil.isEmpty(token, MessageConstant.TOKEN_IS_EMPTY);
- Object userJSON = redisTemplate.opsForValue().get(token);
- String jsonString = JSON.toJSONString(userJSON);
- BaseContext.setUserInfo(JSON.parseObject(jsonString, UserInfo.class));
+ if (!StringUtils.isEmpty(token)) {
+ Object userJSON = redisTemplate.opsForValue().get(token);
+ String userJSONString = JSON.toJSONString(userJSON);
+ BaseContext.setUserInfo(JSON.parseObject(userJSONString, UserInfo.class));
+ }
return true;
}
@Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
+ public void afterCompletion(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler, @Nullable Exception ex) throws Exception {
BaseContext.removeUserInfo();
}
}
\ No newline at end of file
diff --git a/spzx-common/common-service/src/main/java/com/atguigu/utils/RedisUtils.java b/spzx-common/common-service/src/main/java/com/atguigu/utils/RedisUtils.java
new file mode 100644
index 0000000..4445749
--- /dev/null
+++ b/spzx-common/common-service/src/main/java/com/atguigu/utils/RedisUtils.java
@@ -0,0 +1,38 @@
+package com.atguigu.utils;
+
+import com.alibaba.fastjson.JSON;
+import com.atguigu.constant.MessageConstant;
+import com.atguigu.constant.RedisUserConstant;
+import com.atguigu.exception.BunnyException;
+import com.atguigu.spzx.model.entity.user.UserInfo;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+@Component
+public class RedisUtils {
+ @Autowired
+ private RedisTemplate redisTemplate;
+
+ public static String getCartKey(Long userId) {
+ return RedisUserConstant.REDIS_CART_KEY + userId;
+ }
+
+ /**
+ * Redis通过token获取用户信息
+ *
+ * @param token 请求key的token
+ * @return 用户信息
+ */
+ public UserInfo getUserInfo(String token) {
+ // 获取用户信息字符串
+ Object object = redisTemplate.opsForValue().get(token);
+ String jsonString = JSON.toJSONString(object);
+ if (StringUtils.isEmpty(jsonString)) {
+ throw new BunnyException(MessageConstant.USER_DOES_NOT_EXIST);
+ }
+ // 转为UserInfo类
+ return JSON.parseObject(jsonString, UserInfo.class);
+ }
+}
diff --git a/spzx-common/common-util/src/main/java/com/atguigu/constant/RedisUserConstant.java b/spzx-common/common-util/src/main/java/com/atguigu/constant/RedisUserConstant.java
new file mode 100644
index 0000000..f4669bc
--- /dev/null
+++ b/spzx-common/common-util/src/main/java/com/atguigu/constant/RedisUserConstant.java
@@ -0,0 +1,11 @@
+package com.atguigu.constant;
+
+import lombok.Data;
+
+/**
+ * Redis用户前缀设置
+ */
+@Data
+public class RedisUserConstant {
+ public static final String REDIS_CART_KEY = "cart::";
+}
diff --git a/spzx-manager/src/main/resources/application-dev.yml b/spzx-manager/src/main/resources/application-dev.yml
index 43c86ac..243c51f 100644
--- a/spzx-manager/src/main/resources/application-dev.yml
+++ b/spzx-manager/src/main/resources/application-dev.yml
@@ -7,9 +7,10 @@ bunny:
password: "02120212"
redis:
- host: 106.15.251.123
+ host: 47.120.65.66
port: 6379
database: 2
+ password: "02120212"
minio:
endpointUrl: "http://129.211.31.58:9000"
diff --git a/spzx-manager/src/main/resources/application.yml b/spzx-manager/src/main/resources/application.yml
index 59c06e1..8d14380 100644
--- a/spzx-manager/src/main/resources/application.yml
+++ b/spzx-manager/src/main/resources/application.yml
@@ -20,6 +20,7 @@ spring:
host: ${bunny.redis.host}
port: ${bunny.redis.port}
database: ${bunny.redis.database}
+ password: ${bunny.redis.password}
logging:
level:
diff --git a/spzx-server-gateway/src/main/java/com/atguigu/gateway/filter/AuthGlobalFilter.java b/spzx-server-gateway/src/main/java/com/atguigu/gateway/filter/AuthGlobalFilter.java
index ef73c1a..76a43b9 100644
--- a/spzx-server-gateway/src/main/java/com/atguigu/gateway/filter/AuthGlobalFilter.java
+++ b/spzx-server-gateway/src/main/java/com/atguigu/gateway/filter/AuthGlobalFilter.java
@@ -3,65 +3,47 @@ package com.atguigu.gateway.filter;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.spzx.model.vo.result.Result;
import com.atguigu.spzx.model.vo.result.ResultCodeEnum;
+import com.atguigu.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
-import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.AntPathMatcher;
-import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
-import java.util.List;
@Configuration
public class AuthGlobalFilter implements GlobalFilter, Ordered {
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Autowired
- private RedisTemplate redisTemplate;
+ private RedisUtils redisUtils;
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
- // 获取请求路径
+ // 当前请求路径
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
// 判断路径 /api/**/auth/**,登录校验
if (antPathMatcher.match("/api/**/auth/**", path)) {
// 登录校验
- Object userInfo = getUserInfo(request);
+ String token = request.getHeaders().get("token").get(0);
+ Object userInfo = redisUtils.getUserInfo(token);
if (userInfo == null) {
ServerHttpResponse response = exchange.getResponse();
return out(response, ResultCodeEnum.LOGIN_AUTH);
}
}
-
// 放行
return chain.filter(exchange);
}
-
- private Object getUserInfo(ServerHttpRequest request) {
- // 从请求头获取token
- String token = "";
- List tokenList = request.getHeaders().get("token");
- if (tokenList != null) {
- token = tokenList.get(0);
- }
- // 判断token是否为空
- if (!StringUtils.isEmpty(token)) {
- // 根据token查询redis
- return redisTemplate.opsForValue().get(token);
- }
- return null;
- }
-
@Override
public int getOrder() {
return 0;
diff --git a/spzx-server-gateway/src/main/resources/application-dev.yml b/spzx-server-gateway/src/main/resources/application-dev.yml
index ea08af1..e1f8308 100644
--- a/spzx-server-gateway/src/main/resources/application-dev.yml
+++ b/spzx-server-gateway/src/main/resources/application-dev.yml
@@ -5,6 +5,7 @@ bunny:
namespace: spzx
redis:
- host: 106.15.251.123
+ host: 47.120.65.66
port: 6379
- database: 2
\ No newline at end of file
+ database: 2
+ password: "02120212"
\ No newline at end of file
diff --git a/spzx-server-gateway/src/main/resources/application.yml b/spzx-server-gateway/src/main/resources/application.yml
index 27236dc..7f827a0 100644
--- a/spzx-server-gateway/src/main/resources/application.yml
+++ b/spzx-server-gateway/src/main/resources/application.yml
@@ -10,14 +10,14 @@ spring:
# allow-bean-definition-overriding: true
cloud:
+ sentinel:
+ log:
+ dir: logs/${spring.application.name}/sentinel
nacos:
discovery:
namespace: ${bunny.nacos.discovery.namespace}
server-addr: ${bunny.nacos.server-addr}
log-name: logs/${spring.application.name}
- sentinel:
- log:
- dir: logs/${spring.application.name}/sentinel
gateway:
discovery:
locator:
@@ -56,6 +56,7 @@ spring:
host: ${bunny.redis.host}
port: ${bunny.redis.port}
database: ${bunny.redis.database}
+ password: ${bunny.redis.password}
logging:
level:
diff --git a/spzx-service-client/service-product-client/src/main/java/com/atguigu/feign/product/ProductFeignClient.java b/spzx-service-client/service-product-client/src/main/java/com/atguigu/feign/product/ProductFeignClient.java
index 87ad38e..67da4d2 100644
--- a/spzx-service-client/service-product-client/src/main/java/com/atguigu/feign/product/ProductFeignClient.java
+++ b/spzx-service-client/service-product-client/src/main/java/com/atguigu/feign/product/ProductFeignClient.java
@@ -6,7 +6,7 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
-@FeignClient(name = "service-product")
+@FeignClient(name = "service-product", path = "/api/product/")
public interface ProductFeignClient {
@GetMapping("getBySkuId/{skuId}")
Result getBySkuId(@PathVariable Long skuId);
diff --git a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java
index 756ed27..0be0ce0 100644
--- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java
+++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java
@@ -7,6 +7,7 @@ import com.atguigu.context.BaseContext;
import com.atguigu.feign.product.ProductFeignClient;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.entity.product.ProductSku;
+import com.atguigu.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -32,14 +33,15 @@ public class CartServiceImpl implements CartService {
*/
@Override
public void addToCart(Long skuId, Integer skuNum) {
+ // 当前用户
Long userId = BaseContext.getUserInfo().getId();
- // 获取key名称
- String cartKey = userId.toString();
+ // 用户id组合,得到购物车id
+ String cartKey = RedisUtils.getCartKey(userId);
Object cartInfoJson = redisTemplate.opsForHash().get(cartKey, String.valueOf(skuId));
// 如果不为空
CartInfo cartInfo = null;
if (cartInfoJson != null) {
- cartInfo = JSON.parseObject(cartInfoJson.toString(), CartInfo.class);
+ cartInfo = JSON.parseObject(JSON.toJSONString(cartInfoJson), CartInfo.class);
// 数量相加
cartInfo.setSkuNum(cartInfo.getSkuNum() + skuNum);
// 购物车状态为选中
@@ -61,7 +63,7 @@ public class CartServiceImpl implements CartService {
cartInfo.setUpdateTime(new Date());
}
// 将商品数据存储到购物车中
- redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
+ redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), cartInfo);
}
/**
@@ -73,13 +75,13 @@ public class CartServiceImpl implements CartService {
public List getCartList() {
// 获取当前登录的用户信息
Long userId = BaseContext.getUserInfo().getId();
- String cartKey = userId.toString();
+ String cartKey = RedisUtils.getCartKey(userId);
// 获取数据
List