feat(修复): 登录错误等异常

Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
bunny 2024-03-28 16:51:46 +08:00
parent b8c745ff37
commit 87cb1a343a
18 changed files with 105 additions and 105 deletions

View File

@ -7,26 +7,30 @@
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="spzx-model" />
<module name="spzx-server-gateway" />
<module name="common-util" />
<module name="service-product-client" />
<module name="common-service" />
<module name="service-user-client" />
<module name="common-log" />
<module name="service-order" />
<module name="spzx-model" />
<module name="spzx-server-gateway" />
<module name="service-product" />
<module name="service-user" />
<module name="service-cart" />
<module name="spzx-manager" />
<module name="service-user-client" />
<module name="common-log" />
<module name="service-order" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="common-web" target="17" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="common-log" options="-parameters" />
<module name="common-service" options="-parameters" />
<module name="common-util" options="-parameters" />
<module name="common-web" options="-parameters" />
<module name="service-cart" options="-parameters" />
<module name="service-order" options="-parameters" />
<module name="service-product" options="-parameters" />

View File

@ -4,6 +4,7 @@
<file url="file://$PROJECT_DIR$/spzx-common/common-log/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-common/common-service/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-common/common-util/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-common/common-web/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-common/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spzx-manager/src/main/java" charset="UTF-8" />

View File

@ -9,6 +9,7 @@
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/spzx-common/common-web/pom.xml" />
<option value="$PROJECT_DIR$/spzx-service-client/untitled/pom.xml" />
</set>
</option>

View File

@ -38,6 +38,7 @@
<cloud.version>2022.0.2</cloud.version>
<alibaba.version>2022.0.0.0-RC2</alibaba.version>
<alipay.version>4.8.73.ALL</alipay.version>
<minio.version>8.5.9</minio.version>
</properties>
<!-- 管理依赖,版本锁定 -->
@ -94,6 +95,12 @@
<artifactId>alipay-sdk-java</artifactId>
<version>${alipay.version}</version>
</dependency>
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -12,18 +12,13 @@
<packaging>jar</packaging>
<name>common-log</name>
<url>http://maven.apache.org</url>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>spzx-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common-util</artifactId>
@ -42,7 +37,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -37,7 +37,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
@ -52,9 +52,8 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.3.7.v20160115</version>
<version>12.0.5</version>
</dependency>
<!-- openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>

View File

@ -1,17 +0,0 @@
package com.atguigu.anno;
import com.atguigu.config.WebMvcConfiguration;
import com.atguigu.interceptor.UserLoginAuthInterceptor;
import org.springframework.context.annotation.Import;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
@Import(value = {UserLoginAuthInterceptor.class, WebMvcConfiguration.class})
public @interface EnableUserWebMvcConfiguration {
}

View File

@ -24,7 +24,7 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
log.info("WebMvcConfiguration===>开始跨域注册表...");
registry.addMapping("/**")// 添加路径规则
registry.addMapping("/admin/**")// 添加路径规则
.allowCredentials(true)// 是否允许在跨域的情况下传递Cookie
.allowedOriginPatterns("*")// 允许请求来源的域规则
.allowedMethods("*").allowedHeaders("*");// 允许所有的请求头
@ -44,8 +44,9 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
registry.addInterceptor(loginAuthInterceptor).addPathPatterns("/admin/**")
.excludePathPatterns(loginAuthInterceptors);
String[] userLoginAuthInterceptors = {};
registry.addInterceptor(userLoginAuthInterceptor).addPathPatterns("/api/**")
.excludePathPatterns(userLoginAuthInterceptors);
// String[] userLoginAuthInterceptors = {"/api/user/userInfo/login"};
registry.addInterceptor(userLoginAuthInterceptor).addPathPatterns("/api/**/auth/**");
// registry.addInterceptor(userLoginAuthInterceptor).addPathPatterns("/api/**");
// .excludePathPatterns(userLoginAuthInterceptors);
}
}

View File

@ -31,8 +31,9 @@ public class UserLoginAuthInterceptor implements HandlerInterceptor {
String token = request.getHeader("token");
emptyUtil.isEmpty(token, MessageConstant.TOKEN_IS_EMPTY);
String userJSON = (String) redisTemplate.opsForValue().get(token);
BaseContext.setUserInfo(JSON.parseObject(userJSON, UserInfo.class));
Object userJSON = redisTemplate.opsForValue().get(token);
String jsonString = JSON.toJSONString(userJSON);
BaseContext.setUserInfo(JSON.parseObject(jsonString, UserInfo.class));
return true;
}

View File

@ -18,13 +18,6 @@
</properties>
<dependencies>
<!-- 自己的模块 -->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>spzx-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- fastjson依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
@ -34,7 +27,6 @@
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.9</version>
</dependency>
<!-- redis -->
<dependency>

View File

@ -22,6 +22,12 @@
</properties>
<dependencies>
<!-- 自己的模块 -->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>spzx-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- hutool工具包 -->
<dependency>
<groupId>cn.hutool</groupId>

View File

@ -24,11 +24,6 @@
<artifactId>common-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common-log</artifactId>
@ -65,24 +60,4 @@
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests> <!--默认关掉单元测试 -->
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,37 @@
package com.atguigu.gateway.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
@Slf4j
public class CorsConfig {
/**
* * 解决跨域
*/
@Bean
protected CorsWebFilter addCorsMappings() {
log.info("CorsConfig===>开始跨域注册表...");
// 当allowCredentials设置为true时allowedOrigins属性不能使用通配符"*"而是需要显式列出允许的源或使用allowedOriginPatterns
CorsConfiguration config = new CorsConfiguration();
// 设置跨域请求地址
// config.addAllowedOrigin("*");
config.addAllowedMethod("*");
config.addAllowedHeader("*");
// 使用allowedOriginPatterns属性在Spring Framework 5.3及更高版本中
// 您可以使用allowedOriginPatterns属性它允许您使用正则表达式模式来匹配允许的源这样您可以更灵活地配置允许的源例如
// 使用"https?://example.com"来匹配http://example.com和https://example.com两个源
config.addAllowedOriginPattern("*");
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);
return new CorsWebFilter(source);
}
}

View File

@ -1,12 +1,8 @@
package com.atguigu.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.constant.MessageConstant;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.result.Result;
import com.atguigu.spzx.model.vo.result.ResultCodeEnum;
import com.atguigu.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
@ -29,32 +25,39 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private EmptyUtil emptyUtil;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 获取请求路径
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
// 判断请求路径
if (antPathMatcher.match("/api/**/auth/**", path)) {
UserInfo userInfo = getUserinfo(request);
emptyUtil.isEmpty(userInfo, MessageConstant.USER_DOES_NOT_EXIST);
ServerHttpResponse response = exchange.getResponse();
return out(exchange.getResponse());
// 判断路径 /api/**/auth/**登录校验
if (antPathMatcher.match("/api/**/auth/**", path)) {
// 登录校验
Object userInfo = getUserInfo(request);
if (userInfo == null) {
ServerHttpResponse response = exchange.getResponse();
return out(response, ResultCodeEnum.LOGIN_AUTH);
}
}
// 放行
return chain.filter(exchange);
}
private UserInfo getUserinfo(ServerHttpRequest request) {
private Object getUserInfo(ServerHttpRequest request) {
// 从请求头获取token
String token = "";
List<String> tokenList = request.getHeaders().get("token");
emptyUtil.isEmpty(tokenList, MessageConstant.TOKEN_IS_EMPTY);
String token = tokenList.get(0);
if (StringUtils.hasText(token)) {
String userJson = (String) redisTemplate.opsForValue().get(token);
emptyUtil.isEmpty(userJson, MessageConstant.TOKEN_IS_EMPTY);
return JSON.parseObject(userJson, UserInfo.class);
if (tokenList != null) {
token = tokenList.get(0);
}
// 判断token是否为空
if (!StringUtils.isEmpty(token)) {
// 根据token查询redis
return redisTemplate.opsForValue().get(token);
}
return null;
}
@ -64,8 +67,8 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
return 0;
}
private Mono<Void> out(ServerHttpResponse response) {
Result<ResultCodeEnum> result = Result.build(null, ResultCodeEnum.LOGIN_AUTH);
private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
Result<String> result = Result.build(null, resultCodeEnum);
byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().wrap(bits);
// 指定编码否则在浏览器中会中文乱码

View File

@ -7,7 +7,7 @@ spring:
name: service-gateway
main:
web-application-type: reactive
allow-bean-definition-overriding: true
# allow-bean-definition-overriding: true
cloud:
nacos:
@ -43,14 +43,14 @@ spring:
uri: lb://service-user
predicates:
- Path=/*/user/**
- id: service-order
uri: lb://service-order
predicates:
- Path=/*/order/**
- id: service-cart
uri: lb://service-cart
predicates:
- Path=/*/order/cart/**
- Path=/api/order/cart/**
- id: service-order
uri: lb://service-order
predicates:
- Path=/api/order/orderInfo/**
data:
redis:
host: ${bunny.redis.host}

View File

@ -7,7 +7,6 @@ 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.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@ -23,8 +22,6 @@ public class CartServiceImpl implements CartService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private EmptyUtil emptyUtil;
@Autowired
private ProductFeignClient productFeignClient;
/**

View File

@ -4,7 +4,7 @@
<contextName>logback</contextName>
<!-- 日志的输出目录 -->
<property name="log.path" value="logs//service-user//logs"/>
<property name="log.path" value="logs//service-cart//logs"/>
<!--控制台日志格式:彩色日志-->
<!-- magenta:洋红 -->

View File

@ -1,6 +1,5 @@
package com.atguigu.user.service.impl;
import com.alibaba.fastjson.JSON;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.UserConstant;
import com.atguigu.context.BaseContext;
@ -96,7 +95,7 @@ public class UserInfoServiceImpl implements UserInfoService {
}
// 存入用户信息到Redis
String token = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(token, JSON.toJSONString(userInfo), 30, TimeUnit.DAYS);
redisTemplate.opsForValue().set(token, userInfo, 30, TimeUnit.DAYS);
return token;
}
@ -112,7 +111,7 @@ public class UserInfoServiceImpl implements UserInfoService {
// emptyUtil.isEmpty(tokenObject, MessageConstant.TOKEN_IS_EMPTY);
//
// UserInfo userInfo = JSON.parseObject((String) tokenObject, UserInfo.class);
UserInfo userInfo = BaseContext.getUserInfo();
UserInfoVo userInfoVo = new UserInfoVo();
BeanUtils.copyProperties(userInfo, userInfoVo);