From bcbb401b57132226010ea57fe31450d70a8a2497 Mon Sep 17 00:00:00 2001
From: wol <1293433164@qq.com>
Date: Sun, 22 Dec 2024 21:20:30 +0800
Subject: [PATCH] 015
---
weblog-springboot-015/pom.xml | 167 ++++++++++++++++++
.../weblog-module-admin/.gitignore | 33 ++++
.../weblog-module-admin/pom.xml | 56 ++++++
.../admin/config/Knife4jAdminConfig.java | 53 ++++++
.../admin/config/WebSecurityConfig.java | 63 +++++++
.../quanxiaoha/weblog/admin/package-info.java | 7 +
.../WeblogModuleAdminApplicationTests.java | 13 ++
.../weblog-module-common/.gitignore | 33 ++++
.../weblog-module-common/pom.xml | 70 ++++++++
.../weblog/common/aspect/ApiOperationLog.java | 17 ++
.../common/aspect/ApiOperationLogAspect.java | 102 +++++++++++
.../weblog/common/config/JacksonConfig.java | 55 ++++++
.../common/config/MybatisPlusConfig.java | 15 ++
.../weblog/common/domain/dos/UserDO.java | 38 ++++
.../common/domain/mapper/UserMapper.java | 19 ++
.../weblog/common/domain/package-info.java | 7 +
.../weblog/common/enums/ResponseCodeEnum.java | 33 ++++
.../exception/BaseExceptionInterface.java | 13 ++
.../weblog/common/exception/BizException.java | 24 +++
.../exception/GlobalExceptionHandler.java | 85 +++++++++
.../weblog/common/package-info.java | 7 +
.../weblog/common/utils/JsonUtil.java | 27 +++
.../weblog/common/utils/Response.java | 77 ++++++++
.../WeblogModuleCommonApplicationTests.java | 19 ++
.../weblog-module-jwt/.gitignore | 33 ++++
.../weblog-module-jwt/pom.xml | 70 ++++++++
.../JwtAuthenticationSecurityConfig.java | 58 ++++++
.../jwt/config/PasswordEncoderConfig.java | 27 +++
.../UsernameOrPasswordNullException.java | 19 ++
.../jwt/filter/JwtAuthenticationFilter.java | 58 ++++++
.../jwt/filter/TokenAuthenticationFilter.java | 101 +++++++++++
.../jwt/handler/RestAccessDeniedHandler.java | 28 +++
.../handler/RestAuthenticationEntryPoint.java | 38 ++++
.../RestAuthenticationFailureHandler.java | 43 +++++
.../RestAuthenticationSuccessHandler.java | 47 +++++
.../weblog/jwt/model/LoginRspVO.java | 25 +++
.../jwt/service/UserDetailServiceImpl.java | 46 +++++
.../weblog/jwt/utils/JwtTokenHelper.java | 146 +++++++++++++++
.../weblog/jwt/utils/ResultUtil.java | 73 ++++++++
.../jwt/WeblogModuleJwtApplicationTests.java | 13 ++
weblog-springboot-015/weblog-web/.gitignore | 33 ++++
weblog-springboot-015/weblog-web/pom.xml | 75 ++++++++
.../weblog/web/WeblogWebApplication.java | 15 ++
.../weblog/web/config/Knife4jConfig.java | 53 ++++++
.../weblog/web/controller/TestController.java | 47 +++++
.../com/quanxiaoha/weblog/web/model/User.java | 50 ++++++
.../src/main/resources/application-dev.yml | 21 +++
.../src/main/resources/application-prod.yml | 24 +++
.../src/main/resources/application.yml | 18 ++
.../src/main/resources/logback-weblog.xml | 46 +++++
.../src/main/resources/spy.properties | 24 +++
.../weblog/web/WeblogWebApplicationTests.java | 49 +++++
52 files changed, 2313 insertions(+)
create mode 100644 weblog-springboot-015/pom.xml
create mode 100644 weblog-springboot-015/weblog-module-admin/.gitignore
create mode 100644 weblog-springboot-015/weblog-module-admin/pom.xml
create mode 100644 weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/Knife4jAdminConfig.java
create mode 100644 weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/WebSecurityConfig.java
create mode 100644 weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/package-info.java
create mode 100644 weblog-springboot-015/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java
create mode 100644 weblog-springboot-015/weblog-module-common/.gitignore
create mode 100644 weblog-springboot-015/weblog-module-common/pom.xml
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/config/JacksonConfig.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/config/MybatisPlusConfig.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/domain/dos/UserDO.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/domain/mapper/UserMapper.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/domain/package-info.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/enums/ResponseCodeEnum.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BaseExceptionInterface.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BizException.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/GlobalExceptionHandler.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/package-info.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/JsonUtil.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/Response.java
create mode 100644 weblog-springboot-015/weblog-module-common/src/test/java/com/quanxiaoha/weblog/common/WeblogModuleCommonApplicationTests.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/.gitignore
create mode 100644 weblog-springboot-015/weblog-module-jwt/pom.xml
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/config/JwtAuthenticationSecurityConfig.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/config/PasswordEncoderConfig.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/exception/UsernameOrPasswordNullException.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/filter/JwtAuthenticationFilter.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/filter/TokenAuthenticationFilter.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/handler/RestAccessDeniedHandler.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/handler/RestAuthenticationEntryPoint.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/handler/RestAuthenticationFailureHandler.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/handler/RestAuthenticationSuccessHandler.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/model/LoginRspVO.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/service/UserDetailServiceImpl.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/utils/JwtTokenHelper.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/main/java/com/quanxiaoha/weblog/jwt/utils/ResultUtil.java
create mode 100644 weblog-springboot-015/weblog-module-jwt/src/test/java/com/quanxiaoha/weblog/jwt/WeblogModuleJwtApplicationTests.java
create mode 100644 weblog-springboot-015/weblog-web/.gitignore
create mode 100644 weblog-springboot-015/weblog-web/pom.xml
create mode 100644 weblog-springboot-015/weblog-web/src/main/java/com/quanxiaoha/weblog/web/WeblogWebApplication.java
create mode 100644 weblog-springboot-015/weblog-web/src/main/java/com/quanxiaoha/weblog/web/config/Knife4jConfig.java
create mode 100644 weblog-springboot-015/weblog-web/src/main/java/com/quanxiaoha/weblog/web/controller/TestController.java
create mode 100644 weblog-springboot-015/weblog-web/src/main/java/com/quanxiaoha/weblog/web/model/User.java
create mode 100644 weblog-springboot-015/weblog-web/src/main/resources/application-dev.yml
create mode 100644 weblog-springboot-015/weblog-web/src/main/resources/application-prod.yml
create mode 100644 weblog-springboot-015/weblog-web/src/main/resources/application.yml
create mode 100644 weblog-springboot-015/weblog-web/src/main/resources/logback-weblog.xml
create mode 100644 weblog-springboot-015/weblog-web/src/main/resources/spy.properties
create mode 100644 weblog-springboot-015/weblog-web/src/test/java/com/quanxiaoha/weblog/web/WeblogWebApplicationTests.java
diff --git a/weblog-springboot-015/pom.xml b/weblog-springboot-015/pom.xml
new file mode 100644
index 0000000..f8aa5a3
--- /dev/null
+++ b/weblog-springboot-015/pom.xml
@@ -0,0 +1,167 @@
+
+
+ 4.0.0
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+
+ 2.6.3
+
+
+
+ com.quanxiaoha
+ weblog-springboot
+ ${revision}
+ weblog-springboot
+
+ 前后端分离博客 Weblog By 犬小哈
+
+
+ pom
+
+
+
+
+ weblog-web
+
+ weblog-module-admin
+
+ weblog-module-common
+
+ weblog-module-jwt
+
+
+
+
+
+
+ 0.0.1-SNAPSHOT
+ 1.8
+ UTF-8
+
+ ${java.version}
+ ${java.version}
+
+
+ 1.18.28
+ 31.1-jre
+ 3.12.0
+ 2.15.2
+ 4.3.0
+ 3.5.2
+ 3.9.1
+ 0.11.2
+
+
+
+
+
+
+ com.quanxiaoha
+ weblog-module-admin
+ ${revision}
+
+
+
+ com.quanxiaoha
+ weblog-module-common
+ ${revision}
+
+
+
+ com.quanxiaoha
+ weblog-module-jwt
+ ${revision}
+
+
+
+
+ com.google.guava
+ guava
+ ${guava.version}
+
+
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ ${jackson.version}
+
+
+
+
+ com.github.xiaoymin
+ knife4j-openapi2-spring-boot-starter
+ ${knife4j.version}
+
+
+
+
+ com.baomidou
+ mybatis-plus-boot-starter
+ ${mybatis-plus.version}
+
+
+
+ p6spy
+ p6spy
+ ${p6spy.version}
+
+
+
+
+ io.jsonwebtoken
+ jjwt-api
+ ${jjwt.version}
+
+
+ io.jsonwebtoken
+ jjwt-impl
+ ${jjwt.version}
+
+
+ io.jsonwebtoken
+ jjwt-jackson
+ ${jjwt.version}
+
+
+
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+
+
+
+
+
+
+ aliyunmaven
+ aliyun
+ https://maven.aliyun.com/repository/public
+
+
+
diff --git a/weblog-springboot-015/weblog-module-admin/.gitignore b/weblog-springboot-015/weblog-module-admin/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/weblog-springboot-015/weblog-module-admin/pom.xml b/weblog-springboot-015/weblog-module-admin/pom.xml
new file mode 100644
index 0000000..b6a3c4e
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/pom.xml
@@ -0,0 +1,56 @@
+
+
+ 4.0.0
+
+
+ com.quanxiaoha
+ weblog-springboot
+ ${revision}
+
+
+ com.quanxiaoha
+ weblog-module-admin
+ weblog-module-admin
+ weblog-admin (负责管理后台相关功能)
+
+
+
+ com.quanxiaoha
+ weblog-module-common
+
+
+
+ com.quanxiaoha
+ weblog-module-jwt
+
+
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+ com.github.xiaoymin
+ knife4j-openapi2-spring-boot-starter
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
+
+
\ No newline at end of file
diff --git a/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/Knife4jAdminConfig.java b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/Knife4jAdminConfig.java
new file mode 100644
index 0000000..3c2b2da
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/Knife4jAdminConfig.java
@@ -0,0 +1,53 @@
+package com.quanxiaoha.weblog.admin.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Profile;
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.service.Contact;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
+
+/**
+ * @author: 犬小哈
+ * @url: www.quanxiaoha.com
+ * @date: 2023-08-16 7:53
+ * @description: Knife4j 配置
+ **/
+@Configuration
+@EnableSwagger2WebMvc
+@Profile("dev") // 只在 dev 环境中开启
+public class Knife4jAdminConfig {
+
+ @Bean("adminApi")
+ public Docket createApiDoc() {
+ Docket docket = new Docket(DocumentationType.SWAGGER_2)
+ .apiInfo(buildApiInfo())
+ // 分组名称
+ .groupName("Admin 后台接口")
+ .select()
+ // 这里指定 Controller 扫描包路径
+ .apis(RequestHandlerSelectors.basePackage("com.quanxiaoha.weblog.admin.controller"))
+ .paths(PathSelectors.any())
+ .build();
+ return docket;
+ }
+
+ /**
+ * 构建 API 信息
+ * @return
+ */
+ private ApiInfo buildApiInfo() {
+ return new ApiInfoBuilder()
+ .title("Weblog 博客 Admin 后台接口文档") // 标题
+ .description("Weblog 是一款由 Spring Boot + Vue 3.2 + Vite 4.3 开发的前后端分离博客。") // 描述
+ .termsOfServiceUrl("https://www.quanxiaoha.com/") // API 服务条款
+ .contact(new Contact("犬小哈", "https://www.quanxiaoha.com", "871361652@qq.com")) // 联系人
+ .version("1.0") // 版本号
+ .build();
+ }
+}
diff --git a/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/WebSecurityConfig.java b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/WebSecurityConfig.java
new file mode 100644
index 0000000..f3215ab
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/config/WebSecurityConfig.java
@@ -0,0 +1,63 @@
+package com.quanxiaoha.weblog.admin.config;
+
+import com.quanxiaoha.weblog.jwt.config.JwtAuthenticationSecurityConfig;
+import com.quanxiaoha.weblog.jwt.filter.TokenAuthenticationFilter;
+import com.quanxiaoha.weblog.jwt.handler.RestAccessDeniedHandler;
+import com.quanxiaoha.weblog.jwt.handler.RestAuthenticationEntryPoint;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+
+/**
+ * @author: 犬小哈
+ * @url: www.quanxiaoha.com
+ * @date: 2023-08-23 15:48
+ * @description: Spring Security 配置类
+ **/
+@Configuration
+@EnableWebSecurity
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Autowired
+ private JwtAuthenticationSecurityConfig jwtAuthenticationSecurityConfig;
+ @Autowired
+ private RestAuthenticationEntryPoint authEntryPoint;
+ @Autowired
+ private RestAccessDeniedHandler deniedHandler;
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.csrf().disable(). // 禁用 csrf
+ formLogin().disable() // 禁用表单登录
+ .apply(jwtAuthenticationSecurityConfig) // 设置用户登录认证相关配置
+ .and()
+ .authorizeHttpRequests()
+ .mvcMatchers("/admin/**").authenticated() // 认证所有以 /admin 为前缀的 URL 资源
+ .anyRequest().permitAll() // 其他都需要放行,无需认证
+ .and()
+ .httpBasic().authenticationEntryPoint(authEntryPoint) // 处理用户未登录访问受保护的资源的情况
+ .and()
+ .exceptionHandling().accessDeniedHandler(deniedHandler) // 处理登录成功后访问受保护的资源,但是权限不够的情况
+ .and()
+ .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 前后端分离,无需创建会话
+ .and()
+ .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) // 将 Token 校验过滤器添加到用户认证过滤器之前
+ ;
+ }
+
+ /**
+ * Token 校验过滤器
+ * @return
+ */
+ @Bean
+ public TokenAuthenticationFilter tokenAuthenticationFilter() {
+ return new TokenAuthenticationFilter();
+ }
+
+}
diff --git a/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/package-info.java b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/package-info.java
new file mode 100644
index 0000000..8a825e4
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/src/main/java/com/quanxiaoha/weblog/admin/package-info.java
@@ -0,0 +1,7 @@
+/**
+ * @author: 犬小哈
+ * @url: www.quanxiaoha.com
+ * @date: 2023-08-16 9:28
+ * @description: TODO
+ **/
+package com.quanxiaoha.weblog.admin;
\ No newline at end of file
diff --git a/weblog-springboot-015/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java b/weblog-springboot-015/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java
new file mode 100644
index 0000000..dab9a4b
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java
@@ -0,0 +1,13 @@
+package com.quanxiaoha.weblog.admin;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class WeblogModuleAdminApplicationTests {
+
+ @Test
+ void contextLoads() {
+ }
+
+}
diff --git a/weblog-springboot-015/weblog-module-common/.gitignore b/weblog-springboot-015/weblog-module-common/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-common/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/weblog-springboot-015/weblog-module-common/pom.xml b/weblog-springboot-015/weblog-module-common/pom.xml
new file mode 100644
index 0000000..0e587e2
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-common/pom.xml
@@ -0,0 +1,70 @@
+
+
+ 4.0.0
+
+ com.quanxiaoha
+ weblog-springboot
+ ${revision}
+
+
+ com.quanxiaoha
+ weblog-module-common
+ weblog-module-common
+ weblog-module-common (此模块用于存放一些通用的功能)
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-aop
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+
+
+
+
+ com.baomidou
+ mybatis-plus-boot-starter
+
+
+
+
+ mysql
+ mysql-connector-java
+
+
+
+ p6spy
+ p6spy
+
+
+
+
+
+
+
+
+
diff --git a/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java b/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java
new file mode 100644
index 0000000..38eeb0f
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java
@@ -0,0 +1,17 @@
+package com.quanxiaoha.weblog.common.aspect;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.METHOD})
+@Documented
+public @interface ApiOperationLog {
+ /**
+ * API 功能描述
+ *
+ * @return
+ */
+ String description() default "";
+
+}
+
diff --git a/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java b/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java
new file mode 100644
index 0000000..ef199b4
--- /dev/null
+++ b/weblog-springboot-015/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java
@@ -0,0 +1,102 @@
+
+package com.quanxiaoha.weblog.common.aspect;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.quanxiaoha.weblog.common.utils.JsonUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.MDC;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.UUID;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+@Aspect
+@Component
+@Slf4j
+public class ApiOperationLogAspect {
+
+ /** 以自定义 @ApiOperationLog 注解为切点,凡是添加 @ApiOperationLog 的方法,都会执行环绕中的代码 */
+ @Pointcut("@annotation(com.quanxiaoha.weblog.common.aspect.ApiOperationLog)")
+ public void apiOperationLog() {}
+
+ /**
+ * 环绕
+ * @param joinPoint
+ * @return
+ * @throws Throwable
+ */
+ @Around("apiOperationLog()")
+ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
+ try {
+ // 请求开始时间
+ long startTime = System.currentTimeMillis();
+
+ // MDC
+ MDC.put("traceId", UUID.randomUUID().toString());
+
+ // 获取被请求的类和方法
+ String className = joinPoint.getTarget().getClass().getSimpleName();
+ String methodName = joinPoint.getSignature().getName();
+
+ // 请求入参
+ Object[] args = joinPoint.getArgs();
+ // 入参转 JSON 字符串
+ String argsJsonStr = Arrays.stream(args).map(toJsonStr()).collect(Collectors.joining(", "));
+
+ // 功能描述信息
+ String description = getApiOperationLogDescription(joinPoint);
+
+ // 打印请求相关参数
+ log.info("====== 请求开始: [{}], 入参: {}, 请求类: {}, 请求方法: {} =================================== ",
+ description, argsJsonStr, className, methodName);
+
+ // 执行切点方法
+ Object result = joinPoint.proceed();
+
+ // 执行耗时
+ long executionTime = System.currentTimeMillis() - startTime;
+
+ // 打印出参等相关信息
+ log.info("====== 请求结束: [{}], 耗时: {}ms, 出参: {} =================================== ",
+ description, executionTime, JsonUtil.toJsonString(result));
+
+ return result;
+ } finally {
+ MDC.clear();
+ }
+ }
+
+ /**
+ * 获取注解的描述信息
+ * @param joinPoint
+ * @return
+ */
+ private String getApiOperationLogDescription(ProceedingJoinPoint joinPoint) {
+ // 1. 从 ProceedingJoinPoint 获取 MethodSignature
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+
+ // 2. 使用 MethodSignature 获取当前被注解的 Method
+ Method method = signature.getMethod();
+
+ // 3. 从 Method 中提取 LogExecution 注解
+ ApiOperationLog apiOperationLog = method.getAnnotation(ApiOperationLog.class);
+
+ // 4. 从 LogExecution 注解中获取 description 属性
+ return apiOperationLog.description();
+ }
+
+ /**
+ * 转 JSON 字符串
+ * @return
+ */
+ private Function