From a9f2991df176f43113cf98a58e8d90e7ad9981a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E8=B4=BA=E8=B4=BA?= <1510379943@qq.com> Date: Tue, 19 Nov 2024 00:26:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(api):=20=E6=B7=BB=E5=8A=A0=20OpenAPI=20?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=B9=B6=E4=BC=98=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 OpenAPI 依赖并配置 Swagger UI- 更新数据库连接配置 - 修改日志级别配置 - 更新 JWT 密钥 - 优化用户相关 API 路径 - 添加 MyBatis mapper 扫描 --- .idea/.gitignore | 8 -------- trading_platform/pom.xml | 13 +++++++++++++ .../TradingPlatformApplication.java | 11 ++++++++--- .../trading_platform/config/WebConfig.java | 2 +- .../controller/UsersController.java | 4 ++-- .../trading_platform/mapper/UsersMapper.java | 5 +++-- .../com/trading_platform/utils/JwtUtil.java | 2 +- .../src/main/resources/application.yml | 12 +++++++++++- .../target/classes/application.yml | 12 +++++++++++- .../TradingPlatformApplication.class | Bin 774 -> 1040 bytes .../trading_platform/config/WebConfig.class | Bin 1544 -> 1595 bytes .../controller/UsersController.class | Bin 3092 -> 3093 bytes .../com/trading_platform/utils/JwtUtil.class | Bin 2201 -> 2199 bytes 13 files changed, 50 insertions(+), 19 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/trading_platform/pom.xml b/trading_platform/pom.xml index 8a7948d..0401374 100644 --- a/trading_platform/pom.xml +++ b/trading_platform/pom.xml @@ -76,6 +76,19 @@ java-jwt 4.4.0 + + + org.springdoc + springdoc-openapi-starter-webmvc-api + 2.6.0 + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.6.0 + + diff --git a/trading_platform/src/main/java/com/trading_platform/TradingPlatformApplication.java b/trading_platform/src/main/java/com/trading_platform/TradingPlatformApplication.java index 5f00c81..e65b7a3 100644 --- a/trading_platform/src/main/java/com/trading_platform/TradingPlatformApplication.java +++ b/trading_platform/src/main/java/com/trading_platform/TradingPlatformApplication.java @@ -1,13 +1,18 @@ package com.trading_platform; +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@OpenAPIDefinition(info = @Info(title = "Trading Platform API", version = "1.0")) +@MapperScan("com.trading_platform.mapper") public class TradingPlatformApplication { - public static void main(String[] args) { - SpringApplication.run(TradingPlatformApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(TradingPlatformApplication.class, args); + } } diff --git a/trading_platform/src/main/java/com/trading_platform/config/WebConfig.java b/trading_platform/src/main/java/com/trading_platform/config/WebConfig.java index 0b4ed19..fea285c 100644 --- a/trading_platform/src/main/java/com/trading_platform/config/WebConfig.java +++ b/trading_platform/src/main/java/com/trading_platform/config/WebConfig.java @@ -33,7 +33,7 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // 登录和注册路径不进行拦截,以允许未登录用户访问这些路径 - registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login", "/user/register"); + registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login", "/user/register", "/swagger-ui/**", "/v3/api-docs/**"); } } diff --git a/trading_platform/src/main/java/com/trading_platform/controller/UsersController.java b/trading_platform/src/main/java/com/trading_platform/controller/UsersController.java index 2f26833..b55a4e4 100644 --- a/trading_platform/src/main/java/com/trading_platform/controller/UsersController.java +++ b/trading_platform/src/main/java/com/trading_platform/controller/UsersController.java @@ -20,7 +20,7 @@ import java.util.Map; * 它作为前端控制器,将请求委托给UserService进行处理 */ @RestController -@RequestMapping("/user") +@RequestMapping("/users") @Validated /** * UserController类负责处理与用户相关的控制逻辑 @@ -82,7 +82,7 @@ public class UsersController { return Result.error("用户名错误"); } - // 比较密码,使用MD5加密后的密码与数据库中存储的密码进行比较 + // 比较密码,使用SHA256加密后的密码与数据库中存储的密码进行比较 if (Sha256Util.getSHA256String(password).equals(loginUser.getPassword())) { // 如果密码正确,返回成功信息及JWT令牌 Map claims = new HashMap<>(); diff --git a/trading_platform/src/main/java/com/trading_platform/mapper/UsersMapper.java b/trading_platform/src/main/java/com/trading_platform/mapper/UsersMapper.java index 2bb250e..8d76cee 100644 --- a/trading_platform/src/main/java/com/trading_platform/mapper/UsersMapper.java +++ b/trading_platform/src/main/java/com/trading_platform/mapper/UsersMapper.java @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; + import java.util.Optional; /** @@ -27,8 +28,8 @@ public interface UsersMapper { * 向数据库中插入一个新用户 * * @param user 要插入的用户对象,包含用户的所有信息 - * 使用MyBatis的@Insert注解定义插入操作的SQL语句 - * 使用@Options注解配置插入操作生成的主键返回到user对象的id属性 + * 使用MyBatis的@Insert注解定义插入操作的SQL语句 + * 使用@Options注解配置插入操作生成的主键返回到user对象的id属性 */ @Insert("INSERT INTO users (headPicUrl, nickName, username, mobile, password, sex, email, address, status, create_time, update_time) " + "VALUES (#{headPicUrl}, #{nickName}, #{username}, #{mobile}, #{password}, #{sex}, #{email}, #{address}, #{status}, #{createTime}, #{updateTime})") diff --git a/trading_platform/src/main/java/com/trading_platform/utils/JwtUtil.java b/trading_platform/src/main/java/com/trading_platform/utils/JwtUtil.java index 02b2ebd..94b6bd9 100644 --- a/trading_platform/src/main/java/com/trading_platform/utils/JwtUtil.java +++ b/trading_platform/src/main/java/com/trading_platform/utils/JwtUtil.java @@ -7,7 +7,7 @@ import java.util.Date; import java.util.Map; public class JwtUtil { - private static final String KEY = "itheima"; + private static final String KEY = "slqcz"; /** * 生成JWT令牌 diff --git a/trading_platform/src/main/resources/application.yml b/trading_platform/src/main/resources/application.yml index b1f565d..ed20da9 100644 --- a/trading_platform/src/main/resources/application.yml +++ b/trading_platform/src/main/resources/application.yml @@ -3,4 +3,14 @@ spring: name: localhost url: jdbc:mysql://localhost:3306/secondhandtransactions?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root - password: 123456 \ No newline at end of file + password: 123456 +springdoc: + api-docs: + path: /v3/api-docs + swagger-ui: + path: /swagger-ui.html + +logging: + level: + root: debug + com.trading_platform: debug \ No newline at end of file diff --git a/trading_platform/target/classes/application.yml b/trading_platform/target/classes/application.yml index b1f565d..ed20da9 100644 --- a/trading_platform/target/classes/application.yml +++ b/trading_platform/target/classes/application.yml @@ -3,4 +3,14 @@ spring: name: localhost url: jdbc:mysql://localhost:3306/secondhandtransactions?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root - password: 123456 \ No newline at end of file + password: 123456 +springdoc: + api-docs: + path: /v3/api-docs + swagger-ui: + path: /swagger-ui.html + +logging: + level: + root: debug + com.trading_platform: debug \ No newline at end of file diff --git a/trading_platform/target/classes/com/trading_platform/TradingPlatformApplication.class b/trading_platform/target/classes/com/trading_platform/TradingPlatformApplication.class index fc601f52ff9cf96cfa323f779fc88e2a046ba062..233acbf6c0c7b0fbf374a8b1d0c8bf1f62ff4df2 100644 GIT binary patch delta 336 zcmaivy-EW?6ot=h+(d$)g`IyJ|Dqy0hLk~6un9yBXlEf~oLvTXXO@|9#VVbx!aj+u zg%4os`-pe7v9meFz27c@yd!jrPFAf37<@j%_B-@ zXqD1BN@r;F;sb`gk+CVyA8)CKff$?3=9mm46+-!{Fo*?GMh27Pp)9LXXRpGXW*W^hgpZ6je(IthJgV9 DGWH4o diff --git a/trading_platform/target/classes/com/trading_platform/config/WebConfig.class b/trading_platform/target/classes/com/trading_platform/config/WebConfig.class index 4ff8a85700d467827d49b984bfbd446da9eeebc5..245140aeb0b243d04255d3512dae265c3d84a2fd 100644 GIT binary patch delta 347 zcmYLD%SyvQ6g@LdnleVk6cyjE#OlPDw$X(P5k&;oSx6QxN|n-8i$z`P(tW<9s1+3b z0JnaGe<7a6QWrD#o|*G{u-|O;`Sbb#U?00(^(FQSm=zccJGZq)a=hz|_A(vbkhS|&;WMgOI-5B0vUKVAy?bNO@ zL>v<;>PY+4acN}lzWr>fcOnO8c*GoOWg=JABm}g$Rn1kNH?*aFBLjCj1+_Y=hAurt z8q%o%dD!;4Ulf;={2wYKH#$v6UQ|n$#2<4#JBU5h;(Rs{wsiouJFunC{T|%kCLgx< J