21 changed files with 463 additions and 13 deletions
-
17lottery-system/.idea/dataSources.xml
-
7lottery-system/lottery-common/pom.xml
-
20lottery-system/lottery-common/src/main/java/com/lottery/exception/Accountexception.java
-
15lottery-system/lottery-common/src/main/java/com/lottery/exception/BaseException.java
-
27lottery-system/lottery-common/src/main/java/com/lottery/propertise/jwtPropertice.java
-
2lottery-system/lottery-common/src/main/java/com/lottery/result/Result.java
-
67lottery-system/lottery-common/src/main/java/com/lottery/utils/JwtUtil.java
-
2lottery-system/lottery-pojo/pom.xml
-
21lottery-system/lottery-pojo/src/main/java/com/lottery/dto/UserLoginDto.java
-
7lottery-system/lottery-pojo/src/main/java/com/lottery/entity/User.java
-
29lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserLoginVo.java
-
18lottery-system/lottery-pojo/src/main/java/com/lottery/vo/UserVo.java
-
12lottery-system/lottery-service/pom.xml
-
85lottery-system/lottery-service/src/main/java/com/lottery/controller/api/UserController.java
-
19lottery-system/lottery-service/src/main/java/com/lottery/interceptor/JwtTokenUserInterceptor.java
-
22lottery-system/lottery-service/src/main/java/com/lottery/mapper/IUserMapper.java
-
21lottery-system/lottery-service/src/main/java/com/lottery/service/IUserService.java
-
49lottery-system/lottery-service/src/main/java/com/lottery/service/Impl/UserServiceImpl.java
-
9lottery-system/lottery-service/src/main/resources/application.yml
-
5lottery-system/lottery-service/src/main/resources/mapper/userMapper.xml
-
20lottery-system/pom.xml
@ -0,0 +1,17 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true"> |
||||
|
<data-source source="LOCAL" name="lottery_system@localhost" uuid="296b2e99-571b-4184-8fa0-ad8c7969124e"> |
||||
|
<driver-ref>mysql.8</driver-ref> |
||||
|
<synchronize>true</synchronize> |
||||
|
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver> |
||||
|
<jdbc-url>jdbc:mysql://localhost:3306/lottery_system</jdbc-url> |
||||
|
<jdbc-additional-properties> |
||||
|
<property name="com.intellij.clouds.kubernetes.db.host.port" /> |
||||
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" /> |
||||
|
<property name="com.intellij.clouds.kubernetes.db.container.port" /> |
||||
|
</jdbc-additional-properties> |
||||
|
<working-dir>$ProjectFileDir$</working-dir> |
||||
|
</data-source> |
||||
|
</component> |
||||
|
</project> |
@ -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); |
||||
|
} |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.lottery.exception; |
||||
|
|
||||
|
/** |
||||
|
* 业务异常 |
||||
|
*/ |
||||
|
public class BaseException extends RuntimeException { |
||||
|
|
||||
|
public BaseException() { |
||||
|
} |
||||
|
|
||||
|
public BaseException(String msg) { |
||||
|
super(msg); |
||||
|
} |
||||
|
|
||||
|
} |
@ -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; |
||||
|
} |
@ -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<String, Object> 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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -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; |
||||
|
} |
@ -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; |
||||
|
} |
@ -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; |
||||
|
} |
@ -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<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto){ |
||||
|
|
||||
|
LOGGER.info("用户登录 :{}", userLoginDto); |
||||
|
User user = userService.login(userLoginDto); |
||||
|
if (user == null) { |
||||
|
throw new Accountexception("密码错误"); |
||||
|
} |
||||
|
|
||||
|
//登录成功,生成token令牌 |
||||
|
HashMap<String, Object> 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<List<UserVo>> getAllUser(){ |
||||
|
LOGGER.info("查询所有用户"); |
||||
|
|
||||
|
List<User> list = userService.list(); |
||||
|
List<UserVo> list1 = new ArrayList<>(); |
||||
|
|
||||
|
for (User user : list) { |
||||
|
|
||||
|
} |
||||
|
return Result.success(list1); |
||||
|
} |
||||
|
} |
@ -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 { |
||||
|
|
||||
|
|
||||
|
} |
@ -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<User> { |
||||
|
|
||||
|
|
||||
|
@Select("select id, username, password, status from user where username = #{username}") |
||||
|
User getByUsername(String username); |
||||
|
} |
@ -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> { |
||||
|
User login(UserLoginDto userLoginDto); |
||||
|
|
||||
|
} |
@ -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<IUserMapper, User> 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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
<mapper namespace="com.lottery.mapper.IUserMapper"> |
||||
|
|
||||
|
</mapper> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue