diff --git a/lottery-system/.idea/dataSources.xml b/lottery-system/.idea/dataSources.xml
new file mode 100644
index 0000000..414d1e9
--- /dev/null
+++ b/lottery-system/.idea/dataSources.xml
@@ -0,0 +1,17 @@
+
+
+
+
+ mysql.8
+ true
+ com.mysql.cj.jdbc.Driver
+ jdbc:mysql://localhost:3306/lottery_system
+
+
+
+
+
+ $ProjectFileDir$
+
+
+
\ No newline at end of file
diff --git a/lottery-system/lottery-common/pom.xml b/lottery-system/lottery-common/pom.xml
index b70b74a..8ce5f96 100644
--- a/lottery-system/lottery-common/pom.xml
+++ b/lottery-system/lottery-common/pom.xml
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- org.example
+ com.lottery
lottery-system
1.0-SNAPSHOT
@@ -16,5 +16,10 @@
11
UTF-8
+
+
+
+
+
\ No newline at end of file
diff --git a/lottery-system/lottery-common/src/main/java/com/lottery/exception/Accountexception.java b/lottery-system/lottery-common/src/main/java/com/lottery/exception/Accountexception.java
new file mode 100644
index 0000000..722eca7
--- /dev/null
+++ b/lottery-system/lottery-common/src/main/java/com/lottery/exception/Accountexception.java
@@ -0,0 +1,20 @@
+package com.lottery.exception;
+
+/**
+ * @program: lottery-system
+ * @ClassName Accountexception
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:56
+ * @Version 1.0
+ **/
+public class Accountexception extends BaseException{
+
+ public Accountexception(){
+
+ }
+
+ public Accountexception(String massage){
+ super(massage);
+ }
+}
diff --git a/lottery-system/lottery-common/src/main/java/com/lottery/exception/BaseException.java b/lottery-system/lottery-common/src/main/java/com/lottery/exception/BaseException.java
new file mode 100644
index 0000000..d0dba6a
--- /dev/null
+++ b/lottery-system/lottery-common/src/main/java/com/lottery/exception/BaseException.java
@@ -0,0 +1,15 @@
+package com.lottery.exception;
+
+/**
+ * 业务异常
+ */
+public class BaseException extends RuntimeException {
+
+ public BaseException() {
+ }
+
+ public BaseException(String msg) {
+ super(msg);
+ }
+
+}
diff --git a/lottery-system/lottery-common/src/main/java/com/lottery/propertise/jwtPropertice.java b/lottery-system/lottery-common/src/main/java/com/lottery/propertise/jwtPropertice.java
new file mode 100644
index 0000000..8cf59eb
--- /dev/null
+++ b/lottery-system/lottery-common/src/main/java/com/lottery/propertise/jwtPropertice.java
@@ -0,0 +1,27 @@
+package com.lottery.propertise;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * @program: lottery-system
+ * @ClassName jwtPropertice
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:31
+ * @Version 1.0
+ **/
+@Component
+@ConfigurationProperties(prefix = "lottery.jwt")
+@Data
+public class jwtPropertice {
+
+ /**
+ * 用户jwt相关配置
+ *
+ */
+ private String userSecretKey;
+ private long userTtl;
+ private String userTokenName;
+}
diff --git a/lottery-system/lottery-common/src/main/java/com/lottery/result/Result.java b/lottery-system/lottery-common/src/main/java/com/lottery/result/Result.java
index fe85236..5ff2d43 100644
--- a/lottery-system/lottery-common/src/main/java/com/lottery/result/Result.java
+++ b/lottery-system/lottery-common/src/main/java/com/lottery/result/Result.java
@@ -11,7 +11,7 @@ package com.lottery.result;
public class Result {
// 状态码(如 200 表示成功,500 表示失败)
- private int code;
+ private Integer code;
// 提示信息(如 "操作成功" 或 "操作失败")
private String message;
diff --git a/lottery-system/lottery-common/src/main/java/com/lottery/utils/JwtUtil.java b/lottery-system/lottery-common/src/main/java/com/lottery/utils/JwtUtil.java
new file mode 100644
index 0000000..463725d
--- /dev/null
+++ b/lottery-system/lottery-common/src/main/java/com/lottery/utils/JwtUtil.java
@@ -0,0 +1,67 @@
+package com.lottery.utils;
+
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.JwtBuilder;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * @program: lottery-system
+ * @ClassName JwtUtils
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:25
+ * @Version 1.0
+ **/
+public class JwtUtil {
+ /**
+ * 生成jwt
+ * 使用Hs256算法, 私匙使用固定秘钥
+ *
+ * @param secretKey jwt秘钥
+ * @param ttlMillis jwt过期时间(毫秒)
+ * @param claims 设置的信息
+ * @return
+ */
+ public static String createJWT(String secretKey, long ttlMillis, Map claims) {
+ // 指定签名的时候使用的签名算法,也就是header那部分
+ SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
+
+ // 生成JWT的时间
+ long expMillis = System.currentTimeMillis() + ttlMillis;
+ Date exp = new Date(expMillis);
+
+ // 设置jwt的body
+ JwtBuilder builder = Jwts.builder()
+ // 如果有私有声明,一定要先设置这个自己创建的私有的声明,这个是给builder的claim赋值,一旦写在标准的声明赋值之后,就是覆盖了那些标准的声明的
+ .setClaims(claims)
+ // 设置签名使用的签名算法和签名使用的秘钥
+ .signWith(signatureAlgorithm, secretKey.getBytes(StandardCharsets.UTF_8))
+ // 设置过期时间
+ .setExpiration(exp);
+
+ return builder.compact();
+ }
+
+ /**
+ * Token解密
+ *
+ * @param secretKey jwt秘钥 此秘钥一定要保留好在服务端, 不能暴露出去, 否则sign就可以被伪造, 如果对接多个客户端建议改造成多个
+ * @param token 加密后的token
+ * @return
+ */
+ public static Claims parseJWT(String secretKey, String token) {
+ // 得到DefaultJwtParser
+ Claims claims = Jwts.parser()
+ // 设置签名的秘钥
+ .setSigningKey(secretKey.getBytes(StandardCharsets.UTF_8))
+ // 设置需要解析的jwt
+ .parseClaimsJws(token).getBody();
+ return claims;
+ }
+
+}
diff --git a/lottery-system/lottery-pojo/pom.xml b/lottery-system/lottery-pojo/pom.xml
index 34b0d91..bab56a3 100644
--- a/lottery-system/lottery-pojo/pom.xml
+++ b/lottery-system/lottery-pojo/pom.xml
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- org.example
+ com.lottery
lottery-system
1.0-SNAPSHOT
diff --git a/lottery-system/lottery-pojo/src/main/java/com/lottery/dto/UserLoginDto.java b/lottery-system/lottery-pojo/src/main/java/com/lottery/dto/UserLoginDto.java
new file mode 100644
index 0000000..0834a91
--- /dev/null
+++ b/lottery-system/lottery-pojo/src/main/java/com/lottery/dto/UserLoginDto.java
@@ -0,0 +1,21 @@
+package com.lottery.dto;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @program: lottery-system
+ * @ClassName UserLoginDto
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:07
+ * @Version 1.0
+ **/
+@Data
+public class UserLoginDto implements Serializable {
+
+ private String username;
+
+ private String password;
+}
diff --git a/lottery-system/lottery-pojo/src/main/java/com/lottery/entity/User.java b/lottery-system/lottery-pojo/src/main/java/com/lottery/entity/User.java
index 39e99ea..f636085 100644
--- a/lottery-system/lottery-pojo/src/main/java/com/lottery/entity/User.java
+++ b/lottery-system/lottery-pojo/src/main/java/com/lottery/entity/User.java
@@ -31,12 +31,9 @@ public class User {
private String username; // 用户名
+ private String password; //密码
- private String phone; // 手机号
-
-
- private Integer status; // 状态(tinyint对应Integer)
-
+ private String jwcode; // 手机号
private Date createTime; // 创建时间
diff --git a/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserLoginVo.java b/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserLoginVo.java
new file mode 100644
index 0000000..ab7aea2
--- /dev/null
+++ b/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserLoginVo.java
@@ -0,0 +1,29 @@
+package com.lottery.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * @program: lottery-system
+ * @ClassName UserLoginVo
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:15
+ * @Version 1.0
+ **/
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class UserLoginVo implements Serializable {
+
+ private long id;
+
+ private String username;
+
+ private String token;
+}
diff --git a/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserVo.java b/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserVo.java
new file mode 100644
index 0000000..2b861c6
--- /dev/null
+++ b/lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserVo.java
@@ -0,0 +1,18 @@
+package com.lottery.vo;
+
+/**
+ * @program: lottery-system
+ * @ClassName UserVo
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 15:15
+ * @Version 1.0
+ **/
+
+import lombok.Data;
+
+@Data
+public class UserVo {
+ private String jwcode;
+ private String username;
+}
diff --git a/lottery-system/lottery-service/pom.xml b/lottery-system/lottery-service/pom.xml
index d5e7141..e57a96f 100644
--- a/lottery-system/lottery-service/pom.xml
+++ b/lottery-system/lottery-service/pom.xml
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- org.example
+ com.lottery
lottery-system
1.0-SNAPSHOT
@@ -18,11 +18,19 @@
- org.example
+ com.lottery
lottery-pojo
1.0-SNAPSHOT
compile
+
+
+ com.lottery
+ lottery-common
+ 1.0-SNAPSHOT
+
+
+
diff --git a/lottery-system/lottery-service/src/main/java/com/lottery/controller/api/UserController.java b/lottery-system/lottery-service/src/main/java/com/lottery/controller/api/UserController.java
new file mode 100644
index 0000000..3bd9c53
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/java/com/lottery/controller/api/UserController.java
@@ -0,0 +1,85 @@
+package com.lottery.controller.api;
+
+import ch.qos.logback.classic.spi.EventArgUtil;
+import com.lottery.dto.UserLoginDto;
+import com.lottery.entity.User;
+import com.lottery.exception.Accountexception;
+import com.lottery.service.IUserService;
+import com.lottery.utils.JwtUtil;
+import com.lottery.vo.UserLoginVo;
+import com.lottery.vo.UserVo;
+import org.aspectj.weaver.patterns.IToken;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import com.lottery.result.Result;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+
+/**
+ * @program: lottery-system
+ * @ClassName UserController
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 9:45
+ * @Version 1.0
+ **/
+
+@RestController
+@RequestMapping("/api/user")
+public class UserController {
+
+ @Autowired
+ private IUserService userService;
+
+ private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
+ @Autowired
+ private com.lottery.propertise.jwtPropertice jwtPropertice;
+
+ @PostMapping("/login")
+ public Result login(@RequestBody UserLoginDto userLoginDto){
+
+ LOGGER.info("用户登录 :{}", userLoginDto);
+ User user = userService.login(userLoginDto);
+ if (user == null) {
+ throw new Accountexception("密码错误");
+ }
+
+ //登录成功,生成token令牌
+ HashMap map = new HashMap<>();
+ map.put("userId", user.getId());
+ String token = JwtUtil.createJWT(jwtPropertice.getUserSecretKey(), jwtPropertice.getUserTtl(), map);
+
+// UserLoginVo userLoginVo = new UserLoginVo();
+// userLoginVo.setId(user.getId());
+// userLoginVo.setToken(token);
+// userLoginVo.setUsername(user.getUsername());
+
+ UserLoginVo userLoginVo = UserLoginVo.builder()
+ .id(user.getId())
+ .token(token)
+ .username(user.getUsername())
+ .build();
+
+ return Result.success(userLoginVo);
+ }
+
+ public Result> getAllUser(){
+ LOGGER.info("查询所有用户");
+
+ List list = userService.list();
+ List list1 = new ArrayList<>();
+
+ for (User user : list) {
+
+ }
+ return Result.success(list1);
+ }
+}
diff --git a/lottery-system/lottery-service/src/main/java/com/lottery/interceptor/JwtTokenUserInterceptor.java b/lottery-system/lottery-service/src/main/java/com/lottery/interceptor/JwtTokenUserInterceptor.java
new file mode 100644
index 0000000..550d9f0
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/java/com/lottery/interceptor/JwtTokenUserInterceptor.java
@@ -0,0 +1,19 @@
+package com.lottery.interceptor;
+
+import org.springframework.stereotype.Component;
+
+/**
+ * @program: lottery-system
+ * @ClassName JwtTokenUserInterceptor
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 12:24
+ * @Version 1.0
+ **/
+
+
+@Component
+public class JwtTokenUserInterceptor {
+
+
+}
diff --git a/lottery-system/lottery-service/src/main/java/com/lottery/mapper/IUserMapper.java b/lottery-system/lottery-service/src/main/java/com/lottery/mapper/IUserMapper.java
new file mode 100644
index 0000000..79a93b7
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/java/com/lottery/mapper/IUserMapper.java
@@ -0,0 +1,22 @@
+package com.lottery.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.lottery.entity.User;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+/**
+ * @program: lottery-system
+ * @ClassName IUserMapper
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:50
+ * @Version 1.0
+ **/
+@Mapper
+public interface IUserMapper extends BaseMapper {
+
+
+ @Select("select id, username, password, status from user where username = #{username}")
+ User getByUsername(String username);
+}
\ No newline at end of file
diff --git a/lottery-system/lottery-service/src/main/java/com/lottery/service/IUserService.java b/lottery-system/lottery-service/src/main/java/com/lottery/service/IUserService.java
new file mode 100644
index 0000000..6eff631
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/java/com/lottery/service/IUserService.java
@@ -0,0 +1,21 @@
+package com.lottery.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.lottery.dto.UserLoginDto;
+import com.lottery.entity.User;
+import com.lottery.vo.UserVo;
+
+import java.util.List;
+
+/**
+ * @program: lottery-system
+ * @ClassName IUserService
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:05
+ * @Version 1.0
+ **/
+public interface IUserService extends IService {
+ User login(UserLoginDto userLoginDto);
+
+}
diff --git a/lottery-system/lottery-service/src/main/java/com/lottery/service/Impl/UserServiceImpl.java b/lottery-system/lottery-service/src/main/java/com/lottery/service/Impl/UserServiceImpl.java
new file mode 100644
index 0000000..b8688b1
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/java/com/lottery/service/Impl/UserServiceImpl.java
@@ -0,0 +1,49 @@
+package com.lottery.service.Impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.lottery.dto.Prize;
+import com.lottery.dto.UserLoginDto;
+import com.lottery.entity.User;
+import com.lottery.exception.Accountexception;
+import com.lottery.mapper.IPrizeMapper;
+import com.lottery.mapper.IUserMapper;
+import com.lottery.service.IUserService;
+import com.lottery.vo.UserVo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @program: lottery-system
+ * @ClassName UserServiceImpl
+ * @description:
+ * @author: wwl
+ * @create: 2025-07-12 10:05
+ * @Version 1.0
+ **/
+@Service
+public class UserServiceImpl extends ServiceImpl implements IUserService{
+
+ @Autowired
+ private IUserMapper userMapper;
+
+ @Override
+ public User login(UserLoginDto userLoginDto) {
+ String password = userLoginDto.getPassword();
+ String username = userLoginDto.getUsername();
+ User user = userMapper.getByUsername(username);
+
+ if (user == null) throw new Accountexception("账号不存在");
+
+ if (!password.equals(user.getPassword())) {
+ //密码错误
+ throw new Accountexception("密码错误");
+ }
+
+
+ return user;
+ }
+
+}
diff --git a/lottery-system/lottery-service/src/main/resources/application.yml b/lottery-system/lottery-service/src/main/resources/application.yml
index adcf8b6..ca5467b 100644
--- a/lottery-system/lottery-service/src/main/resources/application.yml
+++ b/lottery-system/lottery-service/src/main/resources/application.yml
@@ -13,7 +13,6 @@ spring:
maximum-pool-size: 10 # 连接池大小
connection-timeout: 30000
-
# ========== Redis 配置 ==========
redis:
host: 127.0.0.1
@@ -28,6 +27,7 @@ spring:
min-idle: 1 # 最小空闲连接
timeout: 3000ms # 连接超时时间
+
# ========== MyBatis 配置(如果使用MyBatis代替JPA) ==========
mybatis:
mapper-locations: classpath:mapper/*.xml # XML映射文件路径
@@ -38,4 +38,11 @@ mybatis:
# ========== 自定义配置(示例) ==========
lottery:
- max-draw-times: 3 # 用户每日最大抽奖次数
\ No newline at end of file
+ jwt:
+ #用户端JWT
+ user-secret-key: willier_need_at_least_32_chars_secure_key_12345
+ user-ttl: 7200000
+ user-token-name: authentication
+
+ max-draw-times: 3 # 用户每日最大抽奖次数
+
diff --git a/lottery-system/lottery-service/src/main/resources/mapper/userMapper.xml b/lottery-system/lottery-service/src/main/resources/mapper/userMapper.xml
new file mode 100644
index 0000000..99066b5
--- /dev/null
+++ b/lottery-system/lottery-service/src/main/resources/mapper/userMapper.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/lottery-system/pom.xml b/lottery-system/pom.xml
index 1bfd4b5..242ced7 100644
--- a/lottery-system/pom.xml
+++ b/lottery-system/pom.xml
@@ -16,7 +16,7 @@
4.0.0
- org.example
+ com.lottery
lottery-system
1.0-SNAPSHOT
@@ -85,6 +85,24 @@
commons-lang3
3.12.0
+
+
+ io.jsonwebtoken
+ jjwt-api
+ 0.11.5
+
+
+ io.jsonwebtoken
+ jjwt-impl
+ 0.11.5
+ runtime
+
+
+ io.jsonwebtoken
+ jjwt-jackson
+ 0.11.5
+ runtime
+