From baa8bb57d8e1a4d60170904c40d149fcdfc1c460 Mon Sep 17 00:00:00 2001 From: chaos <7676076@qq.com> Date: Fri, 18 Jul 2025 15:11:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(chaos-api):=20=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=9F=BA=E7=A1=80=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ApiResult 类用于统一返回结果 - 添加应用配置文件和日志配置文件 - 配置 MyBatis-Plus 和 Druid 数据源 - 实现基本的安全配置,包括未授权和权限不足的处理 - 引入必要的依赖,如 lombok、fastjson2、MariaDB驱动等 --- chaos_api_common/pom.xml | 12 +++++- .../cn/nopj/chaos_api/model/ApiResult.java | 30 ++++++++++++++ chaos_api_data/pom.xml | 10 +++++ .../chaos_api/config/MyBatisPlusConfig.java | 9 +++++ .../sec/RestAuthenticationEntryPoint.java | 28 +++++++++++++ .../sec/RestfulAccessDeniedHandler.java | 29 ++++++++++++++ .../chaos_api/config/sec/SecurityConfig.java | 40 +++++++++++++++++++ .../src/main/resources/application.yaml | 28 +++++++++++++ .../src/main/resources/log4j2-spring.xml | 38 ++++++++++++++++++ pom.xml | 26 ++++++++++++ 10 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 chaos_api_common/src/main/java/cn/nopj/chaos_api/model/ApiResult.java create mode 100644 chaos_api_data/src/main/java/cn/nopj/chaos_api/config/MyBatisPlusConfig.java create mode 100644 chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestAuthenticationEntryPoint.java create mode 100644 chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestfulAccessDeniedHandler.java create mode 100644 chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/SecurityConfig.java create mode 100644 chaos_api_web/src/main/resources/application.yaml create mode 100644 chaos_api_web/src/main/resources/log4j2-spring.xml diff --git a/chaos_api_common/pom.xml b/chaos_api_common/pom.xml index 14197f9..7f56791 100644 --- a/chaos_api_common/pom.xml +++ b/chaos_api_common/pom.xml @@ -19,5 +19,15 @@ 21 UTF-8 - + + + org.projectlombok + lombok + true + + + com.alibaba.fastjson2 + fastjson2 + + \ No newline at end of file diff --git a/chaos_api_common/src/main/java/cn/nopj/chaos_api/model/ApiResult.java b/chaos_api_common/src/main/java/cn/nopj/chaos_api/model/ApiResult.java new file mode 100644 index 0000000..3d2eb54 --- /dev/null +++ b/chaos_api_common/src/main/java/cn/nopj/chaos_api/model/ApiResult.java @@ -0,0 +1,30 @@ +package cn.nopj.chaos_api.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ApiResult { + + private int code; + private String msg; + private T data; + + + + public static ApiResult success(T data) { + return new ApiResult<>(200, "success", data); + } + public static ApiResult success(String msg, T data) { + return new ApiResult<>(200, msg, data); + } + public static ApiResult failed(int code, String msg) { + return new ApiResult<>(code, msg, null); + } + public static ApiResult failed(String msg) { + return new ApiResult<>(500, msg, null); + } +} diff --git a/chaos_api_data/pom.xml b/chaos_api_data/pom.xml index 8a5ca58..bd564aa 100644 --- a/chaos_api_data/pom.xml +++ b/chaos_api_data/pom.xml @@ -40,5 +40,15 @@ com.mysql mysql-connector-j + + com.alibaba + druid-spring-boot-starter + + + + org.mariadb.jdbc + mariadb-java-client + 3.5.4 + \ No newline at end of file diff --git a/chaos_api_data/src/main/java/cn/nopj/chaos_api/config/MyBatisPlusConfig.java b/chaos_api_data/src/main/java/cn/nopj/chaos_api/config/MyBatisPlusConfig.java new file mode 100644 index 0000000..e0bfb09 --- /dev/null +++ b/chaos_api_data/src/main/java/cn/nopj/chaos_api/config/MyBatisPlusConfig.java @@ -0,0 +1,9 @@ +package cn.nopj.chaos_api.config; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@MapperScan("cn.nopj.chaos_api.mapper") +public class MyBatisPlusConfig { +} diff --git a/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestAuthenticationEntryPoint.java b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestAuthenticationEntryPoint.java new file mode 100644 index 0000000..1816734 --- /dev/null +++ b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestAuthenticationEntryPoint.java @@ -0,0 +1,28 @@ +package cn.nopj.chaos_api.config.sec; + +import cn.nopj.chaos_api.model.ApiResult; +import com.alibaba.fastjson2.JSONObject; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { + @Override + public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + ApiResult result = ApiResult.failed("未授权"); + + String string = JSONObject.toJSONString(result); + response.getWriter().print(string); + response.getWriter().flush(); + } +} diff --git a/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestfulAccessDeniedHandler.java b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestfulAccessDeniedHandler.java new file mode 100644 index 0000000..9d4b922 --- /dev/null +++ b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/RestfulAccessDeniedHandler.java @@ -0,0 +1,29 @@ +package cn.nopj.chaos_api.config.sec; + +import cn.nopj.chaos_api.model.ApiResult; +import com.alibaba.fastjson2.JSONObject; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.web.access.AccessDeniedHandler; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class RestfulAccessDeniedHandler implements AccessDeniedHandler { + @Override + public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 状态码 403 + + ApiResult result = ApiResult.failed(HttpServletResponse.SC_FORBIDDEN, "权限不足,请联系管理员"); + + response.getWriter().println(JSONObject.toJSONString( result)); + response.getWriter().flush(); + } + + +} diff --git a/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/SecurityConfig.java b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/SecurityConfig.java new file mode 100644 index 0000000..59f53de --- /dev/null +++ b/chaos_api_web/src/main/java/cn/nopj/chaos_api/config/sec/SecurityConfig.java @@ -0,0 +1,40 @@ +package cn.nopj.chaos_api.config.sec; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + + private final RestAuthenticationEntryPoint restAuthenticationEntryPoint; + private final RestfulAccessDeniedHandler restfulAccessDeniedHandler; + + public SecurityConfig(RestAuthenticationEntryPoint restAuthenticationEntryPoint, RestfulAccessDeniedHandler restfulAccessDeniedHandler) { + this.restAuthenticationEntryPoint = restAuthenticationEntryPoint; + this.restfulAccessDeniedHandler = restfulAccessDeniedHandler; + } + + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + http + .authorizeHttpRequests(auth -> auth + // 允许所有对 /api/public/** 的匿名访问 + .requestMatchers("/api/public/**").permitAll() + // 其他所有请求都需要认证 + .anyRequest().authenticated() + ) + // 禁用 CSRF,因为现代前后端分离项目通常使用 Token + .csrf(AbstractHttpConfigurer::disable) + .exceptionHandling(e -> e + .authenticationEntryPoint(restAuthenticationEntryPoint) + .accessDeniedHandler(restfulAccessDeniedHandler)) + ; + + return http.build(); + } +} diff --git a/chaos_api_web/src/main/resources/application.yaml b/chaos_api_web/src/main/resources/application.yaml new file mode 100644 index 0000000..083374b --- /dev/null +++ b/chaos_api_web/src/main/resources/application.yaml @@ -0,0 +1,28 @@ +server: + port: 18888 + +spring: + application: + name: chaos-api + datasource: + driver-class-name: org.mariadb.jdbc.Driver + url: jdbc:mysql://10.91.3.23:3306/chaos?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false + username: chaos + password: zx123456.. + type: com.alibaba.druid.pool.DruidDataSource + druid: + initial-size: 5 + min-idle: 5 + max-active: 20 + max-wait: 60000 + +mybatis-plus: + mapper-locations: classpath*:/mapper/**/*.xml + type-aliases-package: cn.nopj.chaos_api_domain.entity + global-config: + db-config: + id-type: assign_id + configuration: + map-underscore-to-camel-case: true + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + diff --git a/chaos_api_web/src/main/resources/log4j2-spring.xml b/chaos_api_web/src/main/resources/log4j2-spring.xml new file mode 100644 index 0000000..8b40a90 --- /dev/null +++ b/chaos_api_web/src/main/resources/log4j2-spring.xml @@ -0,0 +1,38 @@ + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 138bce8..db0c111 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,32 @@ mybatis-plus 3.5.12 + + + + com.alibaba + druid-spring-boot-starter + 1.2.25 + + + + + org.mariadb.jdbc + mariadb-java-client + 3.5.4 + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.57 + + + + org.projectlombok + lombok + 1.18.38 + \ No newline at end of file