-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a77ce3
commit 599a75c
Showing
43 changed files
with
1,108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...x-common/common-service/src/main/java/com/service/anno/EnableUserWebMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.service.anno; | ||
|
||
import com.service.config.UserWebMvcConfiguration; | ||
import com.service.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; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/16 | ||
**/ | ||
@Retention(value = RetentionPolicy.RUNTIME) | ||
@Target(value = ElementType.TYPE) | ||
@Import(value = {UserLoginAuthInterceptor.class,UserWebMvcConfiguration.class}) | ||
public @interface EnableUserWebMvcConfiguration { | ||
} |
21 changes: 21 additions & 0 deletions
21
.../spzx-common/common-service/src/main/java/com/service/config/UserWebMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.service.config; | ||
|
||
import com.service.interceptor.UserLoginAuthInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/16 | ||
**/ | ||
|
||
public class UserWebMvcConfiguration implements WebMvcConfigurer { | ||
@Autowired | ||
UserLoginAuthInterceptor userLoginAuthInterceptor; | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(userLoginAuthInterceptor) | ||
.addPathPatterns("/api/**"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...common/common-service/src/main/java/com/service/interceptor/UserLoginAuthInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.service.interceptor; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.model.entity.user.UserInfo; | ||
import com.service.utils.AuthContextUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/16 | ||
**/ | ||
|
||
public class UserLoginAuthInterceptor implements HandlerInterceptor { | ||
@Autowired | ||
private RedisTemplate<String , String> redisTemplate ; | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
|
||
// 如果token不为空,那么此时验证token的合法性 | ||
String userInfoJSON = redisTemplate.opsForValue().get("user:login:" + request.getHeader("token")); | ||
AuthContextUtil.setUserInfo(JSON.parseObject(userInfoJSON , UserInfo.class)); | ||
return true ; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
spzx/spzx-common/common-service/src/main/java/com/service/utils/AuthContextUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.service.utils; | ||
|
||
import com.model.entity.user.UserInfo; | ||
|
||
/** | ||
* | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/16 | ||
**/public class AuthContextUtil { | ||
private static final ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>() ; | ||
|
||
|
||
// 定义存储数据的静态方法 | ||
public static void setUserInfo(UserInfo userInfo) { | ||
userInfoThreadLocal.set(userInfo); | ||
} | ||
|
||
// 定义获取数据的方法 | ||
public static UserInfo getUserInfo() { | ||
return userInfoThreadLocal.get() ; | ||
} | ||
|
||
// 删除数据的方法 | ||
public static void removeUserInfo() { | ||
userInfoThreadLocal.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ public static SysUser get() { | |
public static void remove(){ | ||
THREAD_LOCAL.remove(); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
spzx/spzx-common/common-util/src/main/java/com/utils/IpUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.utils; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/14 | ||
**/ | ||
public class IpUtil { | ||
public static String getIpAddress(HttpServletRequest request) { | ||
String ipAddress = null; | ||
try { | ||
ipAddress = request.getHeader("x-forwarded-for"); | ||
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getHeader("Proxy-Client-IP"); | ||
} | ||
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getHeader("WL-Proxy-Client-IP"); | ||
} | ||
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getRemoteAddr(); | ||
if (ipAddress.equals("127.0.0.1")) { | ||
// 根据网卡取本机配置的IP | ||
InetAddress inet = null; | ||
try { | ||
inet = InetAddress.getLocalHost(); | ||
} catch (UnknownHostException e) { | ||
e.printStackTrace(); | ||
} | ||
ipAddress = inet.getHostAddress(); | ||
} | ||
} | ||
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 | ||
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() | ||
// = 15 | ||
if (ipAddress.indexOf(",") > 0) { | ||
ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); | ||
} | ||
} | ||
} catch (Exception e) { | ||
ipAddress=""; | ||
} | ||
// ipAddress = this.getRequest().getRemoteAddr(); | ||
|
||
return ipAddress; | ||
} | ||
// 网关中获取Ip地址 | ||
public static String getGatwayIpAddress(ServerHttpRequest request) { | ||
HttpHeaders headers = request.getHeaders(); | ||
String ip = headers.getFirst("x-forwarded-for"); | ||
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { | ||
// 多次反向代理后会有多个ip值,第一个ip才是真实ip | ||
if (ip.indexOf(",") != -1) { | ||
ip = ip.split(",")[0]; | ||
} | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = headers.getFirst("Proxy-Client-IP"); | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = headers.getFirst("WL-Proxy-Client-IP"); | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = headers.getFirst("HTTP_CLIENT_IP"); | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = headers.getFirst("HTTP_X_FORWARDED_FOR"); | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = headers.getFirst("X-Real-IP"); | ||
} | ||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getRemoteAddress().getAddress().getHostAddress(); | ||
} | ||
return ip; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
spzx/spzx-model/src/main/java/com/model/dto/user/UserLoginDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.model.dto.user; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/14 | ||
**/ | ||
@Data | ||
public class UserLoginDto { | ||
private String username; | ||
|
||
private String password; | ||
} |
22 changes: 22 additions & 0 deletions
22
spzx/spzx-model/src/main/java/com/model/entity/h5/CartInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.model.entity.h5; | ||
|
||
import com.model.entity.base.BaseEntity; | ||
import lombok.Data; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/17 | ||
**/ | ||
@Data | ||
public class CartInfo extends BaseEntity { | ||
private static final long serialVersionUID = 1L; | ||
private Long userId; | ||
private Long skuId; | ||
private BigDecimal cartPrice; | ||
private Integer skuNum; | ||
private String imgUrl; | ||
private String skuName; | ||
private Integer isChecked; | ||
} |
33 changes: 33 additions & 0 deletions
33
spzx/spzx-model/src/main/java/com/model/entity/user/UserAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.model.entity.user; | ||
|
||
import com.model.entity.base.BaseEntity; | ||
import lombok.Data; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/17 | ||
**/ | ||
@Data | ||
public class UserAddress extends BaseEntity { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private Long userId; | ||
|
||
private String name; | ||
|
||
private String phone; | ||
|
||
private String tagName; | ||
|
||
private String provinceCode; | ||
|
||
private String cityCode; | ||
|
||
private String districtCode; | ||
|
||
private String address; | ||
|
||
private String fullAddress; | ||
|
||
private Integer isDefault; | ||
} |
13 changes: 13 additions & 0 deletions
13
spzx/spzx-model/src/main/java/com/model/vo/user/UserInfoVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.model.vo.user; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/14 | ||
**/ | ||
@Data | ||
public class UserInfoVo { | ||
private String nickName; | ||
private String avatar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spzx/spzx-server-gateway/src/main/java/com/gateway/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.gateway.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
/** | ||
* @author 帕斯卡的芦苇 | ||
* @date 2025/2/14 | ||
**/ | ||
@Configuration | ||
public class RedisConfig { | ||
@Bean | ||
@Primary | ||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
|
||
//String的序列化方式 | ||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); | ||
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); | ||
|
||
//序列号key value | ||
redisTemplate.setKeySerializer(stringRedisSerializer); | ||
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); | ||
|
||
redisTemplate.afterPropertiesSet(); | ||
return redisTemplate; | ||
} | ||
} |
Oops, something went wrong.