New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-cloud-plus</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <modules> |
| | | <module>ruoyi-common-bom</module> |
| | | <module>ruoyi-common-alibaba-bom</module> |
| | | <module>ruoyi-common-log</module> |
| | | <module>ruoyi-common-dict</module> |
| | | <module>ruoyi-common-excel</module> |
| | | <module>ruoyi-common-core</module> |
| | | <module>ruoyi-common-redis</module> |
| | | <module>ruoyi-common-doc</module> |
| | | <module>ruoyi-common-security</module> |
| | | <module>ruoyi-common-satoken</module> |
| | | <module>ruoyi-common-web</module> |
| | | <module>ruoyi-common-mybatis</module> |
| | | <module>ruoyi-common-job</module> |
| | | <module>ruoyi-common-dubbo</module> |
| | | <module>ruoyi-common-seata</module> |
| | | <module>ruoyi-common-loadbalancer</module> |
| | | <module>ruoyi-common-oss</module> |
| | | <module>ruoyi-common-ratelimiter</module> |
| | | <module>ruoyi-common-idempotent</module> |
| | | <module>ruoyi-common-mail</module> |
| | | <module>ruoyi-common-sms</module> |
| | | <module>ruoyi-common-logstash</module> |
| | | <module>ruoyi-common-elasticsearch</module> |
| | | <module>ruoyi-common-sentinel</module> |
| | | <module>ruoyi-common-skylog</module> |
| | | <module>ruoyi-common-prometheus</module> |
| | | <module>ruoyi-common-translation</module> |
| | | <module>ruoyi-common-sensitive</module> |
| | | <module>ruoyi-common-json</module> |
| | | <module>ruoyi-common-encrypt</module> |
| | | <module>ruoyi-common-tenant</module> |
| | | <module>ruoyi-common-websocket</module> |
| | | <module>ruoyi-common-social</module> |
| | | </modules> |
| | | |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <packaging>pom</packaging> |
| | | |
| | | <description> |
| | | ruoyi-common通用模块 |
| | | </description> |
| | | |
| | | </project> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-alibaba-bom</artifactId> |
| | | <version>${revision}</version> |
| | | <packaging>pom</packaging> |
| | | |
| | | <description> |
| | | ruoyi-common-alibaba-bom alibaba依赖项 |
| | | </description> |
| | | |
| | | <properties> |
| | | <revision>2.1.2</revision> |
| | | <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version> |
| | | <sentinel.version>1.8.6</sentinel.version> |
| | | <seata.version>1.7.1</seata.version> |
| | | <nacos.client.version>2.2.1</nacos.client.version> |
| | | <dubbo.version>3.2.7</dubbo.version> |
| | | <spring.context.support.version>1.0.11</spring.context.support.version> |
| | | </properties> |
| | | <dependencyManagement> |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>com.alibaba.cloud</groupId> |
| | | <artifactId>spring-cloud-alibaba-dependencies</artifactId> |
| | | <version>${spring-cloud-alibaba.version}</version> |
| | | <type>pom</type> |
| | | <scope>import</scope> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.nacos</groupId> |
| | | <artifactId>nacos-client</artifactId> |
| | | <version>${nacos.client.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-core</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-parameter-flow-control</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-extension</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-apollo</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-zookeeper</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-nacos</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-redis</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-consul</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-web-servlet</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-transport-simple-http</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-annotation-aspectj</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-reactor-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-cluster-server-default</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-cluster-client-default</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-spring-webflux-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-api-gateway-adapter-common</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-spring-webmvc-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-dubbo-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-apache-dubbo-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-apache-dubbo3-adapter</artifactId> |
| | | <version>${sentinel.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>io.seata</groupId> |
| | | <artifactId>seata-spring-boot-starter</artifactId> |
| | | <version>${seata.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- Apache Dubbo 配置 --> |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-starter</artifactId> |
| | | <version>${dubbo.version}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-actuator</artifactId> |
| | | <version>${dubbo.version}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo</artifactId> |
| | | <version>${dubbo.version}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba.spring</groupId> |
| | | <artifactId>spring-context-support</artifactId> |
| | | <version>${spring.context.support.version}</version> |
| | | </dependency> |
| | | </dependencies> |
| | | </dependencyManagement> |
| | | </project> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-bom</artifactId> |
| | | <version>${revision}</version> |
| | | <packaging>pom</packaging> |
| | | |
| | | <description> |
| | | ruoyi-common-bom common依赖项 |
| | | </description> |
| | | |
| | | <properties> |
| | | <revision>2.1.2</revision> |
| | | </properties> |
| | | |
| | | <dependencyManagement> |
| | | <dependencies> |
| | | <!-- 核心模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 接口模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-doc</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 安全模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-security</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 日志记录 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-log</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 字典 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-dict</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- excel --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-excel</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 缓存服务 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- web服务 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-web</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 数据库服务 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-mybatis</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-job</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-dubbo</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-seata</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-loadbalancer</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-oss</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 限流 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-ratelimiter</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-idempotent</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-mail</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-sms</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-logstash</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-elasticsearch</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-sentinel</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-skylog</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-prometheus</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-translation</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 脱敏模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-sensitive</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 序列化模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-encrypt</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <!-- 租户模块 --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-tenant</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-websocket</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-social</artifactId> |
| | | <version>${revision}</version> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </dependencyManagement> |
| | | </project> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-core 核心模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <!-- Spring框架基本的核心工具 --> |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | | <artifactId>spring-context-support</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- SpringWeb模块 --> |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | | <artifactId>spring-web</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Hibernate Validator --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-validation</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-aop</artifactId> |
| | | </dependency> |
| | | |
| | | <!--常用工具类 --> |
| | | <dependency> |
| | | <groupId>org.apache.commons</groupId> |
| | | <artifactId>commons-lang3</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- servlet包 --> |
| | | <dependency> |
| | | <groupId>jakarta.servlet</groupId> |
| | | <artifactId>jakarta.servlet-api</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>io.swagger.core.v3</groupId> |
| | | <artifactId>swagger-annotations</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-http</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-extra</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- 自动生成YML配置关联JSON文件 --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-configuration-processor</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-properties-migrator</artifactId> |
| | | <scope>runtime</scope> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>io.github.linpeilie</groupId> |
| | | <artifactId>mapstruct-plus-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- 离线IP地址定位库 --> |
| | | <dependency> |
| | | <groupId>org.lionsoul</groupId> |
| | | <artifactId>ip2region</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.core.config; |
| | | |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.EnableAspectJAutoProxy; |
| | | |
| | | /** |
| | | * 程序注解配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | // 表示通过aop框架暴露该代理对象,AopContext能够访问 |
| | | @EnableAspectJAutoProxy(exposeProxy = true) |
| | | public class ApplicationConfig { |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.config; |
| | | |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import jakarta.annotation.PreDestroy; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.concurrent.BasicThreadFactory; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.Threads; |
| | | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.scheduling.annotation.AsyncConfigurer; |
| | | import org.springframework.scheduling.annotation.EnableAsync; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.concurrent.*; |
| | | |
| | | /** |
| | | * 异步配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @EnableAsync(proxyTargetClass = true) |
| | | @AutoConfiguration |
| | | public class AsyncConfig implements AsyncConfigurer { |
| | | |
| | | private final int corePoolSize = Runtime.getRuntime().availableProcessors() + 1; |
| | | private ScheduledExecutorService scheduledExecutorService; |
| | | |
| | | /** |
| | | * 执行周期性或定时任务 |
| | | */ |
| | | @Bean(name = "scheduledExecutorService") |
| | | public ScheduledExecutorService scheduledExecutorService() { |
| | | ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize, |
| | | new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(), |
| | | new ThreadPoolExecutor.CallerRunsPolicy()) { |
| | | @Override |
| | | protected void afterExecute(Runnable r, Throwable t) { |
| | | super.afterExecute(r, t); |
| | | printException(r, t); |
| | | } |
| | | }; |
| | | this.scheduledExecutorService = scheduledThreadPoolExecutor; |
| | | return scheduledThreadPoolExecutor; |
| | | } |
| | | |
| | | /** |
| | | * 销毁事件 |
| | | */ |
| | | @PreDestroy |
| | | public void destroy() { |
| | | try { |
| | | log.info("====关闭后台任务任务线程池===="); |
| | | Threads.shutdownAndAwaitTermination(scheduledExecutorService); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 自定义 @Async 注解使用系统线程池 |
| | | */ |
| | | @Override |
| | | public Executor getAsyncExecutor() { |
| | | return SpringUtils.getBean("scheduledExecutorService"); |
| | | } |
| | | |
| | | /** |
| | | * 异步执行异常处理 |
| | | */ |
| | | @Override |
| | | public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { |
| | | return (throwable, method, objects) -> { |
| | | throwable.printStackTrace(); |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append("Exception message - ").append(throwable.getMessage()) |
| | | .append(", Method name - ").append(method.getName()); |
| | | if (ArrayUtil.isNotEmpty(objects)) { |
| | | sb.append(", Parameter value - ").append(Arrays.toString(objects)); |
| | | } |
| | | throw new ServiceException(sb.toString()); |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 打印线程异常信息 |
| | | */ |
| | | public void printException(Runnable r, Throwable t) { |
| | | if (t == null && r instanceof Future<?>) { |
| | | try { |
| | | Future<?> future = (Future<?>) r; |
| | | if (future.isDone()) { |
| | | future.get(); |
| | | } |
| | | } catch (CancellationException ce) { |
| | | t = ce; |
| | | } catch (ExecutionException ee) { |
| | | t = ee.getCause(); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | } |
| | | if (t != null) { |
| | | log.error(t.getMessage(), t); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.config; |
| | | |
| | | import jakarta.validation.Validator; |
| | | import org.hibernate.validator.HibernateValidator; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.MessageSource; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| | | |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * 校验框架配置类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | public class ValidatorConfig { |
| | | |
| | | /** |
| | | * 配置校验框架 快速返回模式 |
| | | */ |
| | | @Bean |
| | | public Validator validator(MessageSource messageSource) { |
| | | try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) { |
| | | // 国际化 |
| | | factoryBean.setValidationMessageSource(messageSource); |
| | | // 设置使用 HibernateValidator 校验器 |
| | | factoryBean.setProviderClass(HibernateValidator.class); |
| | | Properties properties = new Properties(); |
| | | // 设置 快速异常返回 |
| | | properties.setProperty("hibernate.validator.fail_fast", "true"); |
| | | factoryBean.setValidationProperties(properties); |
| | | // 加载配置 |
| | | factoryBean.afterPropertiesSet(); |
| | | return factoryBean.getValidator(); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 缓存的key 常量 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface CacheConstants { |
| | | |
| | | /** |
| | | * 在线用户 redis key |
| | | */ |
| | | String ONLINE_TOKEN_KEY = "online_tokens:"; |
| | | |
| | | /** |
| | | * 参数管理 cache key |
| | | */ |
| | | String SYS_CONFIG_KEY = "sys_config:"; |
| | | |
| | | /** |
| | | * 字典管理 cache key |
| | | */ |
| | | String SYS_DICT_KEY = "sys_dict:"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 缓存组名称常量 |
| | | * <p> |
| | | * key 格式为 cacheNames#ttl#maxIdleTime#maxSize |
| | | * <p> |
| | | * ttl 过期时间 如果设置为0则不过期 默认为0 |
| | | * maxIdleTime 最大空闲时间 根据LRU算法清理空闲数据 如果设置为0则不检测 默认为0 |
| | | * maxSize 组最大长度 根据LRU算法清理溢出数据 如果设置为0则无限长 默认为0 |
| | | * <p> |
| | | * 例子: test#60s、test#0#60s、test#0#1m#1000、test#1h#0#500 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface CacheNames { |
| | | |
| | | /** |
| | | * 演示案例 |
| | | */ |
| | | String DEMO_CACHE = "demo:cache#60s#10m#20"; |
| | | |
| | | /** |
| | | * 系统配置 |
| | | */ |
| | | String SYS_CONFIG = "sys_config"; |
| | | |
| | | /** |
| | | * 数据字典 |
| | | */ |
| | | String SYS_DICT = "sys_dict"; |
| | | |
| | | /** |
| | | * 租户 |
| | | */ |
| | | String SYS_TENANT = GlobalConstants.GLOBAL_REDIS_KEY + "sys_tenant#30d"; |
| | | |
| | | /** |
| | | * 用户账户 |
| | | */ |
| | | String SYS_USER_NAME = "sys_user_name#30d"; |
| | | |
| | | /** |
| | | * 用户名称 |
| | | */ |
| | | String SYS_NICKNAME = "sys_nickname#30d"; |
| | | |
| | | /** |
| | | * 部门 |
| | | */ |
| | | String SYS_DEPT = "sys_dept#30d"; |
| | | |
| | | /** |
| | | * OSS内容 |
| | | */ |
| | | String SYS_OSS = "sys_oss#30d"; |
| | | |
| | | /** |
| | | * OSS配置 |
| | | */ |
| | | String SYS_OSS_CONFIG = GlobalConstants.GLOBAL_REDIS_KEY + "sys_oss_config"; |
| | | |
| | | /** |
| | | * 在线用户 |
| | | */ |
| | | String ONLINE_TOKEN = "online_tokens"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 通用常量信息 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public interface Constants { |
| | | |
| | | /** |
| | | * UTF-8 字符集 |
| | | */ |
| | | String UTF8 = "UTF-8"; |
| | | |
| | | /** |
| | | * GBK 字符集 |
| | | */ |
| | | String GBK = "GBK"; |
| | | |
| | | /** |
| | | * www主域 |
| | | */ |
| | | String WWW = "www."; |
| | | |
| | | /** |
| | | * http请求 |
| | | */ |
| | | String HTTP = "http://"; |
| | | |
| | | /** |
| | | * https请求 |
| | | */ |
| | | String HTTPS = "https://"; |
| | | |
| | | /** |
| | | * 通用成功标识 |
| | | */ |
| | | String SUCCESS = "0"; |
| | | |
| | | /** |
| | | * 通用失败标识 |
| | | */ |
| | | String FAIL = "1"; |
| | | |
| | | /** |
| | | * 登录成功 |
| | | */ |
| | | String LOGIN_SUCCESS = "Success"; |
| | | |
| | | /** |
| | | * 注销 |
| | | */ |
| | | String LOGOUT = "Logout"; |
| | | |
| | | /** |
| | | * 注册 |
| | | */ |
| | | String REGISTER = "Register"; |
| | | |
| | | /** |
| | | * 登录失败 |
| | | */ |
| | | String LOGIN_FAIL = "Error"; |
| | | |
| | | /** |
| | | * 验证码有效期(分钟) |
| | | */ |
| | | Integer CAPTCHA_EXPIRATION = 2; |
| | | |
| | | /** |
| | | * 令牌 |
| | | */ |
| | | String TOKEN = "token"; |
| | | |
| | | /** |
| | | * 顶级部门id |
| | | */ |
| | | Long TOP_PARENT_ID = 0L; |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 全局的key常量 (业务无关的key) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface GlobalConstants { |
| | | |
| | | /** |
| | | * 全局 redis key (业务无关的key) |
| | | */ |
| | | String GLOBAL_REDIS_KEY = "global:"; |
| | | |
| | | /** |
| | | * 验证码 redis key |
| | | */ |
| | | String CAPTCHA_CODE_KEY = GLOBAL_REDIS_KEY + "captcha_codes:"; |
| | | |
| | | /** |
| | | * 防重提交 redis key |
| | | */ |
| | | String REPEAT_SUBMIT_KEY = GLOBAL_REDIS_KEY + "repeat_submit:"; |
| | | |
| | | /** |
| | | * 限流 redis key |
| | | */ |
| | | String RATE_LIMIT_KEY = GLOBAL_REDIS_KEY + "rate_limit:"; |
| | | |
| | | /** |
| | | * 登录账户密码错误次数 redis key |
| | | */ |
| | | String PWD_ERR_CNT_KEY = GLOBAL_REDIS_KEY + "pwd_err_cnt:"; |
| | | |
| | | /** |
| | | * 三方认证 redis key |
| | | */ |
| | | String SOCIAL_AUTH_CODE_KEY = GLOBAL_REDIS_KEY + "social_auth_codes:"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 返回状态码 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface HttpStatus { |
| | | /** |
| | | * 操作成功 |
| | | */ |
| | | int SUCCESS = 200; |
| | | |
| | | /** |
| | | * 对象创建成功 |
| | | */ |
| | | int CREATED = 201; |
| | | |
| | | /** |
| | | * 请求已经被接受 |
| | | */ |
| | | int ACCEPTED = 202; |
| | | |
| | | /** |
| | | * 操作已经执行成功,但是没有返回数据 |
| | | */ |
| | | int NO_CONTENT = 204; |
| | | |
| | | /** |
| | | * 资源已被移除 |
| | | */ |
| | | int MOVED_PERM = 301; |
| | | |
| | | /** |
| | | * 重定向 |
| | | */ |
| | | int SEE_OTHER = 303; |
| | | |
| | | /** |
| | | * 资源没有被修改 |
| | | */ |
| | | int NOT_MODIFIED = 304; |
| | | |
| | | /** |
| | | * 参数列表错误(缺少,格式不匹配) |
| | | */ |
| | | int BAD_REQUEST = 400; |
| | | |
| | | /** |
| | | * 未授权 |
| | | */ |
| | | int UNAUTHORIZED = 401; |
| | | |
| | | /** |
| | | * 访问受限,授权过期 |
| | | */ |
| | | int FORBIDDEN = 403; |
| | | |
| | | /** |
| | | * 资源,服务未找到 |
| | | */ |
| | | int NOT_FOUND = 404; |
| | | |
| | | /** |
| | | * 不允许的http方法 |
| | | */ |
| | | int BAD_METHOD = 405; |
| | | |
| | | /** |
| | | * 资源冲突,或者资源被锁 |
| | | */ |
| | | int CONFLICT = 409; |
| | | |
| | | /** |
| | | * 不支持的数据,媒体类型 |
| | | */ |
| | | int UNSUPPORTED_TYPE = 415; |
| | | |
| | | /** |
| | | * 系统内部错误 |
| | | */ |
| | | int ERROR = 500; |
| | | |
| | | /** |
| | | * 接口未实现 |
| | | */ |
| | | int NOT_IMPLEMENTED = 501; |
| | | |
| | | /** |
| | | * 系统警告消息 |
| | | */ |
| | | int WARN = 601; |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 租户常量信息 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface TenantConstants { |
| | | |
| | | /** |
| | | * 租户正常状态 |
| | | */ |
| | | String NORMAL = "0"; |
| | | |
| | | /** |
| | | * 租户封禁状态 |
| | | */ |
| | | String DISABLE = "1"; |
| | | |
| | | /** |
| | | * 超级管理员ID |
| | | */ |
| | | Long SUPER_ADMIN_ID = 1L; |
| | | |
| | | /** |
| | | * 超级管理员角色 roleKey |
| | | */ |
| | | String SUPER_ADMIN_ROLE_KEY = "superadmin"; |
| | | |
| | | /** |
| | | * 租户管理员角色 roleKey |
| | | */ |
| | | String TENANT_ADMIN_ROLE_KEY = "admin"; |
| | | |
| | | /** |
| | | * 租户管理员角色名称 |
| | | */ |
| | | String TENANT_ADMIN_ROLE_NAME = "管理员"; |
| | | |
| | | /** |
| | | * 默认租户ID |
| | | */ |
| | | String DEFAULT_TENANT_ID = "000000"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.constant; |
| | | |
| | | /** |
| | | * 用户常量信息 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public interface UserConstants { |
| | | |
| | | /** |
| | | * 平台内系统用户的唯一标志 |
| | | */ |
| | | String SYS_USER = "SYS_USER"; |
| | | |
| | | /** |
| | | * 正常状态 |
| | | */ |
| | | String NORMAL = "0"; |
| | | |
| | | /** |
| | | * 异常状态 |
| | | */ |
| | | String EXCEPTION = "1"; |
| | | |
| | | /** |
| | | * 用户正常状态 |
| | | */ |
| | | String USER_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 用户封禁状态 |
| | | */ |
| | | String USER_DISABLE = "1"; |
| | | |
| | | /** |
| | | * 角色正常状态 |
| | | */ |
| | | String ROLE_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 角色封禁状态 |
| | | */ |
| | | String ROLE_DISABLE = "1"; |
| | | |
| | | /** |
| | | * 部门正常状态 |
| | | */ |
| | | String DEPT_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 部门停用状态 |
| | | */ |
| | | String DEPT_DISABLE = "1"; |
| | | |
| | | /** |
| | | * 岗位正常状态 |
| | | */ |
| | | String POST_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 岗位停用状态 |
| | | */ |
| | | String POST_DISABLE = "1"; |
| | | |
| | | /** |
| | | * 字典正常状态 |
| | | */ |
| | | String DICT_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 是否为系统默认(是) |
| | | */ |
| | | String YES = "Y"; |
| | | |
| | | /** |
| | | * 是否菜单外链(是) |
| | | */ |
| | | String YES_FRAME = "0"; |
| | | |
| | | /** |
| | | * 是否菜单外链(否) |
| | | */ |
| | | String NO_FRAME = "1"; |
| | | |
| | | /** |
| | | * 菜单正常状态 |
| | | */ |
| | | String MENU_NORMAL = "0"; |
| | | |
| | | /** |
| | | * 菜单停用状态 |
| | | */ |
| | | String MENU_DISABLE = "1"; |
| | | |
| | | /** |
| | | * 菜单类型(目录) |
| | | */ |
| | | String TYPE_DIR = "M"; |
| | | |
| | | /** |
| | | * 菜单类型(菜单) |
| | | */ |
| | | String TYPE_MENU = "C"; |
| | | |
| | | /** |
| | | * 菜单类型(按钮) |
| | | */ |
| | | String TYPE_BUTTON = "F"; |
| | | |
| | | /** |
| | | * Layout组件标识 |
| | | */ |
| | | String LAYOUT = "Layout"; |
| | | |
| | | /** |
| | | * ParentView组件标识 |
| | | */ |
| | | String PARENT_VIEW = "ParentView"; |
| | | |
| | | /** |
| | | * InnerLink组件标识 |
| | | */ |
| | | String INNER_LINK = "InnerLink"; |
| | | |
| | | /** |
| | | * 用户名长度限制 |
| | | */ |
| | | int USERNAME_MIN_LENGTH = 2; |
| | | int USERNAME_MAX_LENGTH = 20; |
| | | |
| | | /** |
| | | * 密码长度限制 |
| | | */ |
| | | int PASSWORD_MIN_LENGTH = 5; |
| | | int PASSWORD_MAX_LENGTH = 20; |
| | | |
| | | /** |
| | | * 超级管理员ID |
| | | */ |
| | | Long SUPER_ADMIN_ID = 1L; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.domain; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.constant.HttpStatus; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * 响应信息主体 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class R<T> implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 成功 |
| | | */ |
| | | public static final int SUCCESS = 200; |
| | | |
| | | /** |
| | | * 失败 |
| | | */ |
| | | public static final int FAIL = 500; |
| | | |
| | | /** |
| | | * 消息状态码 |
| | | */ |
| | | private int code; |
| | | |
| | | /** |
| | | * 消息内容 |
| | | */ |
| | | private String msg; |
| | | |
| | | /** |
| | | * 数据对象 |
| | | */ |
| | | private T data; |
| | | |
| | | public static <T> R<T> ok() { |
| | | return restResult(null, SUCCESS, "操作成功"); |
| | | } |
| | | |
| | | public static <T> R<T> ok(T data) { |
| | | return restResult(data, SUCCESS, "操作成功"); |
| | | } |
| | | |
| | | public static <T> R<T> ok(String msg) { |
| | | return restResult(null, SUCCESS, msg); |
| | | } |
| | | |
| | | public static <T> R<T> ok(String msg, T data) { |
| | | return restResult(data, SUCCESS, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail() { |
| | | return restResult(null, FAIL, "操作失败"); |
| | | } |
| | | |
| | | public static <T> R<T> fail(String msg) { |
| | | return restResult(null, FAIL, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail(T data) { |
| | | return restResult(data, FAIL, "操作失败"); |
| | | } |
| | | |
| | | public static <T> R<T> fail(String msg, T data) { |
| | | return restResult(data, FAIL, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail(int code, String msg) { |
| | | return restResult(null, code, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回警告消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @return 警告消息 |
| | | */ |
| | | public static <T> R<T> warn(String msg) { |
| | | return restResult(null, HttpStatus.WARN, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回警告消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @param data 数据对象 |
| | | * @return 警告消息 |
| | | */ |
| | | public static <T> R<T> warn(String msg, T data) { |
| | | return restResult(data, HttpStatus.WARN, msg); |
| | | } |
| | | |
| | | private static <T> R<T> restResult(T data, int code, String msg) { |
| | | R<T> r = new R<>(); |
| | | r.setCode(code); |
| | | r.setData(data); |
| | | r.setMsg(msg); |
| | | return r; |
| | | } |
| | | |
| | | public static <T> Boolean isError(R<T> ret) { |
| | | return !isSuccess(ret); |
| | | } |
| | | |
| | | public static <T> Boolean isSuccess(R<T> ret) { |
| | | return R.SUCCESS == ret.getCode(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.domain.model; |
| | | |
| | | import jakarta.validation.constraints.NotBlank; |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | /** |
| | | * 用户登录对象 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class LoginBody { |
| | | |
| | | /** |
| | | * 客户端id |
| | | */ |
| | | @NotBlank(message = "{auth.clientid.not.blank}") |
| | | private String clientId; |
| | | |
| | | /** |
| | | * 授权类型 |
| | | */ |
| | | @NotBlank(message = "{auth.grant.type.not.blank}") |
| | | private String grantType; |
| | | |
| | | /** |
| | | * 租户ID |
| | | */ |
| | | private String tenantId; |
| | | |
| | | /** |
| | | * 验证码 |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * 唯一标识 |
| | | */ |
| | | private String uuid; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.enums; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 设备类型 |
| | | * 针对一套 用户体系 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum DeviceType { |
| | | |
| | | /** |
| | | * pc端 |
| | | */ |
| | | PC("pc"), |
| | | |
| | | /** |
| | | * app端 |
| | | */ |
| | | APP("app"), |
| | | |
| | | /** |
| | | * 小程序端 |
| | | */ |
| | | XCX("xcx"); |
| | | |
| | | private final String device; |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.enums; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 登录类型 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum LoginType { |
| | | |
| | | /** |
| | | * 密码登录 |
| | | */ |
| | | PASSWORD("user.password.retry.limit.exceed", "user.password.retry.limit.count"), |
| | | |
| | | /** |
| | | * 短信登录 |
| | | */ |
| | | SMS("sms.code.retry.limit.exceed", "sms.code.retry.limit.count"), |
| | | |
| | | /** |
| | | * 邮箱登录 |
| | | */ |
| | | EMAIL("email.code.retry.limit.exceed", "email.code.retry.limit.count"), |
| | | |
| | | /** |
| | | * 小程序登录 |
| | | */ |
| | | XCX("", ""); |
| | | |
| | | /** |
| | | * 登录重试超出限制提示 |
| | | */ |
| | | final String retryLimitExceed; |
| | | |
| | | /** |
| | | * 登录重试限制计数提示 |
| | | */ |
| | | final String retryLimitCount; |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.enums; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 用户状态 |
| | | * |
| | | * @author LionLi |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum TenantStatus { |
| | | /** |
| | | * 正常 |
| | | */ |
| | | OK("0", "正常"), |
| | | /** |
| | | * 停用 |
| | | */ |
| | | DISABLE("1", "停用"), |
| | | /** |
| | | * 删除 |
| | | */ |
| | | DELETED("2", "删除"); |
| | | |
| | | private final String code; |
| | | private final String info; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.enums; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 用户状态 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum UserStatus { |
| | | /** |
| | | * 正常 |
| | | */ |
| | | OK("0", "正常"), |
| | | /** |
| | | * 停用 |
| | | */ |
| | | DISABLE("1", "停用"), |
| | | /** |
| | | * 删除 |
| | | */ |
| | | DELETED("2", "删除"); |
| | | |
| | | private final String code; |
| | | private final String info; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.enums; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 设备类型 |
| | | * 针对多套 用户体系 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum UserType { |
| | | |
| | | /** |
| | | * pc端 |
| | | */ |
| | | SYS_USER("sys_user"), |
| | | |
| | | /** |
| | | * app端 |
| | | */ |
| | | APP_USER("app_user"); |
| | | |
| | | private final String userType; |
| | | |
| | | public static UserType getUserType(String str) { |
| | | for (UserType value : values()) { |
| | | if (StringUtils.contains(str, value.getUserType())) { |
| | | return value; |
| | | } |
| | | } |
| | | throw new RuntimeException("'UserType' not found By " + str); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 业务异常 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = true) |
| | | @NoArgsConstructor |
| | | @AllArgsConstructor |
| | | public final class ServiceException extends RuntimeException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 错误码 |
| | | */ |
| | | private Integer code; |
| | | |
| | | /** |
| | | * 错误提示 |
| | | */ |
| | | private String message; |
| | | |
| | | /** |
| | | * 错误明细,内部调试错误 |
| | | */ |
| | | private String detailMessage; |
| | | |
| | | public ServiceException(String message) { |
| | | this.message = message; |
| | | } |
| | | |
| | | public ServiceException(String message, Integer code) { |
| | | this.message = message; |
| | | this.code = code; |
| | | } |
| | | |
| | | public String getDetailMessage() { |
| | | return detailMessage; |
| | | } |
| | | |
| | | @Override |
| | | public String getMessage() { |
| | | return message; |
| | | } |
| | | |
| | | public Integer getCode() { |
| | | return code; |
| | | } |
| | | |
| | | public ServiceException setMessage(String message) { |
| | | this.message = message; |
| | | return this; |
| | | } |
| | | |
| | | public ServiceException setDetailMessage(String detailMessage) { |
| | | this.detailMessage = detailMessage; |
| | | return this; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.base; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.MessageUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 基础异常 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = true) |
| | | @NoArgsConstructor |
| | | @AllArgsConstructor |
| | | public class BaseException extends RuntimeException { |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 所属模块 |
| | | */ |
| | | private String module; |
| | | |
| | | /** |
| | | * 错误码 |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * 错误码对应的参数 |
| | | */ |
| | | private Object[] args; |
| | | |
| | | /** |
| | | * 错误消息 |
| | | */ |
| | | private String defaultMessage; |
| | | |
| | | public BaseException(String module, String code, Object[] args) { |
| | | this(module, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String module, String defaultMessage) { |
| | | this(module, null, null, defaultMessage); |
| | | } |
| | | |
| | | public BaseException(String code, Object[] args) { |
| | | this(null, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String defaultMessage) { |
| | | this(null, null, null, defaultMessage); |
| | | } |
| | | |
| | | @Override |
| | | public String getMessage() { |
| | | String message = null; |
| | | if (!StringUtils.isEmpty(code)) { |
| | | message = MessageUtils.message(code, args); |
| | | } |
| | | if (message == null) { |
| | | message = defaultMessage; |
| | | } |
| | | return message; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.file; |
| | | |
| | | import org.dromara.common.core.exception.base.BaseException; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 文件信息异常类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileException extends BaseException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileException(String code, Object[] args) { |
| | | super("file", code, args, null); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.file; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 文件名称超长限制异常类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileNameLengthLimitExceededException extends FileException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileNameLengthLimitExceededException(int defaultFileNameLength) { |
| | | super("upload.filename.exceed.length", new Object[]{defaultFileNameLength}); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.file; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 文件名大小限制异常类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileSizeLimitExceededException extends FileException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileSizeLimitExceededException(long defaultMaxSize) { |
| | | super("upload.exceed.maxSize", new Object[]{defaultMaxSize}); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.user; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 验证码错误异常类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class CaptchaException extends UserException { |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public CaptchaException() { |
| | | super("user.jcaptcha.error"); |
| | | } |
| | | |
| | | public CaptchaException(String msg) { |
| | | super(msg); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.user; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 验证码失效异常类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CaptchaExpireException extends UserException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public CaptchaExpireException() { |
| | | super("user.jcaptcha.expire"); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.exception.user; |
| | | |
| | | import org.dromara.common.core.exception.base.BaseException; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 用户信息异常类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class UserException extends BaseException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public UserException(String code, Object... args) { |
| | | super("user", code, args, null); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.factory; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; |
| | | import org.springframework.core.env.PropertiesPropertySource; |
| | | import org.springframework.core.env.PropertySource; |
| | | import org.springframework.core.io.support.DefaultPropertySourceFactory; |
| | | import org.springframework.core.io.support.EncodedResource; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * yml 配置源工厂 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class YmlPropertySourceFactory extends DefaultPropertySourceFactory { |
| | | |
| | | @Override |
| | | public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { |
| | | String sourceName = resource.getResource().getFilename(); |
| | | if (StringUtils.isNotBlank(sourceName) && StringUtils.endsWithAny(sourceName, ".yml", ".yaml")) { |
| | | YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
| | | factory.setResources(resource.getResource()); |
| | | factory.afterPropertiesSet(); |
| | | return new PropertiesPropertySource(sourceName, factory.getObject()); |
| | | } |
| | | return super.createPropertySource(name, resource); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.service; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 字典服务服务 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface DictService { |
| | | |
| | | /** |
| | | * 根据字典类型和字典值获取字典标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictValue 字典值 |
| | | * @return 字典标签 |
| | | */ |
| | | default String getDictLabel(String dictType, String dictValue) { |
| | | return getDictLabel(dictType, dictValue, StringUtils.SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 根据字典类型和字典标签获取字典值 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictLabel 字典标签 |
| | | * @return 字典值 |
| | | */ |
| | | default String getDictValue(String dictType, String dictLabel) { |
| | | return getDictValue(dictType, dictLabel, StringUtils.SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 根据字典类型和字典值获取字典标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictValue 字典值 |
| | | * @param separator 分隔符 |
| | | * @return 字典标签 |
| | | */ |
| | | String getDictLabel(String dictType, String dictValue, String separator); |
| | | |
| | | /** |
| | | * 根据字典类型和字典标签获取字典值 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictLabel 字典标签 |
| | | * @param separator 分隔符 |
| | | * @return 字典值 |
| | | */ |
| | | String getDictValue(String dictType, String dictLabel, String separator); |
| | | |
| | | /** |
| | | * 获取字典下所有的字典值与标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @return dictValue为key,dictLabel为值组成的Map |
| | | */ |
| | | Map<String, String> getAllDictByDictType(String dictType); |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | |
| | | import java.lang.management.ManagementFactory; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 时间工具类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class DateUtils extends org.apache.commons.lang3.time.DateUtils { |
| | | |
| | | public static final String YYYY = "yyyy"; |
| | | |
| | | public static final String YYYY_MM = "yyyy-MM"; |
| | | |
| | | public static final String YYYY_MM_DD = "yyyy-MM-dd"; |
| | | |
| | | public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; |
| | | |
| | | public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; |
| | | |
| | | private static final String[] PARSE_PATTERNS = { |
| | | "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", |
| | | "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", |
| | | "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; |
| | | |
| | | /** |
| | | * 获取当前Date型日期 |
| | | * |
| | | * @return Date() 当前日期 |
| | | */ |
| | | public static Date getNowDate() { |
| | | return new Date(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前日期, 默认格式为yyyy-MM-dd |
| | | * |
| | | * @return String |
| | | */ |
| | | public static String getDate() { |
| | | return dateTimeNow(YYYY_MM_DD); |
| | | } |
| | | |
| | | public static String getTime() { |
| | | return dateTimeNow(YYYY_MM_DD_HH_MM_SS); |
| | | } |
| | | |
| | | public static String dateTimeNow() { |
| | | return dateTimeNow(YYYYMMDDHHMMSS); |
| | | } |
| | | |
| | | public static String dateTimeNow(final String format) { |
| | | return parseDateToStr(format, new Date()); |
| | | } |
| | | |
| | | public static String dateTime(final Date date) { |
| | | return parseDateToStr(YYYY_MM_DD, date); |
| | | } |
| | | |
| | | public static String parseDateToStr(final String format, final Date date) { |
| | | return new SimpleDateFormat(format).format(date); |
| | | } |
| | | |
| | | public static Date dateTime(final String format, final String ts) { |
| | | try { |
| | | return new SimpleDateFormat(format).parse(ts); |
| | | } catch (ParseException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 日期路径 即年/月/日 如2018/08/08 |
| | | */ |
| | | public static String datePath() { |
| | | Date now = new Date(); |
| | | return DateFormatUtils.format(now, "yyyy/MM/dd"); |
| | | } |
| | | |
| | | /** |
| | | * 日期路径 即年/月/日 如20180808 |
| | | */ |
| | | public static String dateTime() { |
| | | Date now = new Date(); |
| | | return DateFormatUtils.format(now, "yyyyMMdd"); |
| | | } |
| | | |
| | | /** |
| | | * 日期型字符串转化为日期 格式 |
| | | */ |
| | | public static Date parseDate(Object str) { |
| | | if (str == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return parseDate(str.toString(), PARSE_PATTERNS); |
| | | } catch (ParseException e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取服务器启动时间 |
| | | */ |
| | | public static Date getServerStartDate() { |
| | | long time = ManagementFactory.getRuntimeMXBean().getStartTime(); |
| | | return new Date(time); |
| | | } |
| | | |
| | | /** |
| | | * 计算相差天数 |
| | | */ |
| | | public static int differentDaysByMillisecond(Date date1, Date date2) { |
| | | return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); |
| | | } |
| | | |
| | | /** |
| | | * 计算两个时间差 |
| | | */ |
| | | public static String getDatePoor(Date endDate, Date nowDate) { |
| | | long nd = 1000 * 24 * 60 * 60; |
| | | long nh = 1000 * 60 * 60; |
| | | long nm = 1000 * 60; |
| | | // long ns = 1000; |
| | | // 获得两个时间的毫秒时间差异 |
| | | long diff = endDate.getTime() - nowDate.getTime(); |
| | | // 计算差多少天 |
| | | long day = diff / nd; |
| | | // 计算差多少小时 |
| | | long hour = diff % nd / nh; |
| | | // 计算差多少分钟 |
| | | long min = diff % nd % nh / nm; |
| | | // 计算差多少秒//输出结果 |
| | | // long sec = diff % nd % nh % nm / ns; |
| | | return day + "天" + hour + "小时" + min + "分钟"; |
| | | } |
| | | |
| | | /** |
| | | * 增加 LocalDateTime ==> Date |
| | | */ |
| | | public static Date toDate(LocalDateTime temporalAccessor) { |
| | | ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); |
| | | return Date.from(zdt.toInstant()); |
| | | } |
| | | |
| | | /** |
| | | * 增加 LocalDate ==> Date |
| | | */ |
| | | public static Date toDate(LocalDate temporalAccessor) { |
| | | LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); |
| | | ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); |
| | | return Date.from(zdt.toInstant()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import io.github.linpeilie.Converter; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Mapstruct 工具类 |
| | | * <p>参考文档:<a href="https://mapstruct.plus/introduction/quick-start.html">mapstruct-plus</a></p> |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class MapstructUtils { |
| | | |
| | | private final static Converter CONVERTER = SpringUtils.getBean(Converter.class); |
| | | |
| | | /** |
| | | * 将 T 类型对象,转换为 desc 类型的对象并返回 |
| | | * |
| | | * @param source 数据来源实体 |
| | | * @param desc 描述对象 转换后的对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> V convert(T source, Class<V> desc) { |
| | | if (ObjectUtil.isNull(source)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(desc)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(source, desc); |
| | | } |
| | | |
| | | /** |
| | | * 将 T 类型对象,按照配置的映射字段规则,给 desc 类型的对象赋值并返回 desc 对象 |
| | | * |
| | | * @param source 数据来源实体 |
| | | * @param desc 转换后的对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> V convert(T source, V desc) { |
| | | if (ObjectUtil.isNull(source)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(desc)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(source, desc); |
| | | } |
| | | |
| | | /** |
| | | * 将 T 类型的集合,转换为 desc 类型的集合并返回 |
| | | * |
| | | * @param sourceList 数据来源实体列表 |
| | | * @param desc 描述对象 转换后的对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> List<V> convert(List<T> sourceList, Class<V> desc) { |
| | | if (ObjectUtil.isNull(sourceList)) { |
| | | return null; |
| | | } |
| | | if (CollUtil.isEmpty(sourceList)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return CONVERTER.convert(sourceList, desc); |
| | | } |
| | | |
| | | /** |
| | | * 将 Map 转换为 beanClass 类型的集合并返回 |
| | | * |
| | | * @param map 数据来源 |
| | | * @param beanClass bean类 |
| | | * @return bean对象 |
| | | */ |
| | | public static <T> T convert(Map<String, Object> map, Class<T> beanClass) { |
| | | if (MapUtil.isEmpty(map)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(beanClass)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(map, beanClass); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.context.MessageSource; |
| | | import org.springframework.context.NoSuchMessageException; |
| | | import org.springframework.context.i18n.LocaleContextHolder; |
| | | |
| | | /** |
| | | * 获取i18n资源文件 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class MessageUtils { |
| | | |
| | | private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class); |
| | | |
| | | /** |
| | | * 根据消息键和参数 获取消息 委托给spring messageSource |
| | | * |
| | | * @param code 消息键 |
| | | * @param args 参数 |
| | | * @return 获取国际化翻译值 |
| | | */ |
| | | public static String message(String code, Object... args) { |
| | | try { |
| | | return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale()); |
| | | } catch (NoSuchMessageException e) { |
| | | return code; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class ReUtil { |
| | | public final static Pattern GROUP_VAR = Pattern.compile("\\$(\\d+)"); |
| | | |
| | | /** |
| | | * 正则中需要被转义的关键字 |
| | | */ |
| | | public final static Set<Character> RE_KEYS = new HashSet<>( |
| | | Arrays.asList('$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|')); |
| | | ; |
| | | |
| | | /** |
| | | * 正则替换指定值<br> |
| | | * 通过正则查找到字符串,然后把匹配到的字符串加入到replacementTemplate中,$1表示分组1的字符串 |
| | | * |
| | | * <p> |
| | | * 例如:原字符串是:中文1234,我想把1234换成(1234),则可以: |
| | | * |
| | | * <pre> |
| | | * ReUtil.replaceAll("中文1234", "(\\d+)", "($1)")) |
| | | * |
| | | * 结果:中文(1234) |
| | | * </pre> |
| | | * |
| | | * @param content 文本 |
| | | * @param regex 正则 |
| | | * @param replacementTemplate 替换的文本模板,可以使用$1类似的变量提取正则匹配出的内容 |
| | | * @return 处理后的文本 |
| | | */ |
| | | public static String replaceAll(CharSequence content, String regex, String replacementTemplate) { |
| | | final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); |
| | | return replaceAll(content, pattern, replacementTemplate); |
| | | } |
| | | |
| | | /** |
| | | * 正则替换指定值<br> |
| | | * 通过正则查找到字符串,然后把匹配到的字符串加入到replacementTemplate中,$1表示分组1的字符串 |
| | | * |
| | | * @param content 文本 |
| | | * @param pattern {@link Pattern} |
| | | * @param replacementTemplate 替换的文本模板,可以使用$1类似的变量提取正则匹配出的内容 |
| | | * @return 处理后的文本 |
| | | * @since 3.0.4 |
| | | */ |
| | | public static String replaceAll(CharSequence content, Pattern pattern, String replacementTemplate) { |
| | | if (StringUtils.isEmpty(content)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | |
| | | final Matcher matcher = pattern.matcher(content); |
| | | boolean result = matcher.find(); |
| | | if (result) { |
| | | final Set<String> varNums = findAll(GROUP_VAR, replacementTemplate, 1, new HashSet<>()); |
| | | final StringBuffer sb = new StringBuffer(); |
| | | do { |
| | | String replacement = replacementTemplate; |
| | | for (String var : varNums) { |
| | | int group = Integer.parseInt(var); |
| | | replacement = replacement.replace("$" + var, matcher.group(group)); |
| | | } |
| | | matcher.appendReplacement(sb, escape(replacement)); |
| | | result = matcher.find(); |
| | | } |
| | | while (result); |
| | | matcher.appendTail(sb); |
| | | return sb.toString(); |
| | | } |
| | | return Convert.toStr(content); |
| | | } |
| | | |
| | | /** |
| | | * 取得内容中匹配的所有结果 |
| | | * |
| | | * @param <T> 集合类型 |
| | | * @param pattern 编译后的正则模式 |
| | | * @param content 被查找的内容 |
| | | * @param group 正则的分组 |
| | | * @param collection 返回的集合类型 |
| | | * @return 结果集 |
| | | */ |
| | | public static <T extends Collection<String>> T findAll(Pattern pattern, CharSequence content, int group, |
| | | T collection) { |
| | | if (null == pattern || null == content) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == collection) { |
| | | throw new NullPointerException("Null collection param provided!"); |
| | | } |
| | | |
| | | final Matcher matcher = pattern.matcher(content); |
| | | while (matcher.find()) { |
| | | collection.add(matcher.group(group)); |
| | | } |
| | | return collection; |
| | | } |
| | | |
| | | /** |
| | | * 转义字符,将正则的关键字转义 |
| | | * |
| | | * @param c 字符 |
| | | * @return 转义后的文本 |
| | | */ |
| | | public static String escape(char c) { |
| | | final StringBuilder builder = new StringBuilder(); |
| | | if (RE_KEYS.contains(c)) { |
| | | builder.append('\\'); |
| | | } |
| | | builder.append(c); |
| | | return builder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 转义字符串,将正则的关键字转义 |
| | | * |
| | | * @param content 文本 |
| | | * @return 转义后的文本 |
| | | */ |
| | | public static String escape(CharSequence content) { |
| | | if (StringUtils.isBlank(content)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | |
| | | final StringBuilder builder = new StringBuilder(); |
| | | int len = content.length(); |
| | | char current; |
| | | for (int i = 0; i < len; i++) { |
| | | current = content.charAt(i); |
| | | if (RE_KEYS.contains(current)) { |
| | | builder.append('\\'); |
| | | } |
| | | builder.append(current); |
| | | } |
| | | return builder.toString(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.extra.servlet.JakartaServletUtil; |
| | | import cn.hutool.http.HttpStatus; |
| | | import jakarta.servlet.ServletRequest; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpSession; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.util.LinkedCaseInsensitiveMap; |
| | | import org.springframework.web.context.request.RequestAttributes; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.URLDecoder; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Collections; |
| | | import java.util.Enumeration; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 客户端工具类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ServletUtils extends JakartaServletUtil { |
| | | |
| | | /** |
| | | * 获取String参数 |
| | | */ |
| | | public static String getParameter(String name) { |
| | | return getRequest().getParameter(name); |
| | | } |
| | | |
| | | /** |
| | | * 获取String参数 |
| | | */ |
| | | public static String getParameter(String name, String defaultValue) { |
| | | return Convert.toStr(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * 获取Integer参数 |
| | | */ |
| | | public static Integer getParameterToInt(String name) { |
| | | return Convert.toInt(getRequest().getParameter(name)); |
| | | } |
| | | |
| | | /** |
| | | * 获取Integer参数 |
| | | */ |
| | | public static Integer getParameterToInt(String name, Integer defaultValue) { |
| | | return Convert.toInt(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * 获取Boolean参数 |
| | | */ |
| | | public static Boolean getParameterToBool(String name) { |
| | | return Convert.toBool(getRequest().getParameter(name)); |
| | | } |
| | | |
| | | /** |
| | | * 获取Boolean参数 |
| | | */ |
| | | public static Boolean getParameterToBool(String name, Boolean defaultValue) { |
| | | return Convert.toBool(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * 获得所有请求参数 |
| | | * |
| | | * @param request 请求对象{@link ServletRequest} |
| | | * @return Map |
| | | */ |
| | | public static Map<String, String[]> getParams(ServletRequest request) { |
| | | final Map<String, String[]> map = request.getParameterMap(); |
| | | return Collections.unmodifiableMap(map); |
| | | } |
| | | |
| | | /** |
| | | * 获得所有请求参数 |
| | | * |
| | | * @param request 请求对象{@link ServletRequest} |
| | | * @return Map |
| | | */ |
| | | public static Map<String, String> getParamMap(ServletRequest request) { |
| | | Map<String, String> params = new HashMap<>(); |
| | | for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) { |
| | | params.put(entry.getKey(), StringUtils.join(entry.getValue(), StringUtils.SEPARATOR)); |
| | | } |
| | | return params; |
| | | } |
| | | |
| | | /** |
| | | * 获取request |
| | | */ |
| | | public static HttpServletRequest getRequest() { |
| | | try { |
| | | return getRequestAttributes().getRequest(); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取response |
| | | */ |
| | | public static HttpServletResponse getResponse() { |
| | | try { |
| | | return getRequestAttributes().getResponse(); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取session |
| | | */ |
| | | public static HttpSession getSession() { |
| | | return getRequest().getSession(); |
| | | } |
| | | |
| | | public static ServletRequestAttributes getRequestAttributes() { |
| | | try { |
| | | RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); |
| | | return (ServletRequestAttributes) attributes; |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static String getHeader(HttpServletRequest request, String name) { |
| | | String value = request.getHeader(name); |
| | | if (StringUtils.isEmpty(value)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return urlDecode(value); |
| | | } |
| | | |
| | | public static Map<String, String> getHeaders(HttpServletRequest request) { |
| | | Map<String, String> map = new LinkedCaseInsensitiveMap<>(); |
| | | Enumeration<String> enumeration = request.getHeaderNames(); |
| | | if (enumeration != null) { |
| | | while (enumeration.hasMoreElements()) { |
| | | String key = enumeration.nextElement(); |
| | | String value = request.getHeader(key); |
| | | map.put(key, value); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 将字符串渲染到客户端 |
| | | * |
| | | * @param response 渲染对象 |
| | | * @param string 待渲染的字符串 |
| | | */ |
| | | public static void renderString(HttpServletResponse response, String string) { |
| | | try { |
| | | response.setStatus(HttpStatus.HTTP_OK); |
| | | response.setContentType(MediaType.APPLICATION_JSON_VALUE); |
| | | response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); |
| | | response.getWriter().print(string); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 是否是Ajax异步请求 |
| | | * |
| | | * @param request |
| | | */ |
| | | public static boolean isAjaxRequest(HttpServletRequest request) { |
| | | |
| | | String accept = request.getHeader("accept"); |
| | | if (accept != null && accept.contains(MediaType.APPLICATION_JSON_VALUE)) { |
| | | return true; |
| | | } |
| | | |
| | | String xRequestedWith = request.getHeader("X-Requested-With"); |
| | | if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) { |
| | | return true; |
| | | } |
| | | |
| | | String uri = request.getRequestURI(); |
| | | if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) { |
| | | return true; |
| | | } |
| | | |
| | | String ajax = request.getParameter("__ajax"); |
| | | return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml"); |
| | | } |
| | | |
| | | public static String getClientIP() { |
| | | return getClientIP(getRequest()); |
| | | } |
| | | |
| | | /** |
| | | * 内容编码 |
| | | * |
| | | * @param str 内容 |
| | | * @return 编码后的内容 |
| | | */ |
| | | public static String urlEncode(String str) { |
| | | return URLEncoder.encode(str, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 内容解码 |
| | | * |
| | | * @param str 内容 |
| | | * @return 解码后的内容 |
| | | */ |
| | | public static String urlDecode(String str) { |
| | | return URLDecoder.decode(str, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | | import org.springframework.context.ApplicationContext; |
| | | |
| | | /** |
| | | * spring工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public final class SpringUtils extends SpringUtil { |
| | | |
| | | /** |
| | | * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true |
| | | */ |
| | | public static boolean containsBean(String name) { |
| | | return getBeanFactory().containsBean(name); |
| | | } |
| | | |
| | | /** |
| | | * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 |
| | | * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) |
| | | */ |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| | | return getBeanFactory().isSingleton(name); |
| | | } |
| | | |
| | | /** |
| | | * @return Class 注册对象的类型 |
| | | */ |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| | | return getBeanFactory().getType(name); |
| | | } |
| | | |
| | | /** |
| | | * 如果给定的bean名字在bean定义中有别名,则返回这些别名 |
| | | */ |
| | | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { |
| | | return getBeanFactory().getAliases(name); |
| | | } |
| | | |
| | | /** |
| | | * 获取aop代理对象 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getAopProxy(T invoker) { |
| | | return (T) AopContext.currentProxy(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取spring上下文 |
| | | */ |
| | | public static ApplicationContext context() { |
| | | return getApplicationContext(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.*; |
| | | import java.util.function.BiFunction; |
| | | import java.util.function.Function; |
| | | import java.util.function.Predicate; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * stream 流工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class StreamUtils { |
| | | |
| | | /** |
| | | * 将collection过滤 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 过滤方法 |
| | | * @return 过滤后的list |
| | | */ |
| | | public static <E> List<E> filter(Collection<E> collection, Predicate<E> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | return collection.stream().filter(function).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将collection拼接 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 拼接方法 |
| | | * @return 拼接后的list |
| | | */ |
| | | public static <E> String join(Collection<E> collection, Function<E, String> function) { |
| | | return join(collection, function, StringUtils.SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 将collection拼接 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 拼接方法 |
| | | * @param delimiter 拼接符 |
| | | * @return 拼接后的list |
| | | */ |
| | | public static <E> String join(Collection<E> collection, Function<E, String> function, CharSequence delimiter) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter)); |
| | | } |
| | | |
| | | /** |
| | | * 将collection排序 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param comparing 排序方法 |
| | | * @return 排序后的list |
| | | */ |
| | | public static <E> List<E> sorted(Collection<E> collection, Comparator<E> comparing) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | return collection.stream().filter(Objects::nonNull).sorted(comparing).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为类型不变的map<br> |
| | | * <B>{@code Collection<V> ----> Map<K,V>}</B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param key V类型转化为K类型的lambda方法 |
| | | * @param <V> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @return 转化后的map |
| | | */ |
| | | public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); |
| | | } |
| | | |
| | | /** |
| | | * 将Collection转化为map(value类型与collection的泛型不同)<br> |
| | | * <B>{@code Collection<E> -----> Map<K,V> }</B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param key E类型转化为K类型的lambda方法 |
| | | * @param value E类型转化为V类型的lambda方法 |
| | | * @param <E> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @param <V> map中的value类型 |
| | | * @return 转化后的map |
| | | */ |
| | | public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, value, (l, r) -> l)); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照规则(比如有相同的班级id)分类成map<br> |
| | | * <B>{@code Collection<E> -------> Map<K,List<E>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key 分类的规则 |
| | | * @param <E> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br> |
| | | * <B>{@code Collection<E> ---> Map<T,Map<U,List<E>>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key1 第一个分类的规则 |
| | | * @param key2 第二个分类的规则 |
| | | * @param <E> 集合元素类型 |
| | | * @param <K> 第一个map中的key类型 |
| | | * @param <U> 第二个map中的key类型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1, Function<E, U> key2) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList()))); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br> |
| | | * <B>{@code Collection<E> ---> Map<T,Map<U,E>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key1 第一个分类的规则 |
| | | * @param key2 第二个分类的规则 |
| | | * @param <T> 第一个map中的key类型 |
| | | * @param <U> 第二个map中的key类型 |
| | | * @param <E> collection中的泛型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection, Function<E, T> key1, Function<E, U> key2) { |
| | | if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为List集合,但是两者的泛型不同<br> |
| | | * <B>{@code Collection<E> ------> List<T> } </B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function collection中的泛型转化为list泛型的lambda表达式 |
| | | * @param <E> collection中的泛型 |
| | | * @param <T> List中的泛型 |
| | | * @return 转化后的list |
| | | */ |
| | | public static <E, T> List<T> toList(Collection<E> collection, Function<E, T> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return collection |
| | | .stream() |
| | | .map(function) |
| | | .filter(Objects::nonNull) |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为Set集合,但是两者的泛型不同<br> |
| | | * <B>{@code Collection<E> ------> Set<T> } </B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function collection中的泛型转化为set泛型的lambda表达式 |
| | | * @param <E> collection中的泛型 |
| | | * @param <T> Set中的泛型 |
| | | * @return 转化后的Set |
| | | */ |
| | | public static <E, T> Set<T> toSet(Collection<E> collection, Function<E, T> function) { |
| | | if (CollUtil.isEmpty(collection) || function == null) { |
| | | return CollUtil.newHashSet(); |
| | | } |
| | | return collection |
| | | .stream() |
| | | .map(function) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 合并两个相同key类型的map |
| | | * |
| | | * @param map1 第一个需要合并的 map |
| | | * @param map2 第二个需要合并的 map |
| | | * @param merge 合并的lambda,将key value1 value2合并成最终的类型,注意value可能为空的情况 |
| | | * @param <K> map中的key类型 |
| | | * @param <X> 第一个 map的value类型 |
| | | * @param <Y> 第二个 map的value类型 |
| | | * @param <V> 最终map的value类型 |
| | | * @return 合并后的map |
| | | */ |
| | | public static <K, X, Y, V> Map<K, V> merge(Map<K, X> map1, Map<K, Y> map2, BiFunction<X, Y, V> merge) { |
| | | if (MapUtil.isEmpty(map1) && MapUtil.isEmpty(map2)) { |
| | | return MapUtil.newHashMap(); |
| | | } else if (MapUtil.isEmpty(map1)) { |
| | | map1 = MapUtil.newHashMap(); |
| | | } else if (MapUtil.isEmpty(map2)) { |
| | | map2 = MapUtil.newHashMap(); |
| | | } |
| | | Set<K> key = new HashSet<>(); |
| | | key.addAll(map1.keySet()); |
| | | key.addAll(map2.keySet()); |
| | | Map<K, V> map = new HashMap<>(); |
| | | for (K t : key) { |
| | | X x = map1.get(t); |
| | | Y y = map2.get(t); |
| | | V z = merge.apply(x, y); |
| | | if (z != null) { |
| | | map.put(t, z); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.util.AntPathMatcher; |
| | | |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 字符串工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class StringUtils extends org.apache.commons.lang3.StringUtils { |
| | | |
| | | public static final String SEPARATOR = ","; |
| | | |
| | | /** |
| | | * 获取参数不为空值 |
| | | * |
| | | * @param str defaultValue 要判断的value |
| | | * @return value 返回值 |
| | | */ |
| | | public static String blankToDefault(String str, String defaultValue) { |
| | | return StrUtil.blankToDefault(str, defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * * 判断一个字符串是否为空串 |
| | | * |
| | | * @param str String |
| | | * @return true:为空 false:非空 |
| | | */ |
| | | public static boolean isEmpty(String str) { |
| | | return StrUtil.isEmpty(str); |
| | | } |
| | | |
| | | /** |
| | | * * 判断一个字符串是否为非空串 |
| | | * |
| | | * @param str String |
| | | * @return true:非空串 false:空串 |
| | | */ |
| | | public static boolean isNotEmpty(String str) { |
| | | return !isEmpty(str); |
| | | } |
| | | |
| | | /** |
| | | * 去空格 |
| | | */ |
| | | public static String trim(String str) { |
| | | return StrUtil.trim(str); |
| | | } |
| | | |
| | | /** |
| | | * 截取字符串 |
| | | * |
| | | * @param str 字符串 |
| | | * @param start 开始 |
| | | * @return 结果 |
| | | */ |
| | | public static String substring(final String str, int start) { |
| | | return substring(str, start, str.length()); |
| | | } |
| | | |
| | | /** |
| | | * 截取字符串 |
| | | * |
| | | * @param str 字符串 |
| | | * @param start 开始 |
| | | * @param end 结束 |
| | | * @return 结果 |
| | | */ |
| | | public static String substring(final String str, int start, int end) { |
| | | return StrUtil.sub(str, start, end); |
| | | } |
| | | |
| | | /** |
| | | * 格式化文本, {} 表示占位符<br> |
| | | * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> |
| | | * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> |
| | | * 例:<br> |
| | | * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> |
| | | * 转义{}: format("this is \\{} for {}", "a", "b") -> this is {} for a<br> |
| | | * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> |
| | | * |
| | | * @param template 文本模板,被替换的部分用 {} 表示 |
| | | * @param params 参数值 |
| | | * @return 格式化后的文本 |
| | | */ |
| | | public static String format(String template, Object... params) { |
| | | return StrUtil.format(template, params); |
| | | } |
| | | |
| | | /** |
| | | * 是否为http(s)://开头 |
| | | * |
| | | * @param link 链接 |
| | | * @return 结果 |
| | | */ |
| | | public static boolean ishttp(String link) { |
| | | return Validator.isUrl(link); |
| | | } |
| | | |
| | | /** |
| | | * 字符串转set |
| | | * |
| | | * @param str 字符串 |
| | | * @param sep 分隔符 |
| | | * @return set集合 |
| | | */ |
| | | public static Set<String> str2Set(String str, String sep) { |
| | | return new HashSet<>(str2List(str, sep, true, false)); |
| | | } |
| | | |
| | | /** |
| | | * 字符串转list |
| | | * |
| | | * @param str 字符串 |
| | | * @param sep 分隔符 |
| | | * @param filterBlank 过滤纯空白 |
| | | * @param trim 去掉首尾空白 |
| | | * @return list集合 |
| | | */ |
| | | public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) { |
| | | List<String> list = new ArrayList<>(); |
| | | if (isEmpty(str)) { |
| | | return list; |
| | | } |
| | | |
| | | // 过滤空白字符串 |
| | | if (filterBlank && isBlank(str)) { |
| | | return list; |
| | | } |
| | | String[] split = str.split(sep); |
| | | for (String string : split) { |
| | | if (filterBlank && isBlank(string)) { |
| | | continue; |
| | | } |
| | | if (trim) { |
| | | string = trim(string); |
| | | } |
| | | list.add(string); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写 |
| | | * |
| | | * @param cs 指定字符串 |
| | | * @param searchCharSequences 需要检查的字符串数组 |
| | | * @return 是否包含任意一个字符串 |
| | | */ |
| | | public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) { |
| | | return StrUtil.containsAnyIgnoreCase(cs, searchCharSequences); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰转下划线命名 |
| | | */ |
| | | public static String toUnderScoreCase(String str) { |
| | | return StrUtil.toUnderlineCase(str); |
| | | } |
| | | |
| | | /** |
| | | * 是否包含字符串 |
| | | * |
| | | * @param str 验证字符串 |
| | | * @param strs 字符串组 |
| | | * @return 包含返回true |
| | | */ |
| | | public static boolean inStringIgnoreCase(String str, String... strs) { |
| | | return StrUtil.equalsAnyIgnoreCase(str, strs); |
| | | } |
| | | |
| | | /** |
| | | * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld |
| | | * |
| | | * @param name 转换前的下划线大写方式命名的字符串 |
| | | * @return 转换后的驼峰式命名的字符串 |
| | | */ |
| | | public static String convertToCamelCase(String name) { |
| | | return StrUtil.upperFirst(StrUtil.toCamelCase(name)); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰式命名法 例如:user_name->userName |
| | | */ |
| | | public static String toCamelCase(String s) { |
| | | return StrUtil.toCamelCase(s); |
| | | } |
| | | |
| | | /** |
| | | * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串 |
| | | * |
| | | * @param str 指定字符串 |
| | | * @param strs 需要检查的字符串数组 |
| | | * @return 是否匹配 |
| | | */ |
| | | public static boolean matches(String str, List<String> strs) { |
| | | if (isEmpty(str) || CollUtil.isEmpty(strs)) { |
| | | return false; |
| | | } |
| | | for (String pattern : strs) { |
| | | if (isMatch(pattern, str)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 判断url是否与规则配置: |
| | | * ? 表示单个字符; |
| | | * * 表示一层路径内的任意字符串,不可跨层级; |
| | | * ** 表示任意层路径; |
| | | * |
| | | * @param pattern 匹配规则 |
| | | * @param url 需要匹配的url |
| | | */ |
| | | public static boolean isMatch(String pattern, String url) { |
| | | AntPathMatcher matcher = new AntPathMatcher(); |
| | | return matcher.match(pattern, url); |
| | | } |
| | | |
| | | /** |
| | | * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。 |
| | | * |
| | | * @param num 数字对象 |
| | | * @param size 字符串指定长度 |
| | | * @return 返回数字的字符串格式,该字符串为指定长度。 |
| | | */ |
| | | public static String padl(final Number num, final int size) { |
| | | return padl(num.toString(), size, '0'); |
| | | } |
| | | |
| | | /** |
| | | * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。 |
| | | * |
| | | * @param s 原始字符串 |
| | | * @param size 字符串指定长度 |
| | | * @param c 用于补齐的字符 |
| | | * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。 |
| | | */ |
| | | public static String padl(final String s, final int size, final char c) { |
| | | final StringBuilder sb = new StringBuilder(size); |
| | | if (s != null) { |
| | | final int len = s.length(); |
| | | if (s.length() <= size) { |
| | | sb.append(String.valueOf(c).repeat(size - len)); |
| | | sb.append(s); |
| | | } else { |
| | | return s.substring(len - size, len); |
| | | } |
| | | } else { |
| | | sb.append(String.valueOf(c).repeat(Math.max(0, size))); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串(分隔符默认逗号) |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static List<String> splitList(String str) { |
| | | return splitTo(str, Convert::toStr); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串 |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param separator 分隔符 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static List<String> splitList(String str, String separator) { |
| | | return splitTo(str, separator, Convert::toStr); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串自定义转换(分隔符默认逗号) |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param mapper 自定义转换 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static <T> List<T> splitTo(String str, Function<? super Object, T> mapper) { |
| | | return splitTo(str, SEPARATOR, mapper); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串自定义转换 |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param separator 分隔符 |
| | | * @param mapper 自定义转换 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static <T> List<T> splitTo(String str, String separator, Function<? super Object, T> mapper) { |
| | | if (isBlank(str)) { |
| | | return new ArrayList<>(0); |
| | | } |
| | | return StrUtil.split(str, separator) |
| | | .stream() |
| | | .filter(Objects::nonNull) |
| | | .map(mapper) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.concurrent.*; |
| | | |
| | | /** |
| | | * 线程相关工具类. |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class Threads { |
| | | |
| | | /** |
| | | * sleep等待,单位为毫秒 |
| | | */ |
| | | public static void sleep(long milliseconds) { |
| | | try { |
| | | Thread.sleep(milliseconds); |
| | | } catch (InterruptedException e) { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止线程池 |
| | | * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务. |
| | | * 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数. |
| | | * 如果仍然超時,則強制退出. |
| | | * 另对在shutdown时线程本身被调用中断做了处理. |
| | | */ |
| | | public static void shutdownAndAwaitTermination(ExecutorService pool) { |
| | | if (pool != null && !pool.isShutdown()) { |
| | | pool.shutdown(); |
| | | try { |
| | | if (!pool.awaitTermination(120, TimeUnit.SECONDS)) { |
| | | pool.shutdownNow(); |
| | | if (!pool.awaitTermination(120, TimeUnit.SECONDS)) { |
| | | log.info("Pool did not terminate"); |
| | | } |
| | | } |
| | | } catch (InterruptedException ie) { |
| | | pool.shutdownNow(); |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 打印线程异常信息 |
| | | */ |
| | | public static void printException(Runnable r, Throwable t) { |
| | | if (t == null && r instanceof Future<?>) { |
| | | try { |
| | | Future<?> future = (Future<?>) r; |
| | | if (future.isDone()) { |
| | | future.get(); |
| | | } |
| | | } catch (CancellationException ce) { |
| | | t = ce; |
| | | } catch (ExecutionException ee) { |
| | | t = ee.getCause(); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | } |
| | | if (t != null) { |
| | | log.error(t.getMessage(), t); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.tree.Tree; |
| | | import cn.hutool.core.lang.tree.TreeNodeConfig; |
| | | import cn.hutool.core.lang.tree.TreeUtil; |
| | | import cn.hutool.core.lang.tree.parser.NodeParser; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 扩展 hutool TreeUtil 封装系统树构建 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class TreeBuildUtils extends TreeUtil { |
| | | |
| | | /** |
| | | * 根据前端定制差异化字段 |
| | | */ |
| | | public static final TreeNodeConfig DEFAULT_CONFIG = TreeNodeConfig.DEFAULT_CONFIG.setNameKey("label"); |
| | | |
| | | public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) { |
| | | if (CollUtil.isEmpty(list)) { |
| | | return null; |
| | | } |
| | | K k = ReflectUtils.invokeGetter(list.get(0), "parentId"); |
| | | return TreeUtil.build(list, k, DEFAULT_CONFIG, nodeParser); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | import jakarta.validation.Validator; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Validator 校验框架工具 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ValidatorUtils { |
| | | |
| | | private static final Validator VALID = SpringUtils.getBean(Validator.class); |
| | | |
| | | public static <T> void validate(T object, Class<?>... groups) { |
| | | Set<ConstraintViolation<T>> validate = VALID.validate(object, groups); |
| | | if (!validate.isEmpty()) { |
| | | throw new ConstraintViolationException("参数校验异常", validate); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.file; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * 文件处理工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class FileUtils extends FileUtil { |
| | | |
| | | /** |
| | | * 下载文件名重新编码 |
| | | * |
| | | * @param response 响应对象 |
| | | * @param realFileName 真实文件名 |
| | | */ |
| | | public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) { |
| | | String percentEncodedFileName = percentEncode(realFileName); |
| | | String contentDispositionValue = "attachment; filename=%s;filename*=utf-8''%s".formatted(percentEncodedFileName, percentEncodedFileName); |
| | | response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename"); |
| | | response.setHeader("Content-disposition", contentDispositionValue); |
| | | response.setHeader("download-filename", percentEncodedFileName); |
| | | } |
| | | |
| | | /** |
| | | * 百分号编码工具方法 |
| | | * |
| | | * @param s 需要百分号编码的字符串 |
| | | * @return 百分号编码后的字符串 |
| | | */ |
| | | public static String percentEncode(String s) { |
| | | String encode = URLEncoder.encode(s, StandardCharsets.UTF_8); |
| | | return encode.replaceAll("\\+", "%20"); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.file; |
| | | |
| | | /** |
| | | * 媒体类型工具类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class MimeTypeUtils { |
| | | public static final String IMAGE_PNG = "image/png"; |
| | | |
| | | public static final String IMAGE_JPG = "image/jpg"; |
| | | |
| | | public static final String IMAGE_JPEG = "image/jpeg"; |
| | | |
| | | public static final String IMAGE_BMP = "image/bmp"; |
| | | |
| | | public static final String IMAGE_GIF = "image/gif"; |
| | | |
| | | public static final String[] IMAGE_EXTENSION = {"bmp", "gif", "jpg", "jpeg", "png"}; |
| | | |
| | | public static final String[] FLASH_EXTENSION = {"swf", "flv"}; |
| | | |
| | | public static final String[] MEDIA_EXTENSION = {"swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", |
| | | "asf", "rm", "rmvb"}; |
| | | |
| | | public static final String[] VIDEO_EXTENSION = {"mp4", "avi", "rmvb"}; |
| | | |
| | | public static final String[] DEFAULT_ALLOWED_EXTENSION = { |
| | | // 图片 |
| | | "bmp", "gif", "jpg", "jpeg", "png", |
| | | // word excel powerpoint |
| | | "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt", |
| | | // 压缩文件 |
| | | "rar", "zip", "gz", "bz2", |
| | | // 视频格式 |
| | | "mp4", "avi", "rmvb", |
| | | // pdf |
| | | "pdf"}; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.ip; |
| | | |
| | | import cn.hutool.core.net.NetUtil; |
| | | import cn.hutool.http.HtmlUtil; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * 获取地址类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class AddressUtils { |
| | | |
| | | // 未知地址 |
| | | public static final String UNKNOWN = "XX XX"; |
| | | |
| | | public static String getRealAddressByIP(String ip) { |
| | | if (StringUtils.isBlank(ip)) { |
| | | return UNKNOWN; |
| | | } |
| | | // 内网不查询 |
| | | ip = StringUtils.contains(ip, "0:0:0:0:0:0:0:1") ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip); |
| | | if (NetUtil.isInnerIP(ip)) { |
| | | return "内网IP"; |
| | | } |
| | | return RegionUtils.getCityInfo(ip); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.ip; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.io.resource.ClassPathResource; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.file.FileUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.lionsoul.ip2region.xdb.Searcher; |
| | | |
| | | import java.io.File; |
| | | |
| | | /** |
| | | * 根据ip地址定位工具类,离线方式 |
| | | * 参考地址:<a href="https://gitee.com/lionsoul/ip2region/tree/master/binding/java">集成 ip2region 实现离线IP地址定位库</a> |
| | | * |
| | | * @author lishuyan |
| | | */ |
| | | @Slf4j |
| | | public class RegionUtils { |
| | | |
| | | private static final Searcher SEARCHER; |
| | | |
| | | static { |
| | | String fileName = "/ip2region.xdb"; |
| | | File existFile = FileUtils.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName); |
| | | if (!FileUtils.exist(existFile)) { |
| | | ClassPathResource fileStream = new ClassPathResource(fileName); |
| | | if (ObjectUtil.isEmpty(fileStream.getStream())) { |
| | | throw new ServiceException("RegionUtils初始化失败,原因:IP地址库数据不存在!"); |
| | | } |
| | | FileUtils.writeFromStream(fileStream.getStream(), existFile); |
| | | } |
| | | |
| | | String dbPath = existFile.getPath(); |
| | | |
| | | // 1、从 dbPath 加载整个 xdb 到内存。 |
| | | byte[] cBuff; |
| | | try { |
| | | cBuff = Searcher.loadContentFromFile(dbPath); |
| | | } catch (Exception e) { |
| | | throw new ServiceException("RegionUtils初始化失败,原因:从ip2region.xdb文件加载内容失败!" + e.getMessage()); |
| | | } |
| | | // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。 |
| | | try { |
| | | SEARCHER = Searcher.newWithBuffer(cBuff); |
| | | } catch (Exception e) { |
| | | throw new ServiceException("RegionUtils初始化失败,原因:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据IP地址离线获取城市 |
| | | */ |
| | | public static String getCityInfo(String ip) { |
| | | try { |
| | | ip = ip.trim(); |
| | | // 3、执行查询 |
| | | String region = SEARCHER.search(ip); |
| | | return region.replace("0|", "").replace("|0", ""); |
| | | } catch (Exception e) { |
| | | log.error("IP地址离线获取城市异常 {}", ip); |
| | | return "未知"; |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.reflect; |
| | | |
| | | import cn.hutool.core.util.ReflectUtil; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * 反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数. |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ReflectUtils extends ReflectUtil { |
| | | |
| | | private static final String SETTER_PREFIX = "set"; |
| | | |
| | | private static final String GETTER_PREFIX = "get"; |
| | | |
| | | /** |
| | | * 调用Getter方法. |
| | | * 支持多级,如:对象名.对象名.方法 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E invokeGetter(Object obj, String propertyName) { |
| | | Object object = obj; |
| | | for (String name : StringUtils.split(propertyName, ".")) { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); |
| | | object = invoke(object, getterMethodName); |
| | | } |
| | | return (E) object; |
| | | } |
| | | |
| | | /** |
| | | * 调用Setter方法, 仅匹配方法名。 |
| | | * 支持多级,如:对象名.对象名.方法 |
| | | */ |
| | | public static <E> void invokeSetter(Object obj, String propertyName, E value) { |
| | | Object object = obj; |
| | | String[] names = StringUtils.split(propertyName, "."); |
| | | for (int i = 0; i < names.length; i++) { |
| | | if (i < names.length - 1) { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | object = invoke(object, getterMethodName); |
| | | } else { |
| | | String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | Method method = getMethodByName(object.getClass(), setterMethodName); |
| | | invoke(object, method, value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.utils.sql; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | /** |
| | | * sql操作工具类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class SqlUtil { |
| | | |
| | | /** |
| | | * 定义常用的 sql关键字 |
| | | */ |
| | | public static final String SQL_REGEX = "select |insert |delete |update |drop |count |exec |chr |mid |master |truncate |char |and |declare "; |
| | | |
| | | /** |
| | | * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序) |
| | | */ |
| | | public static final String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; |
| | | |
| | | /** |
| | | * 检查字符,防止注入绕过 |
| | | */ |
| | | public static String escapeOrderBySql(String value) { |
| | | if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value)) { |
| | | throw new IllegalArgumentException("参数不符合规范,不能进行查询"); |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | /** |
| | | * 验证 order by 语法是否符合规范 |
| | | */ |
| | | public static boolean isValidOrderBySql(String value) { |
| | | return value.matches(SQL_PATTERN); |
| | | } |
| | | |
| | | /** |
| | | * SQL关键字检查 |
| | | */ |
| | | public static void filterKeyword(String value) { |
| | | if (StringUtils.isEmpty(value)) { |
| | | return; |
| | | } |
| | | String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|"); |
| | | for (String sqlKeyword : sqlKeywords) { |
| | | if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1) { |
| | | throw new IllegalArgumentException("参数存在SQL注入风险"); |
| | | } |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.validate; |
| | | |
| | | /** |
| | | * 校验分组 add |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface AddGroup { |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.validate; |
| | | |
| | | /** |
| | | * 校验分组 edit |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface EditGroup { |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.validate; |
| | | |
| | | /** |
| | | * 校验分组 query |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface QueryGroup { |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.xss; |
| | | |
| | | import jakarta.validation.Constraint; |
| | | import jakarta.validation.Payload; |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | /** |
| | | * 自定义xss校验注解 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER}) |
| | | @Constraint(validatedBy = {XssValidator.class}) |
| | | public @interface Xss { |
| | | |
| | | String message() default "不允许任何脚本运行"; |
| | | |
| | | Class<?>[] groups() default {}; |
| | | |
| | | Class<? extends Payload>[] payload() default {}; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.core.xss; |
| | | |
| | | import cn.hutool.core.util.ReUtil; |
| | | import cn.hutool.http.HtmlUtil; |
| | | |
| | | import jakarta.validation.ConstraintValidator; |
| | | import jakarta.validation.ConstraintValidatorContext; |
| | | |
| | | /** |
| | | * 自定义xss校验注解实现 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class XssValidator implements ConstraintValidator<Xss, String> { |
| | | |
| | | @Override |
| | | public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { |
| | | return !ReUtil.contains(HtmlUtil.RE_HTML_MARK, value); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.core.utils.SpringUtils |
| | | org.dromara.common.core.config.ApplicationConfig |
| | | org.dromara.common.core.config.ValidatorConfig |
| | | org.dromara.common.core.config.AsyncConfig |
New file |
| | |
| | | #错误消息 |
| | | not.null=* 必须填写 |
| | | user.jcaptcha.error=验证码错误 |
| | | user.jcaptcha.expire=验证码已失效 |
| | | user.not.exists=对不起, 您的账号:{0} 不存在. |
| | | user.password.not.match=用户不存在/密码错误 |
| | | user.password.retry.limit.count=密码输入错误{0}次 |
| | | user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定{1}分钟 |
| | | user.password.delete=对不起,您的账号:{0} 已被删除 |
| | | user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员 |
| | | role.blocked=角色已封禁,请联系管理员 |
| | | user.logout.success=退出成功 |
| | | length.not.valid=长度必须在{min}到{max}个字符之间 |
| | | user.username.not.blank=用户名不能为空 |
| | | user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头 |
| | | user.username.length.valid=账户长度必须在{min}到{max}个字符之间 |
| | | user.password.not.blank=用户密码不能为空 |
| | | user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间 |
| | | user.password.not.valid=* 5-50个字符 |
| | | user.email.not.valid=邮箱格式错误 |
| | | user.email.not.blank=邮箱不能为空 |
| | | user.phonenumber.not.blank=用户手机号不能为空 |
| | | user.mobile.phone.number.not.valid=手机号格式错误 |
| | | user.login.success=登录成功 |
| | | user.register.success=注册成功 |
| | | user.register.save.error=保存用户 {0} 失败,注册账号已存在 |
| | | user.register.error=注册失败,请联系系统管理人员 |
| | | user.notfound=请重新登录 |
| | | user.forcelogout=管理员强制退出,请重新登录 |
| | | user.unknown.error=未知错误,请重新登录 |
| | | auth.grant.type.error=认证权限类型错误 |
| | | auth.grant.type.blocked=认证权限类型已禁用 |
| | | auth.grant.type.not.blank=认证权限类型不能为空 |
| | | auth.clientid.not.blank=认证客户端id不能为空 |
| | | ##文件上传消息 |
| | | upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB! |
| | | upload.filename.exceed.length=上传的文件名最长{0}个字符 |
| | | ##权限 |
| | | no.permission=您没有数据的权限,请联系管理员添加权限 [{0}] |
| | | no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}] |
| | | no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}] |
| | | no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}] |
| | | no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}] |
| | | no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}] |
| | | repeat.submit.message=不允许重复提交,请稍候再试 |
| | | rate.limiter.message=访问过于频繁,请稍候再试 |
| | | sms.code.not.blank=短信验证码不能为空 |
| | | sms.code.retry.limit.count=短信验证码输入错误{0}次 |
| | | sms.code.retry.limit.exceed=短信验证码输入错误{0}次,帐户锁定{1}分钟 |
| | | email.code.not.blank=邮箱验证码不能为空 |
| | | email.code.retry.limit.count=邮箱验证码输入错误{0}次 |
| | | email.code.retry.limit.exceed=邮箱验证码输入错误{0}次,帐户锁定{1}分钟 |
| | | xcx.code.not.blank=小程序[code]不能为空 |
| | | social.source.not.blank=第三方登录平台[source]不能为空 |
| | | social.code.not.blank=第三方登录平台[code]不能为空 |
| | | social.state.not.blank=第三方登录平台[state]不能为空 |
| | | ##租户 |
| | | tenant.number.not.blank=租户编号不能为空 |
| | | tenant.not.exists=对不起, 您的租户不存在,请联系管理员 |
| | | tenant.blocked=对不起,您的租户已禁用,请联系管理员 |
| | | tenant.expired=对不起,您的租户已过期,请联系管理员 |
New file |
| | |
| | | #错误消息 |
| | | not.null=* Required fill in |
| | | user.jcaptcha.error=Captcha error |
| | | user.jcaptcha.expire=Captcha invalid |
| | | user.not.exists=Sorry, your account: {0} does not exist |
| | | user.password.not.match=User does not exist/Password error |
| | | user.password.retry.limit.count=Password input error {0} times |
| | | user.password.retry.limit.exceed=Password input error {0} times, account locked for {1} minutes |
| | | user.password.delete=Sorry, your account:{0} has been deleted |
| | | user.blocked=Sorry, your account: {0} has been disabled. Please contact the administrator |
| | | role.blocked=Role disabled,please contact administrators |
| | | user.logout.success=Exit successful |
| | | length.not.valid=The length must be between {min} and {max} characters |
| | | user.username.not.blank=Username cannot be blank |
| | | user.username.not.valid=* 2 to 20 chinese characters, letters, numbers or underscores, and must start with a non number |
| | | user.username.length.valid=Account length must be between {min} and {max} characters |
| | | user.password.not.blank=Password cannot be empty |
| | | user.password.length.valid=Password length must be between {min} and {max} characters |
| | | user.password.not.valid=* 5-50 characters |
| | | user.email.not.valid=Mailbox format error |
| | | user.email.not.blank=Mailbox cannot be blank |
| | | user.phonenumber.not.blank=Phone number cannot be blank |
| | | user.mobile.phone.number.not.valid=Phone number format error |
| | | user.login.success=Login successful |
| | | user.register.success=Register successful |
| | | user.register.save.error=Failed to save user {0}, The registered account already exists |
| | | user.register.error=Register failed, please contact system administrator |
| | | user.notfound=Please login again |
| | | user.forcelogout=The administrator is forced to exit,please login again |
| | | user.unknown.error=Unknown error, please login again |
| | | auth.grant.type.error=Auth grant type error |
| | | auth.grant.type.blocked=Auth grant type disabled |
| | | auth.grant.type.not.blank=Auth grant type cannot be blank |
| | | auth.clientid.not.blank=Auth clientid cannot be blank |
| | | ##文件上传消息 |
| | | upload.exceed.maxSize=The uploaded file size exceeds the limit file size!<br/>the maximum allowed file size is:{0}MB! |
| | | upload.filename.exceed.length=The maximum length of uploaded file name is {0} characters |
| | | ##权限 |
| | | no.permission=You do not have permission to the data,please contact your administrator to add permissions [{0}] |
| | | no.create.permission=You do not have permission to create data,please contact your administrator to add permissions [{0}] |
| | | no.update.permission=You do not have permission to modify data,please contact your administrator to add permissions [{0}] |
| | | no.delete.permission=You do not have permission to delete data,please contact your administrator to add permissions [{0}] |
| | | no.export.permission=You do not have permission to export data,please contact your administrator to add permissions [{0}] |
| | | no.view.permission=You do not have permission to view data,please contact your administrator to add permissions [{0}] |
| | | repeat.submit.message=Repeat submit is not allowed, please try again later |
| | | rate.limiter.message=Visit too frequently, please try again later |
| | | sms.code.not.blank=Sms code cannot be blank |
| | | sms.code.retry.limit.count=Sms code input error {0} times |
| | | sms.code.retry.limit.exceed=Sms code input error {0} times, account locked for {1} minutes |
| | | email.code.not.blank=Email code cannot be blank |
| | | email.code.retry.limit.count=Email code input error {0} times |
| | | email.code.retry.limit.exceed=Email code input error {0} times, account locked for {1} minutes |
| | | xcx.code.not.blank=Mini program [code] cannot be blank |
| | | social.source.not.blank=Social login platform [source] cannot be blank |
| | | social.code.not.blank=Social login platform [code] cannot be blank |
| | | social.state.not.blank=Social login platform [state] cannot be blank |
| | | ##租户 |
| | | tenant.number.not.blank=Tenant number cannot be blank |
| | | tenant.not.exists=Sorry, your tenant does not exist. Please contact the administrator |
| | | tenant.blocked=Sorry, your tenant is disabled. Please contact the administrator |
| | | tenant.expired=Sorry, your tenant has expired. Please contact the administrator. |
New file |
| | |
| | | #错误消息 |
| | | not.null=* 必须填写 |
| | | user.jcaptcha.error=验证码错误 |
| | | user.jcaptcha.expire=验证码已失效 |
| | | user.not.exists=对不起, 您的账号:{0} 不存在. |
| | | user.password.not.match=用户不存在/密码错误 |
| | | user.password.retry.limit.count=密码输入错误{0}次 |
| | | user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定{1}分钟 |
| | | user.password.delete=对不起,您的账号:{0} 已被删除 |
| | | user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员 |
| | | role.blocked=角色已封禁,请联系管理员 |
| | | user.logout.success=退出成功 |
| | | length.not.valid=长度必须在{min}到{max}个字符之间 |
| | | user.username.not.blank=用户名不能为空 |
| | | user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头 |
| | | user.username.length.valid=账户长度必须在{min}到{max}个字符之间 |
| | | user.password.not.blank=用户密码不能为空 |
| | | user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间 |
| | | user.password.not.valid=* 5-50个字符 |
| | | user.email.not.valid=邮箱格式错误 |
| | | user.email.not.blank=邮箱不能为空 |
| | | user.phonenumber.not.blank=用户手机号不能为空 |
| | | user.mobile.phone.number.not.valid=手机号格式错误 |
| | | user.login.success=登录成功 |
| | | user.register.success=注册成功 |
| | | user.register.save.error=保存用户 {0} 失败,注册账号已存在 |
| | | user.register.error=注册失败,请联系系统管理人员 |
| | | user.notfound=请重新登录 |
| | | user.forcelogout=管理员强制退出,请重新登录 |
| | | user.unknown.error=未知错误,请重新登录 |
| | | auth.grant.type.error=认证权限类型错误 |
| | | auth.grant.type.blocked=认证权限类型已禁用 |
| | | auth.grant.type.not.blank=认证权限类型不能为空 |
| | | auth.clientid.not.blank=认证客户端id不能为空 |
| | | ##文件上传消息 |
| | | upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB! |
| | | upload.filename.exceed.length=上传的文件名最长{0}个字符 |
| | | ##权限 |
| | | no.permission=您没有数据的权限,请联系管理员添加权限 [{0}] |
| | | no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}] |
| | | no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}] |
| | | no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}] |
| | | no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}] |
| | | no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}] |
| | | repeat.submit.message=不允许重复提交,请稍候再试 |
| | | rate.limiter.message=访问过于频繁,请稍候再试 |
| | | sms.code.not.blank=短信验证码不能为空 |
| | | sms.code.retry.limit.count=短信验证码输入错误{0}次 |
| | | sms.code.retry.limit.exceed=短信验证码输入错误{0}次,帐户锁定{1}分钟 |
| | | email.code.not.blank=邮箱验证码不能为空 |
| | | email.code.retry.limit.count=邮箱验证码输入错误{0}次 |
| | | email.code.retry.limit.exceed=邮箱验证码输入错误{0}次,帐户锁定{1}分钟 |
| | | xcx.code.not.blank=小程序[code]不能为空 |
| | | social.source.not.blank=第三方登录平台[source]不能为空 |
| | | social.code.not.blank=第三方登录平台[code]不能为空 |
| | | social.state.not.blank=第三方登录平台[state]不能为空 |
| | | ##租户 |
| | | tenant.number.not.blank=租户编号不能为空 |
| | | tenant.not.exists=对不起, 您的租户不存在,请联系管理员 |
| | | tenant.blocked=对不起,您的租户已禁用,请联系管理员 |
| | | tenant.expired=对不起,您的租户已过期,请联系管理员 |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-dict</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-dict 字典 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <!-- RuoYi Common Security --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-api-system</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-starter</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-core</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.dict.service.impl; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import org.dromara.common.core.constant.CacheConstants; |
| | | import org.dromara.common.core.service.DictService; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.system.api.RemoteDictService; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | import org.dromara.system.api.domain.vo.RemoteDictDataVo; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 字典服务服务 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Service |
| | | public class DictServiceImpl implements DictService { |
| | | |
| | | @DubboReference |
| | | private RemoteDictService remoteDictService; |
| | | |
| | | /** |
| | | * 根据字典类型和字典值获取字典标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictValue 字典值 |
| | | * @param separator 分隔符 |
| | | * @return 字典标签 |
| | | */ |
| | | @SuppressWarnings("unchecked cast") |
| | | @Override |
| | | public String getDictLabel(String dictType, String dictValue, String separator) { |
| | | // 优先从本地缓存获取 |
| | | List<RemoteDictDataVo> datas = (List<RemoteDictDataVo>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); |
| | | if (ObjectUtil.isNull(datas)) { |
| | | datas = remoteDictService.selectDictDataByType(dictType); |
| | | SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); |
| | | } |
| | | |
| | | Map<String, String> map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel); |
| | | if (StringUtils.containsAny(dictValue, separator)) { |
| | | return Arrays.stream(dictValue.split(separator)) |
| | | .map(v -> map.getOrDefault(v, StringUtils.EMPTY)) |
| | | .collect(Collectors.joining(separator)); |
| | | } else { |
| | | return map.getOrDefault(dictValue, StringUtils.EMPTY); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据字典类型和字典标签获取字典值 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictLabel 字典标签 |
| | | * @param separator 分隔符 |
| | | * @return 字典值 |
| | | */ |
| | | @SuppressWarnings("unchecked cast") |
| | | @Override |
| | | public String getDictValue(String dictType, String dictLabel, String separator) { |
| | | // 优先从本地缓存获取 |
| | | List<RemoteDictDataVo> datas = (List<RemoteDictDataVo>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); |
| | | if (ObjectUtil.isNull(datas)) { |
| | | datas = remoteDictService.selectDictDataByType(dictType); |
| | | SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); |
| | | } |
| | | |
| | | Map<String, String> map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictLabel, RemoteDictDataVo::getDictValue); |
| | | if (StringUtils.containsAny(dictLabel, separator)) { |
| | | return Arrays.stream(dictLabel.split(separator)) |
| | | .map(l -> map.getOrDefault(l, StringUtils.EMPTY)) |
| | | .collect(Collectors.joining(separator)); |
| | | } else { |
| | | return map.getOrDefault(dictLabel, StringUtils.EMPTY); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, String> getAllDictByDictType(String dictType) { |
| | | List<RemoteDictDataVo> list = remoteDictService.selectDictDataByType(dictType); |
| | | return StreamUtils.toMap(list, RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.dict.utils; |
| | | |
| | | import org.dromara.common.core.constant.CacheNames; |
| | | import org.dromara.common.redis.utils.CacheUtils; |
| | | import org.dromara.system.api.domain.vo.RemoteDictDataVo; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 字典工具类 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class DictUtils { |
| | | /** |
| | | * 设置字典缓存 |
| | | * |
| | | * @param key 参数键 |
| | | * @param dictDatas 字典数据列表 |
| | | */ |
| | | public static void setDictCache(String key, List<RemoteDictDataVo> dictDatas) { |
| | | CacheUtils.put(CacheNames.SYS_DICT, key, dictDatas); |
| | | } |
| | | |
| | | /** |
| | | * 获取字典缓存 |
| | | * |
| | | * @param key 参数键 |
| | | * @return dictDatas 字典数据列表 |
| | | */ |
| | | public static List<RemoteDictDataVo> getDictCache(String key) { |
| | | return CacheUtils.get(CacheNames.SYS_DICT, key); |
| | | } |
| | | |
| | | /** |
| | | * 删除指定字典缓存 |
| | | * |
| | | * @param key 字典键 |
| | | */ |
| | | public static void removeDictCache(String key) { |
| | | CacheUtils.evict(CacheNames.SYS_DICT, key); |
| | | } |
| | | |
| | | /** |
| | | * 清空字典缓存 |
| | | */ |
| | | public static void clearDictCache() { |
| | | CacheUtils.clear(CacheNames.SYS_DICT); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.dict.service.impl.DictServiceImpl |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-doc</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-doc 系统接口 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springdoc</groupId> |
| | | <artifactId>springdoc-openapi-starter-webmvc-api</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.github.therapi</groupId> |
| | | <artifactId>therapi-runtime-javadoc</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.module</groupId> |
| | | <artifactId>jackson-module-kotlin</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.doc.config; |
| | | |
| | | import io.swagger.v3.oas.models.Paths; |
| | | |
| | | /** |
| | | * 单独使用一个类便于判断 解决springdoc路径拼接重复问题 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class PlusPaths extends Paths { |
| | | |
| | | public PlusPaths() { |
| | | super(); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.doc.config; |
| | | |
| | | import io.swagger.v3.oas.models.OpenAPI; |
| | | import io.swagger.v3.oas.models.Paths; |
| | | import io.swagger.v3.oas.models.info.Info; |
| | | import io.swagger.v3.oas.models.security.SecurityRequirement; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.doc.config.properties.SpringDocProperties; |
| | | import org.dromara.common.doc.handler.OpenApiHandler; |
| | | import org.springdoc.core.configuration.SpringDocConfiguration; |
| | | import org.springdoc.core.customizers.OpenApiBuilderCustomizer; |
| | | import org.springdoc.core.customizers.OpenApiCustomizer; |
| | | import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| | | import org.springdoc.core.properties.SpringDocConfigProperties; |
| | | import org.springdoc.core.providers.JavadocProvider; |
| | | import org.springdoc.core.service.OpenAPIService; |
| | | import org.springdoc.core.service.SecurityService; |
| | | import org.springdoc.core.utils.PropertyResolverUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.autoconfigure.web.ServerProperties; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Swagger 文档配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @RequiredArgsConstructor |
| | | @AutoConfiguration(before = SpringDocConfiguration.class) |
| | | @EnableConfigurationProperties(SpringDocProperties.class) |
| | | @ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true) |
| | | public class SpringDocAutoConfiguration { |
| | | |
| | | private final ServerProperties serverProperties; |
| | | |
| | | @Value("${spring.application.name}") |
| | | private String appName; |
| | | |
| | | @Bean |
| | | @ConditionalOnMissingBean(OpenAPI.class) |
| | | public OpenAPI openApi(SpringDocProperties properties) { |
| | | OpenAPI openApi = new OpenAPI(); |
| | | // 文档基本信息 |
| | | SpringDocProperties.InfoProperties infoProperties = properties.getInfo(); |
| | | Info info = convertInfo(infoProperties); |
| | | openApi.info(info); |
| | | // 扩展文档信息 |
| | | openApi.externalDocs(properties.getExternalDocs()); |
| | | openApi.tags(properties.getTags()); |
| | | openApi.paths(properties.getPaths()); |
| | | openApi.components(properties.getComponents()); |
| | | Set<String> keySet = properties.getComponents().getSecuritySchemes().keySet(); |
| | | List<SecurityRequirement> list = new ArrayList<>(); |
| | | SecurityRequirement securityRequirement = new SecurityRequirement(); |
| | | keySet.forEach(securityRequirement::addList); |
| | | list.add(securityRequirement); |
| | | openApi.security(list); |
| | | |
| | | return openApi; |
| | | } |
| | | |
| | | private Info convertInfo(SpringDocProperties.InfoProperties infoProperties) { |
| | | Info info = new Info(); |
| | | info.setTitle(infoProperties.getTitle()); |
| | | info.setDescription(infoProperties.getDescription()); |
| | | info.setContact(infoProperties.getContact()); |
| | | info.setLicense(infoProperties.getLicense()); |
| | | info.setVersion(infoProperties.getVersion()); |
| | | return info; |
| | | } |
| | | |
| | | /** |
| | | * 自定义 openapi 处理器 |
| | | */ |
| | | @Bean |
| | | public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI, |
| | | SecurityService securityParser, |
| | | SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, |
| | | Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers, |
| | | Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers, Optional<JavadocProvider> javadocProvider) { |
| | | return new OpenApiHandler(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider); |
| | | } |
| | | |
| | | /** |
| | | * 对已经生成好的 OpenApi 进行自定义操作 |
| | | */ |
| | | @Bean |
| | | public OpenApiCustomizer openApiCustomizer() { |
| | | // 拼接服务路径 |
| | | String appPath = "/" + StringUtils.substring(appName, appName.indexOf("-") + 1); |
| | | String contextPath = serverProperties.getServlet().getContextPath(); |
| | | String finalContextPath; |
| | | if (StringUtils.isBlank(contextPath) || "/".equals(contextPath)) { |
| | | finalContextPath = appPath; |
| | | } else { |
| | | finalContextPath = appPath + contextPath; |
| | | } |
| | | // 对所有路径增加前置上下文路径 |
| | | return openApi -> { |
| | | Paths oldPaths = openApi.getPaths(); |
| | | if (oldPaths instanceof PlusPaths) { |
| | | return; |
| | | } |
| | | PlusPaths newPaths = new PlusPaths(); |
| | | oldPaths.forEach((k, v) -> newPaths.addPathItem(finalContextPath + k, v)); |
| | | openApi.setPaths(newPaths); |
| | | }; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.doc.config.properties; |
| | | |
| | | import io.swagger.v3.oas.models.Components; |
| | | import io.swagger.v3.oas.models.ExternalDocumentation; |
| | | import io.swagger.v3.oas.models.Paths; |
| | | import io.swagger.v3.oas.models.info.Contact; |
| | | import io.swagger.v3.oas.models.info.License; |
| | | import io.swagger.v3.oas.models.tags.Tag; |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.boot.context.properties.NestedConfigurationProperty; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * swagger 配置属性 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "springdoc") |
| | | public class SpringDocProperties { |
| | | |
| | | /** |
| | | * 文档基本信息 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private InfoProperties info = new InfoProperties(); |
| | | |
| | | /** |
| | | * 扩展文档地址 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private ExternalDocumentation externalDocs; |
| | | |
| | | /** |
| | | * 标签 |
| | | */ |
| | | private List<Tag> tags = null; |
| | | |
| | | /** |
| | | * 路径 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private Paths paths = null; |
| | | |
| | | /** |
| | | * 组件 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private Components components = null; |
| | | |
| | | /** |
| | | * <p> |
| | | * 文档的基础属性信息 |
| | | * </p> |
| | | * |
| | | * @see io.swagger.v3.oas.models.info.Info |
| | | * |
| | | * 为了 springboot 自动生产配置提示信息,所以这里复制一个类出来 |
| | | */ |
| | | @Data |
| | | public static class InfoProperties { |
| | | |
| | | /** |
| | | * 标题 |
| | | */ |
| | | private String title = null; |
| | | |
| | | /** |
| | | * 描述 |
| | | */ |
| | | private String description = null; |
| | | |
| | | /** |
| | | * 联系人信息 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private Contact contact = null; |
| | | |
| | | /** |
| | | * 许可证 |
| | | */ |
| | | @NestedConfigurationProperty |
| | | private License license = null; |
| | | |
| | | /** |
| | | * 版本 |
| | | */ |
| | | private String version = null; |
| | | |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.doc.handler; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import io.swagger.v3.core.jackson.TypeNameResolver; |
| | | import io.swagger.v3.core.util.AnnotationsUtils; |
| | | import io.swagger.v3.oas.annotations.tags.Tags; |
| | | import io.swagger.v3.oas.models.Components; |
| | | import io.swagger.v3.oas.models.OpenAPI; |
| | | import io.swagger.v3.oas.models.Operation; |
| | | import io.swagger.v3.oas.models.Paths; |
| | | import io.swagger.v3.oas.models.tags.Tag; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springdoc.core.customizers.OpenApiBuilderCustomizer; |
| | | import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| | | import org.springdoc.core.properties.SpringDocConfigProperties; |
| | | import org.springdoc.core.providers.JavadocProvider; |
| | | import org.springdoc.core.service.OpenAPIService; |
| | | import org.springdoc.core.service.SecurityService; |
| | | import org.springdoc.core.utils.PropertyResolverUtils; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.core.annotation.AnnotatedElementUtils; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | |
| | | import java.io.StringReader; |
| | | import java.lang.reflect.Method; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * 自定义 openapi 处理器 |
| | | * 对源码功能进行修改 增强使用 |
| | | */ |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | | public class OpenApiHandler extends OpenAPIService { |
| | | |
| | | /** |
| | | * The Basic error controller. |
| | | */ |
| | | private static Class<?> basicErrorController; |
| | | |
| | | /** |
| | | * The Security parser. |
| | | */ |
| | | private final SecurityService securityParser; |
| | | |
| | | /** |
| | | * The Mappings map. |
| | | */ |
| | | private final Map<String, Object> mappingsMap = new HashMap<>(); |
| | | |
| | | /** |
| | | * The Springdoc tags. |
| | | */ |
| | | private final Map<HandlerMethod, Tag> springdocTags = new HashMap<>(); |
| | | |
| | | /** |
| | | * The Open api builder customisers. |
| | | */ |
| | | private final Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers; |
| | | |
| | | /** |
| | | * The server base URL customisers. |
| | | */ |
| | | private final Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers; |
| | | |
| | | /** |
| | | * The Spring doc config properties. |
| | | */ |
| | | private final SpringDocConfigProperties springDocConfigProperties; |
| | | |
| | | /** |
| | | * The Cached open api map. |
| | | */ |
| | | private final Map<String, OpenAPI> cachedOpenAPI = new HashMap<>(); |
| | | |
| | | /** |
| | | * The Property resolver utils. |
| | | */ |
| | | private final PropertyResolverUtils propertyResolverUtils; |
| | | |
| | | /** |
| | | * The javadoc provider. |
| | | */ |
| | | private final Optional<JavadocProvider> javadocProvider; |
| | | |
| | | /** |
| | | * The Context. |
| | | */ |
| | | private ApplicationContext context; |
| | | |
| | | /** |
| | | * The Open api. |
| | | */ |
| | | private OpenAPI openAPI; |
| | | |
| | | /** |
| | | * The Is servers present. |
| | | */ |
| | | private boolean isServersPresent; |
| | | |
| | | /** |
| | | * The Server base url. |
| | | */ |
| | | private String serverBaseUrl; |
| | | |
| | | /** |
| | | * Instantiates a new Open api builder. |
| | | * |
| | | * @param openAPI the open api |
| | | * @param securityParser the security parser |
| | | * @param springDocConfigProperties the spring doc config properties |
| | | * @param propertyResolverUtils the property resolver utils |
| | | * @param openApiBuilderCustomizers the open api builder customisers |
| | | * @param serverBaseUrlCustomizers the server base url customizers |
| | | * @param javadocProvider the javadoc provider |
| | | */ |
| | | public OpenApiHandler(Optional<OpenAPI> openAPI, SecurityService securityParser, |
| | | SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, |
| | | Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers, |
| | | Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers, |
| | | Optional<JavadocProvider> javadocProvider) { |
| | | super(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider); |
| | | if (openAPI.isPresent()) { |
| | | this.openAPI = openAPI.get(); |
| | | if (this.openAPI.getComponents() == null) |
| | | this.openAPI.setComponents(new Components()); |
| | | if (this.openAPI.getPaths() == null) |
| | | this.openAPI.setPaths(new Paths()); |
| | | if (!CollectionUtils.isEmpty(this.openAPI.getServers())) |
| | | this.isServersPresent = true; |
| | | } |
| | | this.propertyResolverUtils = propertyResolverUtils; |
| | | this.securityParser = securityParser; |
| | | this.springDocConfigProperties = springDocConfigProperties; |
| | | this.openApiBuilderCustomisers = openApiBuilderCustomizers; |
| | | this.serverBaseUrlCustomizers = serverBaseUrlCustomizers; |
| | | this.javadocProvider = javadocProvider; |
| | | if (springDocConfigProperties.isUseFqn()) |
| | | TypeNameResolver.std.setUseFqn(true); |
| | | } |
| | | |
| | | @Override |
| | | public Operation buildTags(HandlerMethod handlerMethod, Operation operation, OpenAPI openAPI, Locale locale) { |
| | | |
| | | Set<Tag> tags = new HashSet<>(); |
| | | Set<String> tagsStr = new HashSet<>(); |
| | | |
| | | buildTagsFromMethod(handlerMethod.getMethod(), tags, tagsStr, locale); |
| | | buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale); |
| | | |
| | | if (!CollectionUtils.isEmpty(tagsStr)) |
| | | tagsStr = tagsStr.stream() |
| | | .map(str -> propertyResolverUtils.resolve(str, locale)) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | if (springdocTags.containsKey(handlerMethod)) { |
| | | Tag tag = springdocTags.get(handlerMethod); |
| | | tagsStr.add(tag.getName()); |
| | | if (openAPI.getTags() == null || !openAPI.getTags().contains(tag)) { |
| | | openAPI.addTagsItem(tag); |
| | | } |
| | | } |
| | | |
| | | if (!CollectionUtils.isEmpty(tagsStr)) { |
| | | if (CollectionUtils.isEmpty(operation.getTags())) |
| | | operation.setTags(new ArrayList<>(tagsStr)); |
| | | else { |
| | | Set<String> operationTagsSet = new HashSet<>(operation.getTags()); |
| | | operationTagsSet.addAll(tagsStr); |
| | | operation.getTags().clear(); |
| | | operation.getTags().addAll(operationTagsSet); |
| | | } |
| | | } |
| | | |
| | | if (isAutoTagClasses(operation)) { |
| | | |
| | | |
| | | if (javadocProvider.isPresent()) { |
| | | String description = javadocProvider.get().getClassJavadoc(handlerMethod.getBeanType()); |
| | | if (StringUtils.isNotBlank(description)) { |
| | | Tag tag = new Tag(); |
| | | |
| | | // 自定义部分 修改使用java注释当tag名 |
| | | List<String> list = IoUtil.readLines(new StringReader(description), new ArrayList<>()); |
| | | // tag.setName(tagAutoName); |
| | | tag.setName(list.get(0)); |
| | | operation.addTagsItem(list.get(0)); |
| | | |
| | | tag.setDescription(description); |
| | | if (openAPI.getTags() == null || !openAPI.getTags().contains(tag)) { |
| | | openAPI.addTagsItem(tag); |
| | | } |
| | | } |
| | | } else { |
| | | String tagAutoName = splitCamelCase(handlerMethod.getBeanType().getSimpleName()); |
| | | operation.addTagsItem(tagAutoName); |
| | | } |
| | | } |
| | | |
| | | if (!CollectionUtils.isEmpty(tags)) { |
| | | // Existing tags |
| | | List<Tag> openApiTags = openAPI.getTags(); |
| | | if (!CollectionUtils.isEmpty(openApiTags)) |
| | | tags.addAll(openApiTags); |
| | | openAPI.setTags(new ArrayList<>(tags)); |
| | | } |
| | | |
| | | // Handle SecurityRequirement at operation level |
| | | io.swagger.v3.oas.annotations.security.SecurityRequirement[] securityRequirements = securityParser |
| | | .getSecurityRequirements(handlerMethod); |
| | | if (securityRequirements != null) { |
| | | if (securityRequirements.length == 0) |
| | | operation.setSecurity(Collections.emptyList()); |
| | | else |
| | | securityParser.buildSecurityRequirement(securityRequirements, operation); |
| | | } |
| | | |
| | | return operation; |
| | | } |
| | | |
| | | private void buildTagsFromMethod(Method method, Set<Tag> tags, Set<String> tagsStr, Locale locale) { |
| | | // method tags |
| | | Set<Tags> tagsSet = AnnotatedElementUtils |
| | | .findAllMergedAnnotations(method, Tags.class); |
| | | Set<io.swagger.v3.oas.annotations.tags.Tag> methodTags = tagsSet.stream() |
| | | .flatMap(x -> Stream.of(x.value())).collect(Collectors.toSet()); |
| | | methodTags.addAll(AnnotatedElementUtils.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class)); |
| | | if (!CollectionUtils.isEmpty(methodTags)) { |
| | | tagsStr.addAll(methodTags.stream().map(tag -> propertyResolverUtils.resolve(tag.name(), locale)).collect(Collectors.toSet())); |
| | | List<io.swagger.v3.oas.annotations.tags.Tag> allTags = new ArrayList<>(methodTags); |
| | | addTags(allTags, tags, locale); |
| | | } |
| | | } |
| | | |
| | | private void addTags(List<io.swagger.v3.oas.annotations.tags.Tag> sourceTags, Set<Tag> tags, Locale locale) { |
| | | Optional<Set<Tag>> optionalTagSet = AnnotationsUtils |
| | | .getTags(sourceTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[0]), true); |
| | | optionalTagSet.ifPresent(tagsSet -> { |
| | | tagsSet.forEach(tag -> { |
| | | tag.name(propertyResolverUtils.resolve(tag.getName(), locale)); |
| | | tag.description(propertyResolverUtils.resolve(tag.getDescription(), locale)); |
| | | if (tags.stream().noneMatch(t -> t.getName().equals(tag.getName()))) |
| | | tags.add(tag); |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.doc.config.SpringDocAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-dubbo</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-dubbo |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-context</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-actuator</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-spring-boot3-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Sa-Token 整合 Dubbo --> |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-dubbo3</artifactId> |
| | | <version>${satoken.version}</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.dubbo.config; |
| | | |
| | | import org.dromara.common.core.factory.YmlPropertySourceFactory; |
| | | import org.dromara.common.dubbo.properties.DubboCustomProperties; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.PropertySource; |
| | | |
| | | /** |
| | | * dubbo 配置类 |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(DubboCustomProperties.class) |
| | | @PropertySource(value = "classpath:common-dubbo.yml", factory = YmlPropertySourceFactory.class) |
| | | public class DubboConfiguration { |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.dubbo.enumd; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | |
| | | /** |
| | | * 请求日志泛型 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AllArgsConstructor |
| | | public enum RequestLogEnum { |
| | | |
| | | /** |
| | | * info 基础信息 param 参数信息 full 全部 |
| | | */ |
| | | INFO, PARAM, FULL; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.dubbo.filter; |
| | | |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.dubbo.enumd.RequestLogEnum; |
| | | import org.dromara.common.dubbo.properties.DubboCustomProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.dubbo.common.constants.CommonConstants; |
| | | import org.apache.dubbo.common.extension.Activate; |
| | | import org.apache.dubbo.rpc.*; |
| | | import org.apache.dubbo.rpc.service.GenericService; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | |
| | | /** |
| | | * dubbo日志过滤器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = Integer.MAX_VALUE) |
| | | public class DubboRequestFilter implements Filter { |
| | | |
| | | @Override |
| | | public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { |
| | | DubboCustomProperties properties = SpringUtils.getBean(DubboCustomProperties.class); |
| | | if (!properties.getRequestLog()) { |
| | | // 未开启则跳过日志逻辑 |
| | | return invoker.invoke(invocation); |
| | | } |
| | | String client = CommonConstants.PROVIDER; |
| | | if (RpcContext.getServiceContext().isConsumerSide()) { |
| | | client = CommonConstants.CONSUMER; |
| | | } |
| | | String baselog = "Client[" + client + "],InterfaceName=[" + invocation.getInvoker().getInterface().getSimpleName() + "],MethodName=[" + invocation.getMethodName() + "]"; |
| | | if (properties.getLogLevel() == RequestLogEnum.INFO) { |
| | | log.info("DUBBO - 服务调用: {}", baselog); |
| | | } else { |
| | | log.info("DUBBO - 服务调用: {},Parameter={}", baselog, invocation.getArguments()); |
| | | } |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | // 执行接口调用逻辑 |
| | | Result result = invoker.invoke(invocation); |
| | | // 调用耗时 |
| | | long elapsed = System.currentTimeMillis() - startTime; |
| | | // 如果发生异常 则打印异常日志 |
| | | if (result.hasException() && invoker.getInterface().equals(GenericService.class)) { |
| | | log.error("DUBBO - 服务异常: {},Exception={}", baselog, result.getException()); |
| | | } else { |
| | | if (properties.getLogLevel() == RequestLogEnum.INFO) { |
| | | log.info("DUBBO - 服务响应: {},SpendTime=[{}ms]", baselog, elapsed); |
| | | } else if (properties.getLogLevel() == RequestLogEnum.FULL) { |
| | | log.info("DUBBO - 服务响应: {},SpendTime=[{}ms],Response={}", baselog, elapsed, JsonUtils.toJsonString(new Object[]{result.getValue()})); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.dubbo.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.dromara.common.dubbo.enumd.RequestLogEnum; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.cloud.context.config.annotation.RefreshScope; |
| | | |
| | | /** |
| | | * 自定义配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @RefreshScope |
| | | @ConfigurationProperties(prefix = "dubbo.custom") |
| | | public class DubboCustomProperties { |
| | | |
| | | private Boolean requestLog; |
| | | |
| | | private RequestLogEnum logLevel; |
| | | |
| | | } |
New file |
| | |
| | | dubboRequestFilter=org.dromara.common.dubbo.filter.DubboRequestFilter |
New file |
| | |
| | | org.dromara.common.dubbo.config.DubboConfiguration |
New file |
| | |
| | | # 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖 |
| | | dubbo: |
| | | application: |
| | | logger: slf4j |
| | | # 元数据中心 local 本地 remote 远程 这里使用远程便于其他服务获取 |
| | | metadataType: remote |
| | | # 可选值 interface、instance、all,默认是 all,即接口级地址、应用级地址都注册 |
| | | register-mode: instance |
| | | service-discovery: |
| | | # FORCE_INTERFACE,只消费接口级地址,如无地址则报错,单订阅 2.x 地址 |
| | | # APPLICATION_FIRST,智能决策接口级/应用级地址,双订阅 |
| | | # FORCE_APPLICATION,只消费应用级地址,如无地址则报错,单订阅 3.x 地址 |
| | | migration: FORCE_APPLICATION |
| | | # 注册中心配置 |
| | | registry: |
| | | address: nacos://${spring.cloud.nacos.server-addr} |
| | | group: DUBBO_GROUP |
| | | parameters: |
| | | namespace: ${spring.profiles.active} |
| | | # 消费者相关配置 |
| | | consumer: |
| | | # 结果缓存(LRU算法) |
| | | # 会有数据不一致问题 建议在注解局部开启 |
| | | cache: false |
| | | # 支持校验注解 |
| | | validation: jvalidationNew |
| | | # 调用重试 不包括第一次 0为不需要重试 |
| | | retries: 0 |
| | | # 初始化检查 |
| | | check: false |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-elasticsearch</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-elasticsearch ES搜索引擎服务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara.easy-es</groupId> |
| | | <artifactId>easy-es-boot-starter</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.elasticsearch.config; |
| | | |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.env.EnvironmentPostProcessor; |
| | | import org.springframework.core.Ordered; |
| | | import org.springframework.core.env.ConfigurableEnvironment; |
| | | |
| | | /** |
| | | * 健康检查配置注入 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class ActuatorEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { |
| | | |
| | | @Override |
| | | public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| | | System.setProperty("management.health.elasticsearch.enabled", "false"); |
| | | } |
| | | |
| | | @Override |
| | | public int getOrder() { |
| | | return Ordered.HIGHEST_PRECEDENCE; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.elasticsearch.config; |
| | | |
| | | import org.dromara.easyes.starter.register.EsMapperScan; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | |
| | | /** |
| | | * easy-es 配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | @ConditionalOnProperty(value = "easy-es.enable", havingValue = "true") |
| | | @EsMapperScan("org.dromara.**.esmapper") |
| | | public class EasyEsConfiguration { |
| | | |
| | | } |
New file |
| | |
| | | org.springframework.boot.env.EnvironmentPostProcessor=\ |
| | | org.dromara.common.elasticsearch.config.ActuatorEnvironmentPostProcessor |
New file |
| | |
| | | org.dromara.common.elasticsearch.config.EasyEsConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-encrypt</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-encrypt 数据加解密模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.mybatis.spring.boot</groupId> |
| | | <artifactId>mybatis-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.bouncycastle</groupId> |
| | | <artifactId>bcprov-jdk15to18</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-crypto</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | | <artifactId>spring-webmvc</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.encrypt.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 强制加密注解 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @Documented |
| | | @Target({ElementType.METHOD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface ApiEncrypt { |
| | | |
| | | /** |
| | | * 响应加密忽略,默认不加密,为 true 时加密 |
| | | */ |
| | | boolean response() default false; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.annotation; |
| | | |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 字段加密注解 |
| | | * |
| | | * @author 老马 |
| | | */ |
| | | @Documented |
| | | @Inherited |
| | | @Target({ElementType.FIELD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface EncryptField { |
| | | |
| | | /** |
| | | * 加密算法 |
| | | */ |
| | | AlgorithmType algorithm() default AlgorithmType.DEFAULT; |
| | | |
| | | /** |
| | | * 秘钥。AES、SM4需要 |
| | | */ |
| | | String password() default ""; |
| | | |
| | | /** |
| | | * 公钥。RSA、SM2需要 |
| | | */ |
| | | String publicKey() default ""; |
| | | |
| | | /** |
| | | * 私钥。RSA、SM2需要 |
| | | */ |
| | | String privateKey() default ""; |
| | | |
| | | /** |
| | | * 编码方式。对加密算法为BASE64的不起作用 |
| | | */ |
| | | EncodeType encode() default EncodeType.DEFAULT; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.config; |
| | | |
| | | import jakarta.servlet.DispatcherType; |
| | | import org.dromara.common.encrypt.filter.CryptoFilter; |
| | | import org.dromara.common.encrypt.properties.ApiDecryptProperties; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * api 解密自动配置 |
| | | * |
| | | * @author wdhcr |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(ApiDecryptProperties.class) |
| | | @ConditionalOnProperty(value = "api-decrypt.enabled", havingValue = "true") |
| | | public class ApiDecryptAutoConfiguration { |
| | | |
| | | @Bean |
| | | public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration(ApiDecryptProperties properties) { |
| | | FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>(); |
| | | registration.setDispatcherTypes(DispatcherType.REQUEST); |
| | | registration.setFilter(new CryptoFilter(properties)); |
| | | registration.addUrlPatterns("/*"); |
| | | registration.setName("cryptoFilter"); |
| | | registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE); |
| | | return registration; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.config; |
| | | |
| | | import org.dromara.common.encrypt.core.EncryptorManager; |
| | | import org.dromara.common.encrypt.interceptor.MybatisDecryptInterceptor; |
| | | import org.dromara.common.encrypt.interceptor.MybatisEncryptInterceptor; |
| | | import org.dromara.common.encrypt.properties.EncryptorProperties; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * 加解密配置 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(EncryptorProperties.class) |
| | | @ConditionalOnProperty(value = "mybatis-encryptor.enable", havingValue = "true") |
| | | public class EncryptorAutoConfiguration { |
| | | |
| | | @Autowired |
| | | private EncryptorProperties properties; |
| | | |
| | | @Bean |
| | | public EncryptorManager encryptorManager() { |
| | | return new EncryptorManager(); |
| | | } |
| | | |
| | | @Bean |
| | | public MybatisEncryptInterceptor mybatisEncryptInterceptor(EncryptorManager encryptorManager) { |
| | | return new MybatisEncryptInterceptor(encryptorManager, properties); |
| | | } |
| | | |
| | | @Bean |
| | | public MybatisDecryptInterceptor mybatisDecryptInterceptor(EncryptorManager encryptorManager) { |
| | | return new MybatisDecryptInterceptor(encryptorManager, properties); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core; |
| | | |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * 加密上下文 用于encryptor传递必要的参数。 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Data |
| | | public class EncryptContext { |
| | | |
| | | /** |
| | | * 默认算法 |
| | | */ |
| | | private AlgorithmType algorithm; |
| | | |
| | | /** |
| | | * 安全秘钥 |
| | | */ |
| | | private String password; |
| | | |
| | | /** |
| | | * 公钥 |
| | | */ |
| | | private String publicKey; |
| | | |
| | | /** |
| | | * 私钥 |
| | | */ |
| | | private String privateKey; |
| | | |
| | | /** |
| | | * 编码方式,base64/hex |
| | | */ |
| | | private EncodeType encode; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core; |
| | | |
| | | import cn.hutool.core.util.ReflectUtil; |
| | | import org.dromara.common.encrypt.annotation.EncryptField; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 加密管理类 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Slf4j |
| | | public class EncryptorManager { |
| | | |
| | | /** |
| | | * 缓存加密器 |
| | | */ |
| | | Map<EncryptContext, IEncryptor> encryptorMap = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * 类加密字段缓存 |
| | | */ |
| | | Map<Class<?>, Set<Field>> fieldCache = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * 获取类加密字段缓存 |
| | | */ |
| | | public Set<Field> getFieldCache(Class<?> sourceClazz) { |
| | | return fieldCache.computeIfAbsent(sourceClazz, clazz -> { |
| | | Set<Field> fieldSet = new HashSet<>(); |
| | | while (clazz != null) { |
| | | Field[] fields = clazz.getDeclaredFields(); |
| | | fieldSet.addAll(Arrays.asList(fields)); |
| | | clazz = clazz.getSuperclass(); |
| | | } |
| | | fieldSet = fieldSet.stream().filter(field -> |
| | | field.isAnnotationPresent(EncryptField.class) && field.getType() == String.class) |
| | | .collect(Collectors.toSet()); |
| | | for (Field field : fieldSet) { |
| | | field.setAccessible(true); |
| | | } |
| | | return fieldSet; |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 注册加密执行者到缓存 |
| | | * |
| | | * @param encryptContext 加密执行者需要的相关配置参数 |
| | | */ |
| | | public IEncryptor registAndGetEncryptor(EncryptContext encryptContext) { |
| | | if (encryptorMap.containsKey(encryptContext)) { |
| | | return encryptorMap.get(encryptContext); |
| | | } |
| | | IEncryptor encryptor = ReflectUtil.newInstance(encryptContext.getAlgorithm().getClazz(), encryptContext); |
| | | encryptorMap.put(encryptContext, encryptor); |
| | | return encryptor; |
| | | } |
| | | |
| | | /** |
| | | * 移除缓存中的加密执行者 |
| | | * |
| | | * @param encryptContext 加密执行者需要的相关配置参数 |
| | | */ |
| | | public void removeEncryptor(EncryptContext encryptContext) { |
| | | this.encryptorMap.remove(encryptContext); |
| | | } |
| | | |
| | | /** |
| | | * 根据配置进行加密。会进行本地缓存对应的算法和对应的秘钥信息。 |
| | | * |
| | | * @param value 待加密的值 |
| | | * @param encryptContext 加密相关的配置信息 |
| | | */ |
| | | public String encrypt(String value, EncryptContext encryptContext) { |
| | | IEncryptor encryptor = this.registAndGetEncryptor(encryptContext); |
| | | return encryptor.encrypt(value, encryptContext.getEncode()); |
| | | } |
| | | |
| | | /** |
| | | * 根据配置进行解密 |
| | | * |
| | | * @param value 待解密的值 |
| | | * @param encryptContext 加密相关的配置信息 |
| | | */ |
| | | public String decrypt(String value, EncryptContext encryptContext) { |
| | | IEncryptor encryptor = this.registAndGetEncryptor(encryptContext); |
| | | return encryptor.decrypt(value); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core; |
| | | |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | |
| | | /** |
| | | * 加解者 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public interface IEncryptor { |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | AlgorithmType algorithm(); |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | * @return 加密后的字符串 |
| | | */ |
| | | String encrypt(String value, EncodeType encodeType); |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @return 解密后的字符串 |
| | | */ |
| | | String decrypt(String value); |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.core.IEncryptor; |
| | | |
| | | /** |
| | | * 所有加密执行者的基类 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public abstract class AbstractEncryptor implements IEncryptor { |
| | | |
| | | public AbstractEncryptor(EncryptContext context) { |
| | | // 用户配置校验与配置注入 |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | /** |
| | | * AES算法实现 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public class AesEncryptor extends AbstractEncryptor { |
| | | |
| | | private final EncryptContext context; |
| | | |
| | | public AesEncryptor(EncryptContext context) { |
| | | super(context); |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | @Override |
| | | public AlgorithmType algorithm() { |
| | | return AlgorithmType.AES; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | if (encodeType == EncodeType.HEX) { |
| | | return EncryptUtils.encryptByAesHex(value, context.getPassword()); |
| | | } else { |
| | | return EncryptUtils.encryptByAes(value, context.getPassword()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return EncryptUtils.decryptByAes(value, context.getPassword()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | /** |
| | | * Base64算法实现 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public class Base64Encryptor extends AbstractEncryptor { |
| | | |
| | | public Base64Encryptor(EncryptContext context) { |
| | | super(context); |
| | | } |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | @Override |
| | | public AlgorithmType algorithm() { |
| | | return AlgorithmType.BASE64; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | return EncryptUtils.encryptByBase64(value); |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return EncryptUtils.decryptByBase64(value); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | |
| | | /** |
| | | * RSA算法实现 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public class RsaEncryptor extends AbstractEncryptor { |
| | | |
| | | private final EncryptContext context; |
| | | |
| | | public RsaEncryptor(EncryptContext context) { |
| | | super(context); |
| | | String privateKey = context.getPrivateKey(); |
| | | String publicKey = context.getPublicKey(); |
| | | if (StringUtils.isAnyEmpty(privateKey, publicKey)) { |
| | | throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。"); |
| | | } |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | @Override |
| | | public AlgorithmType algorithm() { |
| | | return AlgorithmType.RSA; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | if (encodeType == EncodeType.HEX) { |
| | | return EncryptUtils.encryptByRsaHex(value, context.getPublicKey()); |
| | | } else { |
| | | return EncryptUtils.encryptByRsa(value, context.getPublicKey()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return EncryptUtils.decryptByRsa(value, context.getPrivateKey()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | /** |
| | | * sm2算法实现 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public class Sm2Encryptor extends AbstractEncryptor { |
| | | |
| | | private final EncryptContext context; |
| | | |
| | | public Sm2Encryptor(EncryptContext context) { |
| | | super(context); |
| | | String privateKey = context.getPrivateKey(); |
| | | String publicKey = context.getPublicKey(); |
| | | if (StringUtils.isAnyEmpty(privateKey, publicKey)) { |
| | | throw new IllegalArgumentException("SM2公私钥均需要提供,公钥加密,私钥解密。"); |
| | | } |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | @Override |
| | | public AlgorithmType algorithm() { |
| | | return AlgorithmType.SM2; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | if (encodeType == EncodeType.HEX) { |
| | | return EncryptUtils.encryptBySm2Hex(value, context.getPublicKey()); |
| | | } else { |
| | | return EncryptUtils.encryptBySm2(value, context.getPublicKey()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return EncryptUtils.decryptBySm2(value, context.getPrivateKey()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | /** |
| | | * sm4算法实现 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public class Sm4Encryptor extends AbstractEncryptor { |
| | | |
| | | private final EncryptContext context; |
| | | |
| | | public Sm4Encryptor(EncryptContext context) { |
| | | super(context); |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | | * 获得当前算法 |
| | | */ |
| | | @Override |
| | | public AlgorithmType algorithm() { |
| | | return AlgorithmType.SM4; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | * @param encodeType 加密后的编码格式 |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | if (encodeType == EncodeType.HEX) { |
| | | return EncryptUtils.encryptBySm4Hex(value, context.getPassword()); |
| | | } else { |
| | | return EncryptUtils.encryptBySm4(value, context.getPassword()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param value 待加密字符串 |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return EncryptUtils.decryptBySm4(value, context.getPassword()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.enumd; |
| | | |
| | | import org.dromara.common.encrypt.core.encryptor.*; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 算法名称 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum AlgorithmType { |
| | | |
| | | /** |
| | | * 默认走yml配置 |
| | | */ |
| | | DEFAULT(null), |
| | | |
| | | /** |
| | | * base64 |
| | | */ |
| | | BASE64(Base64Encryptor.class), |
| | | |
| | | /** |
| | | * aes |
| | | */ |
| | | AES(AesEncryptor.class), |
| | | |
| | | /** |
| | | * rsa |
| | | */ |
| | | RSA(RsaEncryptor.class), |
| | | |
| | | /** |
| | | * sm2 |
| | | */ |
| | | SM2(Sm2Encryptor.class), |
| | | |
| | | /** |
| | | * sm4 |
| | | */ |
| | | SM4(Sm4Encryptor.class); |
| | | |
| | | private final Class<? extends AbstractEncryptor> clazz; |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.enumd; |
| | | |
| | | /** |
| | | * 编码类型 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | public enum EncodeType { |
| | | |
| | | /** |
| | | * 默认使用yml配置 |
| | | */ |
| | | DEFAULT, |
| | | |
| | | /** |
| | | * base64编码 |
| | | */ |
| | | BASE64, |
| | | |
| | | /** |
| | | * 16进制编码 |
| | | */ |
| | | HEX; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.filter; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import jakarta.servlet.*; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import org.dromara.common.core.constant.HttpStatus; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.annotation.ApiEncrypt; |
| | | import org.dromara.common.encrypt.properties.ApiDecryptProperties; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | import org.springframework.web.servlet.HandlerExceptionResolver; |
| | | import org.springframework.web.servlet.HandlerExecutionChain; |
| | | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | |
| | | /** |
| | | * Crypto 过滤器 |
| | | * |
| | | * @author wdhcr |
| | | */ |
| | | public class CryptoFilter implements Filter { |
| | | private final ApiDecryptProperties properties; |
| | | |
| | | public CryptoFilter(ApiDecryptProperties properties) { |
| | | this.properties = properties; |
| | | } |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
| | | HttpServletRequest servletRequest = (HttpServletRequest) request; |
| | | HttpServletResponse servletResponse = (HttpServletResponse) response; |
| | | |
| | | boolean responseFlag = false; |
| | | ServletRequest requestWrapper = null; |
| | | ServletResponse responseWrapper = null; |
| | | EncryptResponseBodyWrapper responseBodyWrapper = null; |
| | | |
| | | // 是否为 json 请求 |
| | | if (StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE)) { |
| | | // 是否为 put 或者 post 请求 |
| | | if (HttpMethod.PUT.matches(servletRequest.getMethod()) || HttpMethod.POST.matches(servletRequest.getMethod())) { |
| | | // 是否存在加密标头 |
| | | String headerValue = servletRequest.getHeader(properties.getHeaderFlag()); |
| | | // 获取加密注解 |
| | | ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest); |
| | | responseFlag = apiEncrypt != null && apiEncrypt.response(); |
| | | if (StringUtils.isNotBlank(headerValue)) { |
| | | // 请求解密 |
| | | requestWrapper = new DecryptRequestBodyWrapper(servletRequest, properties.getPrivateKey(), properties.getHeaderFlag()); |
| | | } else { |
| | | // 是否有注解,有就报错,没有放行 |
| | | if (ObjectUtil.isNotNull(apiEncrypt)) { |
| | | HandlerExceptionResolver exceptionResolver = SpringUtils.getBean("handlerExceptionResolver", HandlerExceptionResolver.class); |
| | | exceptionResolver.resolveException( |
| | | servletRequest, servletResponse, null, |
| | | new ServiceException("没有访问权限,请联系管理员授权", HttpStatus.FORBIDDEN)); |
| | | return; |
| | | } |
| | | } |
| | | // 判断是否响应加密 |
| | | if (responseFlag) { |
| | | responseBodyWrapper = new EncryptResponseBodyWrapper(servletResponse); |
| | | responseWrapper = responseBodyWrapper; |
| | | } |
| | | } |
| | | } |
| | | |
| | | chain.doFilter( |
| | | ObjectUtil.defaultIfNull(requestWrapper, request), |
| | | ObjectUtil.defaultIfNull(responseWrapper, response)); |
| | | |
| | | if (responseFlag) { |
| | | servletResponse.reset(); |
| | | // 对原始内容加密 |
| | | String encryptContent = responseBodyWrapper.getEncryptContent( |
| | | servletResponse, properties.getPublicKey(), properties.getHeaderFlag()); |
| | | // 对加密后的内容写出 |
| | | servletResponse.getWriter().write(encryptContent); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取 ApiEncrypt 注解 |
| | | */ |
| | | private ApiEncrypt getApiEncryptAnnotation(HttpServletRequest servletRequest) { |
| | | RequestMappingHandlerMapping handlerMapping = SpringUtils.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class); |
| | | // 获取注解 |
| | | try { |
| | | HandlerExecutionChain mappingHandler = handlerMapping.getHandler(servletRequest); |
| | | if (ObjectUtil.isNotNull(mappingHandler)) { |
| | | Object handler = mappingHandler.getHandler(); |
| | | if (ObjectUtil.isNotNull(handler)) { |
| | | // 从handler获取注解 |
| | | if (handler instanceof HandlerMethod handlerMethod) { |
| | | return handlerMethod.getMethodAnnotation(ApiEncrypt.class); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public void destroy() { |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.filter; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import jakarta.servlet.ReadListener; |
| | | import jakarta.servlet.ServletInputStream; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletRequestWrapper; |
| | | import org.dromara.common.core.constant.Constants; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | import org.springframework.http.MediaType; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * 解密请求参数工具类 |
| | | * |
| | | * @author wdhcr |
| | | */ |
| | | public class DecryptRequestBodyWrapper extends HttpServletRequestWrapper { |
| | | |
| | | private final byte[] body; |
| | | |
| | | public DecryptRequestBodyWrapper(HttpServletRequest request, String privateKey, String headerFlag) throws IOException { |
| | | super(request); |
| | | // 获取 AES 密码 采用 RSA 加密 |
| | | String headerRsa = request.getHeader(headerFlag); |
| | | String decryptAes = EncryptUtils.decryptByRsa(headerRsa, privateKey); |
| | | // 解密 AES 密码 |
| | | String aesPassword = EncryptUtils.decryptByBase64(decryptAes); |
| | | request.setCharacterEncoding(Constants.UTF8); |
| | | byte[] readBytes = IoUtil.readBytes(request.getInputStream(), false); |
| | | String requestBody = new String(readBytes, StandardCharsets.UTF_8); |
| | | // 解密 body 采用 AES 加密 |
| | | String decryptBody = EncryptUtils.decryptByAes(requestBody, aesPassword); |
| | | body = decryptBody.getBytes(StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | @Override |
| | | public BufferedReader getReader() { |
| | | return new BufferedReader(new InputStreamReader(getInputStream())); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int getContentLength() { |
| | | return body.length; |
| | | } |
| | | |
| | | @Override |
| | | public long getContentLengthLong() { |
| | | return body.length; |
| | | } |
| | | |
| | | @Override |
| | | public String getContentType() { |
| | | return MediaType.APPLICATION_JSON_VALUE; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public ServletInputStream getInputStream() { |
| | | final ByteArrayInputStream bais = new ByteArrayInputStream(body); |
| | | return new ServletInputStream() { |
| | | @Override |
| | | public int read() { |
| | | return bais.read(); |
| | | } |
| | | |
| | | @Override |
| | | public int available() { |
| | | return body.length; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isFinished() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isReady() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void setReadListener(ReadListener readListener) { |
| | | |
| | | } |
| | | }; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.filter; |
| | | |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.WriteListener; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpServletResponseWrapper; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStreamWriter; |
| | | import java.io.PrintWriter; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * 加密响应参数包装类 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | public class EncryptResponseBodyWrapper extends HttpServletResponseWrapper { |
| | | |
| | | private final ByteArrayOutputStream byteArrayOutputStream; |
| | | private final ServletOutputStream servletOutputStream; |
| | | private final PrintWriter printWriter; |
| | | |
| | | public EncryptResponseBodyWrapper(HttpServletResponse response) throws IOException { |
| | | super(response); |
| | | this.byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | this.servletOutputStream = this.getOutputStream(); |
| | | this.printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream)); |
| | | } |
| | | |
| | | @Override |
| | | public PrintWriter getWriter() { |
| | | return printWriter; |
| | | } |
| | | |
| | | @Override |
| | | public void flushBuffer() throws IOException { |
| | | if (servletOutputStream != null) { |
| | | servletOutputStream.flush(); |
| | | } |
| | | if (printWriter != null) { |
| | | printWriter.flush(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void reset() { |
| | | byteArrayOutputStream.reset(); |
| | | } |
| | | |
| | | public byte[] getResponseData() throws IOException { |
| | | flushBuffer(); |
| | | return byteArrayOutputStream.toByteArray(); |
| | | } |
| | | |
| | | public String getContent() throws IOException { |
| | | flushBuffer(); |
| | | return byteArrayOutputStream.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 获取加密内容 |
| | | * |
| | | * @param servletResponse response |
| | | * @param publicKey RSA公钥 (用于加密 AES 秘钥) |
| | | * @param headerFlag 请求头标志 |
| | | * @return 加密内容 |
| | | * @throws IOException |
| | | */ |
| | | public String getEncryptContent(HttpServletResponse servletResponse, String publicKey, String headerFlag) throws IOException { |
| | | // 生成秘钥 |
| | | String aesPassword = RandomUtil.randomString(32); |
| | | // 秘钥使用 Base64 编码 |
| | | String encryptAes = EncryptUtils.encryptByBase64(aesPassword); |
| | | // Rsa 公钥加密 Base64 编码 |
| | | String encryptPassword = EncryptUtils.encryptByRsa(encryptAes, publicKey); |
| | | |
| | | // 设置响应头 |
| | | servletResponse.setHeader(headerFlag, encryptPassword); |
| | | servletResponse.setHeader("Access-Control-Allow-Origin", "*"); |
| | | servletResponse.setHeader("Access-Control-Allow-Methods", "*"); |
| | | servletResponse.setCharacterEncoding(StandardCharsets.UTF_8.toString()); |
| | | |
| | | // 获取原始内容 |
| | | String originalBody = this.getContent(); |
| | | // 对内容进行加密 |
| | | return EncryptUtils.encryptByAes(originalBody, aesPassword); |
| | | } |
| | | |
| | | @Override |
| | | public ServletOutputStream getOutputStream() throws IOException { |
| | | return new ServletOutputStream() { |
| | | @Override |
| | | public boolean isReady() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void setWriteListener(WriteListener writeListener) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void write(int b) throws IOException { |
| | | byteArrayOutputStream.write(b); |
| | | } |
| | | |
| | | @Override |
| | | public void write(byte[] b) throws IOException { |
| | | byteArrayOutputStream.write(b); |
| | | } |
| | | |
| | | @Override |
| | | public void write(byte[] b, int off, int len) throws IOException { |
| | | byteArrayOutputStream.write(b, off, len); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.interceptor; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.executor.resultset.ResultSetHandler; |
| | | import org.apache.ibatis.plugin.*; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.annotation.EncryptField; |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.core.EncryptorManager; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.properties.EncryptorProperties; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.sql.Statement; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 出参解密拦截器 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Slf4j |
| | | @Intercepts({@Signature( |
| | | type = ResultSetHandler.class, |
| | | method = "handleResultSets", |
| | | args = {Statement.class}) |
| | | }) |
| | | @AllArgsConstructor |
| | | public class MybatisDecryptInterceptor implements Interceptor { |
| | | |
| | | private final EncryptorManager encryptorManager; |
| | | private final EncryptorProperties defaultProperties; |
| | | |
| | | @Override |
| | | public Object intercept(Invocation invocation) throws Throwable { |
| | | // 获取执行mysql执行结果 |
| | | Object result = invocation.proceed(); |
| | | if (result == null) { |
| | | return null; |
| | | } |
| | | decryptHandler(result); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 解密对象 |
| | | * |
| | | * @param sourceObject 待加密对象 |
| | | */ |
| | | private void decryptHandler(Object sourceObject) { |
| | | if (ObjectUtil.isNull(sourceObject)) { |
| | | return; |
| | | } |
| | | if (sourceObject instanceof Map<?, ?> map) { |
| | | new HashSet<>(map.values()).forEach(this::decryptHandler); |
| | | return; |
| | | } |
| | | if (sourceObject instanceof List<?> list) { |
| | | if(CollUtil.isEmpty(list)) { |
| | | return; |
| | | } |
| | | // 判断第一个元素是否含有注解。如果没有直接返回,提高效率 |
| | | Object firstItem = list.get(0); |
| | | if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) { |
| | | return; |
| | | } |
| | | list.forEach(this::decryptHandler); |
| | | return; |
| | | } |
| | | Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass()); |
| | | try { |
| | | for (Field field : fields) { |
| | | field.set(sourceObject, this.decryptField(Convert.toStr(field.get(sourceObject)), field)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("处理解密字段时出错", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 字段值进行加密。通过字段的批注注册新的加密算法 |
| | | * |
| | | * @param value 待加密的值 |
| | | * @param field 待加密字段 |
| | | * @return 加密后结果 |
| | | */ |
| | | private String decryptField(String value, Field field) { |
| | | if (ObjectUtil.isNull(value)) { |
| | | return null; |
| | | } |
| | | EncryptField encryptField = field.getAnnotation(EncryptField.class); |
| | | EncryptContext encryptContext = new EncryptContext(); |
| | | encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm()); |
| | | encryptContext.setEncode(encryptField.encode() == EncodeType.DEFAULT ? defaultProperties.getEncode() : encryptField.encode()); |
| | | encryptContext.setPassword(StringUtils.isBlank(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password()); |
| | | encryptContext.setPrivateKey(StringUtils.isBlank(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey()); |
| | | encryptContext.setPublicKey(StringUtils.isBlank(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey()); |
| | | return this.encryptorManager.decrypt(value, encryptContext); |
| | | } |
| | | |
| | | @Override |
| | | public Object plugin(Object target) { |
| | | return Plugin.wrap(target, this); |
| | | } |
| | | |
| | | @Override |
| | | public void setProperties(Properties properties) { |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.interceptor; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.executor.parameter.ParameterHandler; |
| | | import org.apache.ibatis.plugin.Interceptor; |
| | | import org.apache.ibatis.plugin.Intercepts; |
| | | import org.apache.ibatis.plugin.Invocation; |
| | | import org.apache.ibatis.plugin.Signature; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.annotation.EncryptField; |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.core.EncryptorManager; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.properties.EncryptorProperties; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.sql.PreparedStatement; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 入参加密拦截器 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Slf4j |
| | | @Intercepts({@Signature( |
| | | type = ParameterHandler.class, |
| | | method = "setParameters", |
| | | args = {PreparedStatement.class}) |
| | | }) |
| | | @AllArgsConstructor |
| | | public class MybatisEncryptInterceptor implements Interceptor { |
| | | |
| | | private final EncryptorManager encryptorManager; |
| | | private final EncryptorProperties defaultProperties; |
| | | |
| | | @Override |
| | | public Object intercept(Invocation invocation) throws Throwable { |
| | | return invocation; |
| | | } |
| | | |
| | | @Override |
| | | public Object plugin(Object target) { |
| | | if (target instanceof ParameterHandler parameterHandler) { |
| | | // 进行加密操作 |
| | | Object parameterObject = parameterHandler.getParameterObject(); |
| | | if (ObjectUtil.isNotNull(parameterObject) && !(parameterObject instanceof String)) { |
| | | this.encryptHandler(parameterObject); |
| | | } |
| | | } |
| | | return target; |
| | | } |
| | | |
| | | /** |
| | | * 加密对象 |
| | | * |
| | | * @param sourceObject 待加密对象 |
| | | */ |
| | | private void encryptHandler(Object sourceObject) { |
| | | if (ObjectUtil.isNull(sourceObject)) { |
| | | return; |
| | | } |
| | | if (sourceObject instanceof Map<?, ?> map) { |
| | | new HashSet<>(map.values()).forEach(this::encryptHandler); |
| | | return; |
| | | } |
| | | if (sourceObject instanceof List<?> list) { |
| | | if(CollUtil.isEmpty(list)) { |
| | | return; |
| | | } |
| | | // 判断第一个元素是否含有注解。如果没有直接返回,提高效率 |
| | | Object firstItem = list.get(0); |
| | | if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) { |
| | | return; |
| | | } |
| | | list.forEach(this::encryptHandler); |
| | | return; |
| | | } |
| | | Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass()); |
| | | try { |
| | | for (Field field : fields) { |
| | | field.set(sourceObject, this.encryptField(Convert.toStr(field.get(sourceObject)), field)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("处理加密字段时出错", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 字段值进行加密。通过字段的批注注册新的加密算法 |
| | | * |
| | | * @param value 待加密的值 |
| | | * @param field 待加密字段 |
| | | * @return 加密后结果 |
| | | */ |
| | | private String encryptField(String value, Field field) { |
| | | if (ObjectUtil.isNull(value)) { |
| | | return null; |
| | | } |
| | | EncryptField encryptField = field.getAnnotation(EncryptField.class); |
| | | EncryptContext encryptContext = new EncryptContext(); |
| | | encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm()); |
| | | encryptContext.setEncode(encryptField.encode() == EncodeType.DEFAULT ? defaultProperties.getEncode() : encryptField.encode()); |
| | | encryptContext.setPassword(StringUtils.isBlank(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password()); |
| | | encryptContext.setPrivateKey(StringUtils.isBlank(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey()); |
| | | encryptContext.setPublicKey(StringUtils.isBlank(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey()); |
| | | return this.encryptorManager.encrypt(value, encryptContext); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void setProperties(Properties properties) { |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * api解密属性配置类 |
| | | * @author wdhcr |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "api-decrypt") |
| | | public class ApiDecryptProperties { |
| | | |
| | | /** |
| | | * 加密开关 |
| | | */ |
| | | private Boolean enabled; |
| | | |
| | | /** |
| | | * 头部标识 |
| | | */ |
| | | private String headerFlag; |
| | | |
| | | /** |
| | | * 响应加密公钥 |
| | | */ |
| | | private String publicKey; |
| | | |
| | | /** |
| | | * 请求解密私钥 |
| | | */ |
| | | private String privateKey; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.properties; |
| | | |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * 加解密属性配置类 |
| | | * |
| | | * @author 老马 |
| | | * @version 4.6.0 |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "mybatis-encryptor") |
| | | public class EncryptorProperties { |
| | | |
| | | /** |
| | | * 过滤开关 |
| | | */ |
| | | private Boolean enable; |
| | | |
| | | /** |
| | | * 默认算法 |
| | | */ |
| | | private AlgorithmType algorithm; |
| | | |
| | | /** |
| | | * 安全秘钥 |
| | | */ |
| | | private String password; |
| | | |
| | | /** |
| | | * 公钥 |
| | | */ |
| | | private String publicKey; |
| | | |
| | | /** |
| | | * 私钥 |
| | | */ |
| | | private String privateKey; |
| | | |
| | | /** |
| | | * 编码方式,base64/hex |
| | | */ |
| | | private EncodeType encode; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.encrypt.utils; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import cn.hutool.crypto.SmUtil; |
| | | import cn.hutool.crypto.asymmetric.KeyType; |
| | | import cn.hutool.crypto.asymmetric.RSA; |
| | | import cn.hutool.crypto.asymmetric.SM2; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 安全相关工具类 |
| | | * |
| | | * @author 老马 |
| | | */ |
| | | public class EncryptUtils { |
| | | /** |
| | | * 公钥 |
| | | */ |
| | | public static final String PUBLIC_KEY = "publicKey"; |
| | | /** |
| | | * 私钥 |
| | | */ |
| | | public static final String PRIVATE_KEY = "privateKey"; |
| | | |
| | | /** |
| | | * Base64加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @return 加密后字符串 |
| | | */ |
| | | public static String encryptByBase64(String data) { |
| | | return Base64.encode(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * Base64解密 |
| | | * |
| | | * @param data 待解密数据 |
| | | * @return 解密后字符串 |
| | | */ |
| | | public static String decryptByBase64(String data) { |
| | | return Base64.decodeStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * AES加密 |
| | | * |
| | | * @param data 待解密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 加密后字符串, 采用Base64编码 |
| | | */ |
| | | public static String encryptByAes(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("AES需要传入秘钥信息"); |
| | | } |
| | | // aes算法的秘钥要求是16位、24位、32位 |
| | | int[] array = {16, 24, 32}; |
| | | if (!ArrayUtil.contains(array, password.length())) { |
| | | throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); |
| | | } |
| | | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * AES加密 |
| | | * |
| | | * @param data 待解密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptByAesHex(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("AES需要传入秘钥信息"); |
| | | } |
| | | // aes算法的秘钥要求是16位、24位、32位 |
| | | int[] array = {16, 24, 32}; |
| | | if (!ArrayUtil.contains(array, password.length())) { |
| | | throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); |
| | | } |
| | | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * AES解密 |
| | | * |
| | | * @param data 待解密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 解密后字符串 |
| | | */ |
| | | public static String decryptByAes(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("AES需要传入秘钥信息"); |
| | | } |
| | | // aes算法的秘钥要求是16位、24位、32位 |
| | | int[] array = {16, 24, 32}; |
| | | if (!ArrayUtil.contains(array, password.length())) { |
| | | throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); |
| | | } |
| | | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * sm4加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 加密后字符串, 采用Base64编码 |
| | | */ |
| | | public static String encryptBySm4(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("SM4需要传入秘钥信息"); |
| | | } |
| | | // sm4算法的秘钥要求是16位长度 |
| | | int sm4PasswordLength = 16; |
| | | if (sm4PasswordLength != password.length()) { |
| | | throw new IllegalArgumentException("SM4秘钥长度要求为16位"); |
| | | } |
| | | return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * sm4加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 加密后字符串, 采用Base64编码 |
| | | */ |
| | | public static String encryptBySm4Hex(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("SM4需要传入秘钥信息"); |
| | | } |
| | | // sm4算法的秘钥要求是16位长度 |
| | | int sm4PasswordLength = 16; |
| | | if (sm4PasswordLength != password.length()) { |
| | | throw new IllegalArgumentException("SM4秘钥长度要求为16位"); |
| | | } |
| | | return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * sm4解密 |
| | | * |
| | | * @param data 待解密数据 |
| | | * @param password 秘钥字符串 |
| | | * @return 解密后字符串 |
| | | */ |
| | | public static String decryptBySm4(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("SM4需要传入秘钥信息"); |
| | | } |
| | | // sm4算法的秘钥要求是16位长度 |
| | | int sm4PasswordLength = 16; |
| | | if (sm4PasswordLength != password.length()) { |
| | | throw new IllegalArgumentException("SM4秘钥长度要求为16位"); |
| | | } |
| | | return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 产生sm2加解密需要的公钥和私钥 |
| | | * |
| | | * @return 公私钥Map |
| | | */ |
| | | public static Map<String, String> generateSm2Key() { |
| | | Map<String, String> keyMap = new HashMap<>(2); |
| | | SM2 sm2 = SmUtil.sm2(); |
| | | keyMap.put(PRIVATE_KEY, sm2.getPrivateKeyBase64()); |
| | | keyMap.put(PUBLIC_KEY, sm2.getPublicKeyBase64()); |
| | | return keyMap; |
| | | } |
| | | |
| | | /** |
| | | * sm2公钥加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param publicKey 公钥 |
| | | * @return 加密后字符串, 采用Base64编码 |
| | | */ |
| | | public static String encryptBySm2(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("SM2需要传入公钥进行加密"); |
| | | } |
| | | SM2 sm2 = SmUtil.sm2(null, publicKey); |
| | | return sm2.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * sm2公钥加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param publicKey 公钥 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptBySm2Hex(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("SM2需要传入公钥进行加密"); |
| | | } |
| | | SM2 sm2 = SmUtil.sm2(null, publicKey); |
| | | return sm2.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * sm2私钥解密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param privateKey 私钥 |
| | | * @return 解密后字符串 |
| | | */ |
| | | public static String decryptBySm2(String data, String privateKey) { |
| | | if (StrUtil.isBlank(privateKey)) { |
| | | throw new IllegalArgumentException("SM2需要传入私钥进行解密"); |
| | | } |
| | | SM2 sm2 = SmUtil.sm2(privateKey, null); |
| | | return sm2.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 产生RSA加解密需要的公钥和私钥 |
| | | * |
| | | * @return 公私钥Map |
| | | */ |
| | | public static Map<String, String> generateRsaKey() { |
| | | Map<String, String> keyMap = new HashMap<>(2); |
| | | RSA rsa = SecureUtil.rsa(); |
| | | keyMap.put(PRIVATE_KEY, rsa.getPrivateKeyBase64()); |
| | | keyMap.put(PUBLIC_KEY, rsa.getPublicKeyBase64()); |
| | | return keyMap; |
| | | } |
| | | |
| | | /** |
| | | * rsa公钥加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param publicKey 公钥 |
| | | * @return 加密后字符串, 采用Base64编码 |
| | | */ |
| | | public static String encryptByRsa(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("RSA需要传入公钥进行加密"); |
| | | } |
| | | RSA rsa = SecureUtil.rsa(null, publicKey); |
| | | return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * rsa公钥加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param publicKey 公钥 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptByRsaHex(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("RSA需要传入公钥进行加密"); |
| | | } |
| | | RSA rsa = SecureUtil.rsa(null, publicKey); |
| | | return rsa.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * rsa私钥解密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @param privateKey 私钥 |
| | | * @return 解密后字符串 |
| | | */ |
| | | public static String decryptByRsa(String data, String privateKey) { |
| | | if (StrUtil.isBlank(privateKey)) { |
| | | throw new IllegalArgumentException("RSA需要传入私钥进行解密"); |
| | | } |
| | | RSA rsa = SecureUtil.rsa(privateKey, null); |
| | | return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * md5加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptByMd5(String data) { |
| | | return SecureUtil.md5(data); |
| | | } |
| | | |
| | | /** |
| | | * sha256加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptBySha256(String data) { |
| | | return SecureUtil.sha256(data); |
| | | } |
| | | |
| | | /** |
| | | * sm3加密 |
| | | * |
| | | * @param data 待加密数据 |
| | | * @return 加密后字符串, 采用Hex编码 |
| | | */ |
| | | public static String encryptBySm3(String data) { |
| | | return SmUtil.sm3(data); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.encrypt.config.EncryptorAutoConfiguration |
| | | org.dromara.common.encrypt.config.ApiDecryptAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-excel</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-excel |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>easyexcel</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.excel.annotation; |
| | | |
| | | import org.dromara.common.excel.core.CellMergeStrategy; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * excel 列单元格合并(合并列相同项) |
| | | * |
| | | * 需搭配 {@link CellMergeStrategy} 策略使用 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target(ElementType.FIELD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface CellMerge { |
| | | |
| | | /** |
| | | * col index |
| | | */ |
| | | int index() default -1; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.annotation; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 字典格式化 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target({ElementType.FIELD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface ExcelDictFormat { |
| | | |
| | | /** |
| | | * 如果是字典类型,请设置字典的type值 (如: sys_user_sex) |
| | | */ |
| | | String dictType() default ""; |
| | | |
| | | /** |
| | | * 读取内容转表达式 (如: 0=男,1=女,2=未知) |
| | | */ |
| | | String readConverterExp() default ""; |
| | | |
| | | /** |
| | | * 分隔符,读取字符串组内容 |
| | | */ |
| | | String separator() default StringUtils.SEPARATOR; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 枚举格式化 |
| | | * |
| | | * @author Liang |
| | | */ |
| | | @Target({ElementType.FIELD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface ExcelEnumFormat { |
| | | |
| | | /** |
| | | * 字典枚举类型 |
| | | */ |
| | | Class<? extends Enum<?>> enumClass(); |
| | | |
| | | /** |
| | | * 字典枚举类中对应的code属性名称,默认为code |
| | | */ |
| | | String codeField() default "code"; |
| | | |
| | | /** |
| | | * 字典枚举类中对应的text属性名称,默认为text |
| | | */ |
| | | String textField() default "text"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.convert; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.excel.converters.Converter; |
| | | import com.alibaba.excel.enums.CellDataTypeEnum; |
| | | import com.alibaba.excel.metadata.GlobalConfiguration; |
| | | import com.alibaba.excel.metadata.data.ReadCellData; |
| | | import com.alibaba.excel.metadata.data.WriteCellData; |
| | | import com.alibaba.excel.metadata.property.ExcelContentProperty; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.math.BigDecimal; |
| | | |
| | | /** |
| | | * 大数值转换 |
| | | * Excel 数值长度位15位 大于15位的数值转换位字符串 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class ExcelBigNumberConvert implements Converter<Long> { |
| | | |
| | | @Override |
| | | public Class<Long> supportJavaTypeKey() { |
| | | return Long.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return CellDataTypeEnum.STRING; |
| | | } |
| | | |
| | | @Override |
| | | public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | return Convert.toLong(cellData.getData()); |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | if (ObjectUtil.isNotNull(object)) { |
| | | String str = Convert.toStr(object); |
| | | if (str.length() > 15) { |
| | | return new WriteCellData<>(str); |
| | | } |
| | | } |
| | | WriteCellData<Object> cellData = new WriteCellData<>(new BigDecimal(object)); |
| | | cellData.setType(CellDataTypeEnum.NUMBER); |
| | | return cellData; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.convert; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.excel.converters.Converter; |
| | | import com.alibaba.excel.enums.CellDataTypeEnum; |
| | | import com.alibaba.excel.metadata.GlobalConfiguration; |
| | | import com.alibaba.excel.metadata.data.ReadCellData; |
| | | import com.alibaba.excel.metadata.data.WriteCellData; |
| | | import com.alibaba.excel.metadata.property.ExcelContentProperty; |
| | | import org.dromara.common.excel.annotation.ExcelDictFormat; |
| | | import org.dromara.common.core.service.DictService; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.excel.utils.ExcelUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Field; |
| | | |
| | | /** |
| | | * 字典格式化转换处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class ExcelDictConvert implements Converter<Object> { |
| | | |
| | | @Override |
| | | public Class<Object> supportJavaTypeKey() { |
| | | return Object.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | ExcelDictFormat anno = getAnnotation(contentProperty.getField()); |
| | | String type = anno.dictType(); |
| | | String label = cellData.getStringValue(); |
| | | String value; |
| | | if (StringUtils.isBlank(type)) { |
| | | value = ExcelUtil.reverseByExp(label, anno.readConverterExp(), anno.separator()); |
| | | } else { |
| | | value = SpringUtils.getBean(DictService.class).getDictValue(type, label, anno.separator()); |
| | | } |
| | | return Convert.convert(contentProperty.getField().getType(), value); |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return new WriteCellData<>(""); |
| | | } |
| | | ExcelDictFormat anno = getAnnotation(contentProperty.getField()); |
| | | String type = anno.dictType(); |
| | | String value = Convert.toStr(object); |
| | | String label; |
| | | if (StringUtils.isBlank(type)) { |
| | | label = ExcelUtil.convertByExp(value, anno.readConverterExp(), anno.separator()); |
| | | } else { |
| | | label = SpringUtils.getBean(DictService.class).getDictLabel(type, value, anno.separator()); |
| | | } |
| | | return new WriteCellData<>(label); |
| | | } |
| | | |
| | | private ExcelDictFormat getAnnotation(Field field) { |
| | | return AnnotationUtil.getAnnotation(field, ExcelDictFormat.class); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.convert; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.excel.converters.Converter; |
| | | import com.alibaba.excel.enums.CellDataTypeEnum; |
| | | import com.alibaba.excel.metadata.GlobalConfiguration; |
| | | import com.alibaba.excel.metadata.data.ReadCellData; |
| | | import com.alibaba.excel.metadata.data.WriteCellData; |
| | | import com.alibaba.excel.metadata.property.ExcelContentProperty; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import org.dromara.common.excel.annotation.ExcelEnumFormat; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 枚举格式化转换处理 |
| | | * |
| | | * @author Liang |
| | | */ |
| | | @Slf4j |
| | | public class ExcelEnumConvert implements Converter<Object> { |
| | | |
| | | @Override |
| | | public Class<Object> supportJavaTypeKey() { |
| | | return Object.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | cellData.checkEmpty(); |
| | | // Excel中填入的是枚举中指定的描述 |
| | | Object textValue = switch (cellData.getType()) { |
| | | case STRING, DIRECT_STRING, RICH_TEXT_STRING -> cellData.getStringValue(); |
| | | case NUMBER -> cellData.getNumberValue(); |
| | | case BOOLEAN -> cellData.getBooleanValue(); |
| | | default -> throw new IllegalArgumentException("单元格类型异常!"); |
| | | }; |
| | | // 如果是空值 |
| | | if (ObjectUtil.isNull(textValue)) { |
| | | return null; |
| | | } |
| | | Map<Object, String> enumCodeToTextMap = beforeConvert(contentProperty); |
| | | // 从Java输出至Excel是code转text |
| | | // 因此从Excel转Java应该将text与code对调 |
| | | Map<Object, Object> enumTextToCodeMap = new HashMap<>(); |
| | | enumCodeToTextMap.forEach((key, value) -> enumTextToCodeMap.put(value, key)); |
| | | // 应该从text -> code中查找 |
| | | Object codeValue = enumTextToCodeMap.get(textValue); |
| | | return Convert.convert(contentProperty.getField().getType(), codeValue); |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return new WriteCellData<>(""); |
| | | } |
| | | Map<Object, String> enumValueMap = beforeConvert(contentProperty); |
| | | String value = Convert.toStr(enumValueMap.get(object), ""); |
| | | return new WriteCellData<>(value); |
| | | } |
| | | |
| | | private Map<Object, String> beforeConvert(ExcelContentProperty contentProperty) { |
| | | ExcelEnumFormat anno = getAnnotation(contentProperty.getField()); |
| | | Map<Object, String> enumValueMap = new HashMap<>(); |
| | | Enum<?>[] enumConstants = anno.enumClass().getEnumConstants(); |
| | | for (Enum<?> enumConstant : enumConstants) { |
| | | Object codeValue = ReflectUtils.invokeGetter(enumConstant, anno.codeField()); |
| | | String textValue = ReflectUtils.invokeGetter(enumConstant, anno.textField()); |
| | | enumValueMap.put(codeValue, textValue); |
| | | } |
| | | return enumValueMap; |
| | | } |
| | | |
| | | private ExcelEnumFormat getAnnotation(Field field) { |
| | | return AnnotationUtil.getAnnotation(field, ExcelEnumFormat.class); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.alibaba.excel.annotation.ExcelProperty; |
| | | import com.alibaba.excel.metadata.Head; |
| | | import com.alibaba.excel.write.merge.AbstractMergeStrategy; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import org.dromara.common.excel.annotation.CellMerge; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 列值重复合并策略 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class CellMergeStrategy extends AbstractMergeStrategy { |
| | | |
| | | private final List<CellRangeAddress> cellList; |
| | | private final boolean hasTitle; |
| | | private int rowIndex; |
| | | |
| | | public CellMergeStrategy(List<?> list, boolean hasTitle) { |
| | | this.hasTitle = hasTitle; |
| | | // 行合并开始下标 |
| | | this.rowIndex = hasTitle ? 1 : 0; |
| | | this.cellList = handle(list, hasTitle); |
| | | } |
| | | |
| | | @Override |
| | | protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { |
| | | // judge the list is not null |
| | | if (CollUtil.isNotEmpty(cellList)) { |
| | | // the judge is necessary |
| | | if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == 0) { |
| | | for (CellRangeAddress item : cellList) { |
| | | sheet.addMergedRegion(item); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @SneakyThrows |
| | | private List<CellRangeAddress> handle(List<?> list, boolean hasTitle) { |
| | | List<CellRangeAddress> cellList = new ArrayList<>(); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return cellList; |
| | | } |
| | | Field[] fields = ReflectUtils.getFields(list.get(0).getClass(), field -> !"serialVersionUID".equals(field.getName())); |
| | | |
| | | // 有注解的字段 |
| | | List<Field> mergeFields = new ArrayList<>(); |
| | | List<Integer> mergeFieldsIndex = new ArrayList<>(); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | Field field = fields[i]; |
| | | if (field.isAnnotationPresent(CellMerge.class)) { |
| | | CellMerge cm = field.getAnnotation(CellMerge.class); |
| | | mergeFields.add(field); |
| | | mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index()); |
| | | if (hasTitle) { |
| | | ExcelProperty property = field.getAnnotation(ExcelProperty.class); |
| | | rowIndex = Math.max(rowIndex, property.value().length); |
| | | } |
| | | } |
| | | } |
| | | |
| | | Map<Field, RepeatCell> map = new HashMap<>(); |
| | | // 生成两两合并单元格 |
| | | for (int i = 0; i < list.size(); i++) { |
| | | for (int j = 0; j < mergeFields.size(); j++) { |
| | | Field field = mergeFields.get(j); |
| | | Object val = ReflectUtils.invokeGetter(list.get(i), field.getName()); |
| | | |
| | | int colNum = mergeFieldsIndex.get(j); |
| | | if (!map.containsKey(field)) { |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } else { |
| | | RepeatCell repeatCell = map.get(field); |
| | | Object cellValue = repeatCell.getValue(); |
| | | if (cellValue == null || "".equals(cellValue)) { |
| | | // 空值跳过不合并 |
| | | continue; |
| | | } |
| | | if (!cellValue.equals(val)) { |
| | | if (i - repeatCell.getCurrent() > 1) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); |
| | | } |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } else if (j == 0) { |
| | | if (i == list.size() - 1) { |
| | | if (i > repeatCell.getCurrent()) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); |
| | | } |
| | | } |
| | | } else { |
| | | // 判断前面的是否合并了 |
| | | RepeatCell firstCell = map.get(mergeFields.get(0)); |
| | | if (repeatCell.getCurrent() != firstCell.getCurrent()) { |
| | | if (i == list.size() - 1) { |
| | | if (i > repeatCell.getCurrent()) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); |
| | | } |
| | | } else if (repeatCell.getCurrent() < firstCell.getCurrent()) { |
| | | if (i - repeatCell.getCurrent() > 1) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); |
| | | } |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } |
| | | } else if (i == list.size() - 1) { |
| | | if (i > repeatCell.getCurrent()) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return cellList; |
| | | } |
| | | |
| | | @Data |
| | | @AllArgsConstructor |
| | | static class RepeatCell { |
| | | |
| | | private Object value; |
| | | |
| | | private int current; |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.excel.context.AnalysisContext; |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.excel.exception.ExcelAnalysisException; |
| | | import com.alibaba.excel.exception.ExcelDataConvertException; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.ValidatorUtils; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Excel 导入监听 |
| | | * |
| | | * @author Yjoioooo |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor |
| | | public class DefaultExcelListener<T> extends AnalysisEventListener<T> implements ExcelListener<T> { |
| | | |
| | | /** |
| | | * 是否Validator检验,默认为是 |
| | | */ |
| | | private Boolean isValidate = Boolean.TRUE; |
| | | |
| | | /** |
| | | * excel 表头数据 |
| | | */ |
| | | private Map<Integer, String> headMap; |
| | | |
| | | /** |
| | | * 导入回执 |
| | | */ |
| | | private ExcelResult<T> excelResult; |
| | | |
| | | public DefaultExcelListener(boolean isValidate) { |
| | | this.excelResult = new DefaultExcelResult<>(); |
| | | this.isValidate = isValidate; |
| | | } |
| | | |
| | | /** |
| | | * 处理异常 |
| | | * |
| | | * @param exception ExcelDataConvertException |
| | | * @param context Excel 上下文 |
| | | */ |
| | | @Override |
| | | public void onException(Exception exception, AnalysisContext context) throws Exception { |
| | | String errMsg = null; |
| | | if (exception instanceof ExcelDataConvertException excelDataConvertException) { |
| | | // 如果是某一个单元格的转换异常 能获取到具体行号 |
| | | Integer rowIndex = excelDataConvertException.getRowIndex(); |
| | | Integer columnIndex = excelDataConvertException.getColumnIndex(); |
| | | errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", |
| | | rowIndex + 1, columnIndex + 1, headMap.get(columnIndex)); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | | } |
| | | } |
| | | if (exception instanceof ConstraintViolationException constraintViolationException) { |
| | | Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations(); |
| | | String constraintViolationsMsg = StreamUtils.join(constraintViolations, ConstraintViolation::getMessage, ", "); |
| | | errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | | } |
| | | } |
| | | excelResult.getErrorList().add(errMsg); |
| | | throw new ExcelAnalysisException(errMsg); |
| | | } |
| | | |
| | | @Override |
| | | public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
| | | this.headMap = headMap; |
| | | log.debug("解析到一条表头数据: {}", JsonUtils.toJsonString(headMap)); |
| | | } |
| | | |
| | | @Override |
| | | public void invoke(T data, AnalysisContext context) { |
| | | if (isValidate) { |
| | | ValidatorUtils.validate(data); |
| | | } |
| | | excelResult.getList().add(data); |
| | | } |
| | | |
| | | @Override |
| | | public void doAfterAllAnalysed(AnalysisContext context) { |
| | | log.debug("所有数据解析完成!"); |
| | | } |
| | | |
| | | @Override |
| | | public ExcelResult<T> getExcelResult() { |
| | | return excelResult; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.Setter; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 默认excel返回对象 |
| | | * |
| | | * @author Yjoioooo |
| | | * @author Lion Li |
| | | */ |
| | | public class DefaultExcelResult<T> implements ExcelResult<T> { |
| | | |
| | | /** |
| | | * 数据对象list |
| | | */ |
| | | @Setter |
| | | private List<T> list; |
| | | |
| | | /** |
| | | * 错误信息列表 |
| | | */ |
| | | @Setter |
| | | private List<String> errorList; |
| | | |
| | | public DefaultExcelResult() { |
| | | this.list = new ArrayList<>(); |
| | | this.errorList = new ArrayList<>(); |
| | | } |
| | | |
| | | public DefaultExcelResult(List<T> list, List<String> errorList) { |
| | | this.list = list; |
| | | this.errorList = errorList; |
| | | } |
| | | |
| | | public DefaultExcelResult(ExcelResult<T> excelResult) { |
| | | this.list = excelResult.getList(); |
| | | this.errorList = excelResult.getErrorList(); |
| | | } |
| | | |
| | | @Override |
| | | public List<T> getList() { |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | public List<String> getErrorList() { |
| | | return errorList; |
| | | } |
| | | |
| | | /** |
| | | * 获取导入回执 |
| | | * |
| | | * @return 导入回执 |
| | | */ |
| | | @Override |
| | | public String getAnalysis() { |
| | | int successCount = list.size(); |
| | | int errorCount = errorList.size(); |
| | | if (successCount == 0) { |
| | | return "读取失败,未解析到数据"; |
| | | } else { |
| | | if (errorCount == 0) { |
| | | return StrUtil.format("恭喜您,全部读取成功!共{}条", successCount); |
| | | } else { |
| | | return ""; |
| | | } |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <h1>Excel下拉可选项</h1> |
| | | * 注意:为确保下拉框解析正确,传值务必使用createOptionValue()做为值的拼接 |
| | | * |
| | | * @author Emil.Zhang |
| | | */ |
| | | @Data |
| | | @AllArgsConstructor |
| | | @NoArgsConstructor |
| | | @SuppressWarnings("unused") |
| | | public class DropDownOptions { |
| | | /** |
| | | * 一级下拉所在列index,从0开始算 |
| | | */ |
| | | private int index = 0; |
| | | /** |
| | | * 二级下拉所在的index,从0开始算,不能与一级相同 |
| | | */ |
| | | private int nextIndex = 0; |
| | | /** |
| | | * 一级下拉所包含的数据 |
| | | */ |
| | | private List<String> options = new ArrayList<>(); |
| | | /** |
| | | * 二级下拉所包含的数据Map |
| | | * <p>以每一个一级选项值为Key,每个一级选项对应的二级数据为Value</p> |
| | | */ |
| | | private Map<String, List<String>> nextOptions = new HashMap<>(); |
| | | /** |
| | | * 分隔符 |
| | | */ |
| | | private static final String DELIMITER = "_"; |
| | | |
| | | /** |
| | | * 创建只有一级的下拉选 |
| | | */ |
| | | public DropDownOptions(int index, List<String> options) { |
| | | this.index = index; |
| | | this.options = options; |
| | | } |
| | | |
| | | /** |
| | | * <h2>创建每个选项可选值</h2> |
| | | * <p>注意:不能以数字,特殊符号开头,选项中不可以包含任何运算符号</p> |
| | | * |
| | | * @param vars 可选值内包含的参数 |
| | | * @return 合规的可选值 |
| | | */ |
| | | public static String createOptionValue(Object... vars) { |
| | | StringBuilder stringBuffer = new StringBuilder(); |
| | | String regex = "^[\\S\\d\\u4e00-\\u9fa5]+$"; |
| | | for (int i = 0; i < vars.length; i++) { |
| | | String var = StrUtil.trimToEmpty(String.valueOf(vars[i])); |
| | | if (!var.matches(regex)) { |
| | | throw new ServiceException("选项数据不符合规则,仅允许使用中英文字符以及数字"); |
| | | } |
| | | stringBuffer.append(var); |
| | | if (i < vars.length - 1) { |
| | | // 直至最后一个前,都以_作为切割线 |
| | | stringBuffer.append(DELIMITER); |
| | | } |
| | | } |
| | | if (stringBuffer.toString().matches("^\\d_*$")) { |
| | | throw new ServiceException("禁止以数字开头"); |
| | | } |
| | | return stringBuffer.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 将处理后合理的可选值解析为原始的参数 |
| | | * |
| | | * @param option 经过处理后的合理的可选项 |
| | | * @return 原始的参数 |
| | | */ |
| | | public static List<String> analyzeOptionValue(String option) { |
| | | return StrUtil.split(option, DELIMITER, true, true); |
| | | } |
| | | |
| | | /** |
| | | * 创建级联下拉选项 |
| | | * |
| | | * @param parentList 父实体可选项原始数据 |
| | | * @param parentIndex 父下拉选位置 |
| | | * @param sonList 子实体可选项原始数据 |
| | | * @param sonIndex 子下拉选位置 |
| | | * @param parentHowToGetIdFunction 父类如何获取唯一标识 |
| | | * @param sonHowToGetParentIdFunction 子类如何获取父类的唯一标识 |
| | | * @param howToBuildEveryOption 如何生成下拉选内容 |
| | | * @return 级联下拉选项 |
| | | */ |
| | | public static <T> DropDownOptions buildLinkedOptions(List<T> parentList, |
| | | int parentIndex, |
| | | List<T> sonList, |
| | | int sonIndex, |
| | | Function<T, Number> parentHowToGetIdFunction, |
| | | Function<T, Number> sonHowToGetParentIdFunction, |
| | | Function<T, String> howToBuildEveryOption) { |
| | | DropDownOptions parentLinkSonOptions = new DropDownOptions(); |
| | | // 先创建父类的下拉 |
| | | parentLinkSonOptions.setIndex(parentIndex); |
| | | parentLinkSonOptions.setOptions( |
| | | parentList.stream() |
| | | .map(howToBuildEveryOption) |
| | | .collect(Collectors.toList()) |
| | | ); |
| | | // 提取父-子级联下拉 |
| | | Map<String, List<String>> sonOptions = new HashMap<>(); |
| | | // 父级依据自己的ID分组 |
| | | Map<Number, List<T>> parentGroupByIdMap = |
| | | parentList.stream().collect(Collectors.groupingBy(parentHowToGetIdFunction)); |
| | | // 遍历每个子集,提取到Map中 |
| | | sonList.forEach(everySon -> { |
| | | if (parentGroupByIdMap.containsKey(sonHowToGetParentIdFunction.apply(everySon))) { |
| | | // 找到对应的上级 |
| | | T parentObj = parentGroupByIdMap.get(sonHowToGetParentIdFunction.apply(everySon)).get(0); |
| | | // 提取名称和ID作为Key |
| | | String key = howToBuildEveryOption.apply(parentObj); |
| | | // Key对应的Value |
| | | List<String> thisParentSonOptionList; |
| | | if (sonOptions.containsKey(key)) { |
| | | thisParentSonOptionList = sonOptions.get(key); |
| | | } else { |
| | | thisParentSonOptionList = new ArrayList<>(); |
| | | sonOptions.put(key, thisParentSonOptionList); |
| | | } |
| | | // 往Value中添加当前子集选项 |
| | | thisParentSonOptionList.add(howToBuildEveryOption.apply(everySon)); |
| | | } |
| | | }); |
| | | parentLinkSonOptions.setNextIndex(sonIndex); |
| | | parentLinkSonOptions.setNextOptions(sonOptions); |
| | | return parentLinkSonOptions; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.EnumUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.excel.metadata.FieldCache; |
| | | import com.alibaba.excel.metadata.FieldWrapper; |
| | | import com.alibaba.excel.util.ClassUtils; |
| | | import com.alibaba.excel.write.handler.SheetWriteHandler; |
| | | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| | | import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.apache.poi.ss.util.WorkbookUtil; |
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.service.DictService; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.excel.annotation.ExcelDictFormat; |
| | | import org.dromara.common.excel.annotation.ExcelEnumFormat; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <h1>Excel表格下拉选操作</h1> |
| | | * 考虑到下拉选过多可能导致Excel打开缓慢的问题,只校验前1000行 |
| | | * <p> |
| | | * 即只有前1000行的数据可以用下拉框,超出的自行通过限制数据量的形式,第二次输出 |
| | | * |
| | | * @author Emil.Zhang |
| | | */ |
| | | @Slf4j |
| | | public class ExcelDownHandler implements SheetWriteHandler { |
| | | |
| | | /** |
| | | * Excel表格中的列名英文 |
| | | * 仅为了解析列英文,禁止修改 |
| | | */ |
| | | private static final String EXCEL_COLUMN_NAME = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| | | /** |
| | | * 单选数据Sheet名 |
| | | */ |
| | | private static final String OPTIONS_SHEET_NAME = "options"; |
| | | /** |
| | | * 联动选择数据Sheet名的头 |
| | | */ |
| | | private static final String LINKED_OPTIONS_SHEET_NAME = "linkedOptions"; |
| | | /** |
| | | * 下拉可选项 |
| | | */ |
| | | private final List<DropDownOptions> dropDownOptions; |
| | | /** |
| | | * 当前单选进度 |
| | | */ |
| | | private int currentOptionsColumnIndex; |
| | | /** |
| | | * 当前联动选择进度 |
| | | */ |
| | | private int currentLinkedOptionsSheetIndex; |
| | | private final DictService dictService; |
| | | |
| | | public ExcelDownHandler(List<DropDownOptions> options) { |
| | | this.dropDownOptions = options; |
| | | this.currentOptionsColumnIndex = 0; |
| | | this.currentLinkedOptionsSheetIndex = 0; |
| | | this.dictService = SpringUtils.getBean(DictService.class); |
| | | } |
| | | |
| | | /** |
| | | * <h2>开始创建下拉数据</h2> |
| | | * 1.通过解析传入的@ExcelProperty同级是否标注有@DropDown选项 |
| | | * 如果有且设置了value值,则将其直接置为下拉可选项 |
| | | * <p> |
| | | * 2.或者在调用ExcelUtil时指定了可选项,将依据传入的可选项做下拉 |
| | | * <p> |
| | | * 3.二者并存,注意调用方式 |
| | | */ |
| | | @Override |
| | | public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
| | | Sheet sheet = writeSheetHolder.getSheet(); |
| | | // 开始设置下拉框 HSSFWorkbook |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | Workbook workbook = writeWorkbookHolder.getWorkbook(); |
| | | FieldCache fieldCache = ClassUtils.declaredFields(writeWorkbookHolder.getClazz(), writeWorkbookHolder); |
| | | for (Map.Entry<Integer, FieldWrapper> entry : fieldCache.getSortedFieldMap().entrySet()) { |
| | | Integer index = entry.getKey(); |
| | | FieldWrapper wrapper = entry.getValue(); |
| | | Field field = wrapper.getField(); |
| | | // 循环实体中的每个属性 |
| | | // 可选的下拉值 |
| | | List<String> options = new ArrayList<>(); |
| | | if (field.isAnnotationPresent(ExcelDictFormat.class)) { |
| | | // 如果指定了@ExcelDictFormat,则使用字典的逻辑 |
| | | ExcelDictFormat format = field.getDeclaredAnnotation(ExcelDictFormat.class); |
| | | String dictType = format.dictType(); |
| | | String converterExp = format.readConverterExp(); |
| | | if (StrUtil.isNotBlank(dictType)) { |
| | | // 如果传递了字典名,则依据字典建立下拉 |
| | | Collection<String> values = Optional.ofNullable(dictService.getAllDictByDictType(dictType)) |
| | | .orElseThrow(() -> new ServiceException(String.format("字典 %s 不存在", dictType))) |
| | | .values(); |
| | | options = new ArrayList<>(values); |
| | | } else if (StrUtil.isNotBlank(converterExp)) { |
| | | // 如果指定了确切的值,则直接解析确切的值 |
| | | options = StrUtil.split(converterExp, format.separator(), true, true); |
| | | } |
| | | } else if (field.isAnnotationPresent(ExcelEnumFormat.class)) { |
| | | // 否则如果指定了@ExcelEnumFormat,则使用枚举的逻辑 |
| | | ExcelEnumFormat format = field.getDeclaredAnnotation(ExcelEnumFormat.class); |
| | | List<Object> values = EnumUtil.getFieldValues(format.enumClass(), format.textField()); |
| | | options = StreamUtils.toList(values, String::valueOf); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(options)) { |
| | | // 仅当下拉可选项不为空时执行 |
| | | if (options.size() > 20) { |
| | | // 这里限制如果可选项大于20,则使用额外表形式 |
| | | dropDownWithSheet(helper, workbook, sheet, index, options); |
| | | } else { |
| | | // 否则使用固定值形式 |
| | | dropDownWithSimple(helper, sheet, index, options); |
| | | } |
| | | } |
| | | } |
| | | if (CollUtil.isEmpty(dropDownOptions)) { |
| | | return; |
| | | } |
| | | dropDownOptions.forEach(everyOptions -> { |
| | | // 如果传递了下拉框选择器参数 |
| | | if (!everyOptions.getNextOptions().isEmpty()) { |
| | | // 当二级选项不为空时,使用额外关联表的形式 |
| | | dropDownLinkedOptions(helper, workbook, sheet, everyOptions); |
| | | } else if (everyOptions.getOptions().size() > 10) { |
| | | // 当一级选项参数个数大于10,使用额外表的形式 |
| | | dropDownWithSheet(helper, workbook, sheet, everyOptions.getIndex(), everyOptions.getOptions()); |
| | | } else if (everyOptions.getOptions().size() != 0) { |
| | | // 当一级选项个数不为空,使用默认形式 |
| | | dropDownWithSimple(helper, sheet, everyOptions.getIndex(), everyOptions.getOptions()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * <h2>简单下拉框</h2> |
| | | * 直接将可选项拼接为指定列的数据校验值 |
| | | * |
| | | * @param celIndex 列index |
| | | * @param value 下拉选可选值 |
| | | */ |
| | | private void dropDownWithSimple(DataValidationHelper helper, Sheet sheet, Integer celIndex, List<String> value) { |
| | | if (ObjectUtil.isEmpty(value)) { |
| | | return; |
| | | } |
| | | this.markOptionsToSheet(helper, sheet, celIndex, helper.createExplicitListConstraint(ArrayUtil.toArray(value, String.class))); |
| | | } |
| | | |
| | | /** |
| | | * <h2>额外表格形式的级联下拉框</h2> |
| | | * |
| | | * @param options 额外表格形式存储的下拉可选项 |
| | | */ |
| | | private void dropDownLinkedOptions(DataValidationHelper helper, Workbook workbook, Sheet sheet, DropDownOptions options) { |
| | | String linkedOptionsSheetName = String.format("%s_%d", LINKED_OPTIONS_SHEET_NAME, currentLinkedOptionsSheetIndex); |
| | | // 创建联动下拉数据表 |
| | | Sheet linkedOptionsDataSheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(linkedOptionsSheetName)); |
| | | // 将下拉表隐藏 |
| | | workbook.setSheetHidden(workbook.getSheetIndex(linkedOptionsDataSheet), true); |
| | | // 完善横向的一级选项数据表 |
| | | List<String> firstOptions = options.getOptions(); |
| | | Map<String, List<String>> secoundOptionsMap = options.getNextOptions(); |
| | | |
| | | // 创建名称管理器 |
| | | Name name = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | name.setNameName(linkedOptionsSheetName); |
| | | // 以横向第一行创建一级下拉拼接引用位置 |
| | | String firstOptionsFunction = String.format("%s!$%s$1:$%s$1", |
| | | linkedOptionsSheetName, |
| | | getExcelColumnName(0), |
| | | getExcelColumnName(firstOptions.size()) |
| | | ); |
| | | // 设置名称管理器的引用位置 |
| | | name.setRefersToFormula(firstOptionsFunction); |
| | | // 设置数据校验为序列模式,引用的是名称管理器中的别名 |
| | | this.markOptionsToSheet(helper, sheet, options.getIndex(), helper.createFormulaListConstraint(linkedOptionsSheetName)); |
| | | |
| | | for (int columIndex = 0; columIndex < firstOptions.size(); columIndex++) { |
| | | // 先提取主表中一级下拉的列名 |
| | | String firstOptionsColumnName = getExcelColumnName(columIndex); |
| | | // 一次循环是每一个一级选项 |
| | | int finalI = columIndex; |
| | | // 本次循环的一级选项值 |
| | | String thisFirstOptionsValue = firstOptions.get(columIndex); |
| | | // 创建第一行的数据 |
| | | Optional.ofNullable(linkedOptionsDataSheet.getRow(0)) |
| | | // 如果不存在则创建第一行 |
| | | .orElseGet(() -> linkedOptionsDataSheet.createRow(finalI)) |
| | | // 第一行当前列 |
| | | .createCell(columIndex) |
| | | // 设置值为当前一级选项值 |
| | | .setCellValue(thisFirstOptionsValue); |
| | | |
| | | // 第二行开始,设置第二级别选项参数 |
| | | List<String> secondOptions = secoundOptionsMap.get(thisFirstOptionsValue); |
| | | if (CollUtil.isEmpty(secondOptions)) { |
| | | // 必须保证至少有一个关联选项,否则将导致Excel解析错误 |
| | | secondOptions = Collections.singletonList("暂无_0"); |
| | | } |
| | | |
| | | // 以该一级选项值创建子名称管理器 |
| | | Name sonName = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | sonName.setNameName(thisFirstOptionsValue); |
| | | // 以第二行该列数据拼接引用位置 |
| | | String sonFunction = String.format("%s!$%s$2:$%s$%d", |
| | | linkedOptionsSheetName, |
| | | firstOptionsColumnName, |
| | | firstOptionsColumnName, |
| | | secondOptions.size() + 1 |
| | | ); |
| | | // 设置名称管理器的引用位置 |
| | | sonName.setRefersToFormula(sonFunction); |
| | | // 数据验证为序列模式,引用到每一个主表中的二级选项位置 |
| | | // 创建子项的名称管理器,只是为了使得Excel可以识别到数据 |
| | | String mainSheetFirstOptionsColumnName = getExcelColumnName(options.getIndex()); |
| | | for (int i = 0; i < 100; i++) { |
| | | // 以一级选项对应的主体所在位置创建二级下拉 |
| | | String secondOptionsFunction = String.format("=INDIRECT(%s%d)", mainSheetFirstOptionsColumnName, i + 1); |
| | | // 二级只能主表每一行的每一列添加二级校验 |
| | | markLinkedOptionsToSheet(helper, sheet, i, options.getNextIndex(), helper.createFormulaListConstraint(secondOptionsFunction)); |
| | | } |
| | | |
| | | for (int rowIndex = 0; rowIndex < secondOptions.size(); rowIndex++) { |
| | | // 从第二行开始填充二级选项 |
| | | int finalRowIndex = rowIndex + 1; |
| | | int finalColumIndex = columIndex; |
| | | |
| | | Row row = Optional.ofNullable(linkedOptionsDataSheet.getRow(finalRowIndex)) |
| | | // 没有则创建 |
| | | .orElseGet(() -> linkedOptionsDataSheet.createRow(finalRowIndex)); |
| | | Optional |
| | | // 在本级一级选项所在的列 |
| | | .ofNullable(row.getCell(finalColumIndex)) |
| | | // 不存在则创建 |
| | | .orElseGet(() -> row.createCell(finalColumIndex)) |
| | | // 设置二级选项值 |
| | | .setCellValue(secondOptions.get(rowIndex)); |
| | | } |
| | | } |
| | | |
| | | currentLinkedOptionsSheetIndex++; |
| | | } |
| | | |
| | | /** |
| | | * <h2>额外表格形式的普通下拉框</h2> |
| | | * 由于下拉框可选值数量过多,为提升Excel打开效率,使用额外表格形式做下拉 |
| | | * |
| | | * @param celIndex 下拉选 |
| | | * @param value 下拉选可选值 |
| | | */ |
| | | private void dropDownWithSheet(DataValidationHelper helper, Workbook workbook, Sheet sheet, Integer celIndex, List<String> value) { |
| | | // 创建下拉数据表 |
| | | Sheet simpleDataSheet = Optional.ofNullable(workbook.getSheet(WorkbookUtil.createSafeSheetName(OPTIONS_SHEET_NAME))) |
| | | .orElseGet(() -> workbook.createSheet(WorkbookUtil.createSafeSheetName(OPTIONS_SHEET_NAME))); |
| | | // 将下拉表隐藏 |
| | | workbook.setSheetHidden(workbook.getSheetIndex(simpleDataSheet), true); |
| | | // 完善纵向的一级选项数据表 |
| | | for (int i = 0; i < value.size(); i++) { |
| | | int finalI = i; |
| | | // 获取每一选项行,如果没有则创建 |
| | | Row row = Optional.ofNullable(simpleDataSheet.getRow(i)) |
| | | .orElseGet(() -> simpleDataSheet.createRow(finalI)); |
| | | // 获取本级选项对应的选项列,如果没有则创建 |
| | | Cell cell = Optional.ofNullable(row.getCell(currentOptionsColumnIndex)) |
| | | .orElseGet(() -> row.createCell(currentOptionsColumnIndex)); |
| | | // 设置值 |
| | | cell.setCellValue(value.get(i)); |
| | | } |
| | | |
| | | // 创建名称管理器 |
| | | Name name = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | String nameName = String.format("%s_%d", OPTIONS_SHEET_NAME, celIndex); |
| | | name.setNameName(nameName); |
| | | // 以纵向第一列创建一级下拉拼接引用位置 |
| | | String function = String.format("%s!$%s$1:$%s$%d", |
| | | OPTIONS_SHEET_NAME, |
| | | getExcelColumnName(currentOptionsColumnIndex), |
| | | getExcelColumnName(currentOptionsColumnIndex), |
| | | value.size()); |
| | | // 设置名称管理器的引用位置 |
| | | name.setRefersToFormula(function); |
| | | // 设置数据校验为序列模式,引用的是名称管理器中的别名 |
| | | this.markOptionsToSheet(helper, sheet, celIndex, helper.createFormulaListConstraint(nameName)); |
| | | currentOptionsColumnIndex++; |
| | | } |
| | | |
| | | /** |
| | | * 挂载下拉的列,仅限一级选项 |
| | | */ |
| | | private void markOptionsToSheet(DataValidationHelper helper, Sheet sheet, Integer celIndex, |
| | | DataValidationConstraint constraint) { |
| | | // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 |
| | | CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, celIndex, celIndex); |
| | | markDataValidationToSheet(helper, sheet, constraint, addressList); |
| | | } |
| | | |
| | | /** |
| | | * 挂载下拉的列,仅限二级选项 |
| | | */ |
| | | private void markLinkedOptionsToSheet(DataValidationHelper helper, Sheet sheet, Integer rowIndex, |
| | | Integer celIndex, DataValidationConstraint constraint) { |
| | | // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 |
| | | CellRangeAddressList addressList = new CellRangeAddressList(rowIndex, rowIndex, celIndex, celIndex); |
| | | markDataValidationToSheet(helper, sheet, constraint, addressList); |
| | | } |
| | | |
| | | /** |
| | | * 应用数据校验 |
| | | */ |
| | | private void markDataValidationToSheet(DataValidationHelper helper, Sheet sheet, |
| | | DataValidationConstraint constraint, CellRangeAddressList addressList) { |
| | | // 数据有效性对象 |
| | | DataValidation dataValidation = helper.createValidation(constraint, addressList); |
| | | // 处理Excel兼容性问题 |
| | | if (dataValidation instanceof XSSFDataValidation) { |
| | | //数据校验 |
| | | dataValidation.setSuppressDropDownArrow(true); |
| | | //错误提示 |
| | | dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP); |
| | | dataValidation.createErrorBox("提示", "此值与单元格定义数据不一致"); |
| | | dataValidation.setShowErrorBox(true); |
| | | //选定提示 |
| | | dataValidation.createPromptBox("填写说明:", "填写内容只能为下拉中数据,其他数据将导致导入失败"); |
| | | dataValidation.setShowPromptBox(true); |
| | | sheet.addValidationData(dataValidation); |
| | | } else { |
| | | dataValidation.setSuppressDropDownArrow(false); |
| | | } |
| | | sheet.addValidationData(dataValidation); |
| | | } |
| | | |
| | | /** |
| | | * <h2>依据列index获取列名英文</h2> |
| | | * 依据列index转换为Excel中的列名英文 |
| | | * <p>例如第1列,index为0,解析出来为A列</p> |
| | | * 第27列,index为26,解析为AA列 |
| | | * <p>第28列,index为27,解析为AB列</p> |
| | | * |
| | | * @param columnIndex 列index |
| | | * @return 列index所在得英文名 |
| | | */ |
| | | private String getExcelColumnName(int columnIndex) { |
| | | // 26一循环的次数 |
| | | int columnCircleCount = columnIndex / 26; |
| | | // 26一循环内的位置 |
| | | int thisCircleColumnIndex = columnIndex % 26; |
| | | // 26一循环的次数大于0,则视为栏名至少两位 |
| | | String columnPrefix = columnCircleCount == 0 |
| | | ? StrUtil.EMPTY |
| | | : StrUtil.subWithLength(EXCEL_COLUMN_NAME, columnCircleCount - 1, 1); |
| | | // 从26一循环内取对应的栏位名 |
| | | String columnNext = StrUtil.subWithLength(EXCEL_COLUMN_NAME, thisCircleColumnIndex, 1); |
| | | // 将二者拼接即为最终的栏位名 |
| | | return columnPrefix + columnNext; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import com.alibaba.excel.read.listener.ReadListener; |
| | | |
| | | /** |
| | | * Excel 导入监听 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ExcelListener<T> extends ReadListener<T> { |
| | | |
| | | ExcelResult<T> getExcelResult(); |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.core; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * excel返回对象 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ExcelResult<T> { |
| | | |
| | | /** |
| | | * 对象列表 |
| | | */ |
| | | List<T> getList(); |
| | | |
| | | /** |
| | | * 错误列表 |
| | | */ |
| | | List<String> getErrorList(); |
| | | |
| | | /** |
| | | * 导入回执 |
| | | */ |
| | | String getAnalysis(); |
| | | } |
New file |
| | |
| | | package org.dromara.common.excel.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.io.resource.ClassPathResource; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.alibaba.excel.write.metadata.fill.FillConfig; |
| | | import com.alibaba.excel.write.metadata.fill.FillWrapper; |
| | | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.file.FileUtils; |
| | | import org.dromara.common.excel.convert.ExcelBigNumberConvert; |
| | | import org.dromara.common.excel.core.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Excel相关处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ExcelUtil { |
| | | |
| | | /** |
| | | * 同步导入(适用于小数据量) |
| | | * |
| | | * @param is 输入流 |
| | | * @return 转换后集合 |
| | | */ |
| | | public static <T> List<T> importExcel(InputStream is, Class<T> clazz) { |
| | | return EasyExcel.read(is).head(clazz).autoCloseStream(false).sheet().doReadSync(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 使用校验监听器 异步导入 同步返回 |
| | | * |
| | | * @param is 输入流 |
| | | * @param clazz 对象类型 |
| | | * @param isValidate 是否 Validator 检验 默认为是 |
| | | * @return 转换后集合 |
| | | */ |
| | | public static <T> ExcelResult<T> importExcel(InputStream is, Class<T> clazz, boolean isValidate) { |
| | | DefaultExcelListener<T> listener = new DefaultExcelListener<>(isValidate); |
| | | EasyExcel.read(is, clazz, listener).sheet().doRead(); |
| | | return listener.getExcelResult(); |
| | | } |
| | | |
| | | /** |
| | | * 使用自定义监听器 异步导入 自定义返回 |
| | | * |
| | | * @param is 输入流 |
| | | * @param clazz 对象类型 |
| | | * @param listener 自定义监听器 |
| | | * @return 转换后集合 |
| | | */ |
| | | public static <T> ExcelResult<T> importExcel(InputStream is, Class<T> clazz, ExcelListener<T> listener) { |
| | | EasyExcel.read(is, clazz, listener).sheet().doRead(); |
| | | return listener.getExcelResult(); |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param response 响应体 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, false, os, null); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param response 响应体 |
| | | * @param options 级联下拉选 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, false, os, options); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param merge 是否合并单元格 |
| | | * @param response 响应体 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, merge, os, null); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param merge 是否合并单元格 |
| | | * @param response 响应体 |
| | | * @param options 级联下拉选 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, merge, os, options); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param os 输出流 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os) { |
| | | exportExcel(list, sheetName, clazz, false, os, null); |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param os 输出流 |
| | | * @param options 级联下拉选内容 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os, List<DropDownOptions> options) { |
| | | exportExcel(list, sheetName, clazz, false, os, options); |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param merge 是否合并单元格 |
| | | * @param os 输出流 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, |
| | | OutputStream os, List<DropDownOptions> options) { |
| | | ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) |
| | | .autoCloseStream(false) |
| | | // 自动适配 |
| | | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .sheet(sheetName); |
| | | if (merge) { |
| | | // 合并处理器 |
| | | builder.registerWriteHandler(new CellMergeStrategy(list, true)); |
| | | } |
| | | // 添加下拉框操作 |
| | | builder.registerWriteHandler(new ExcelDownHandler(options)); |
| | | builder.doWrite(list); |
| | | } |
| | | |
| | | /** |
| | | * 单表多数据模板导出 模板格式为 {.属性} |
| | | * |
| | | * @param filename 文件名 |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param response 响应体 |
| | | */ |
| | | public static void exportTemplate(List<Object> data, String filename, String templatePath, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(filename, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportTemplate(data, templatePath, os); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 单表多数据模板导出 模板格式为 {.属性} |
| | | * |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param os 输出流 |
| | | */ |
| | | public static void exportTemplate(List<Object> data, String templatePath, OutputStream os) { |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | // 单表多数据导出 模板格式为 {.属性} |
| | | for (Object d : data) { |
| | | excelWriter.fill(d, writeSheet); |
| | | } |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | /** |
| | | * 多表多数据模板导出 模板格式为 {key.属性} |
| | | * |
| | | * @param filename 文件名 |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param response 响应体 |
| | | */ |
| | | public static void exportTemplateMultiList(Map<String, Object> data, String filename, String templatePath, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(filename, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportTemplateMultiList(data, templatePath, os); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 多sheet模板导出 模板格式为 {key.属性} |
| | | * |
| | | * @param filename 文件名 |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param response 响应体 |
| | | */ |
| | | public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(filename, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportTemplateMultiSheet(data, templatePath, os); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 多表多数据模板导出 模板格式为 {key.属性} |
| | | * |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param os 输出流 |
| | | */ |
| | | public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) { |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | for (Map.Entry<String, Object> map : data.entrySet()) { |
| | | // 设置列表后续还有数据 |
| | | FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
| | | if (map.getValue() instanceof Collection) { |
| | | // 多表导出必须使用 FillWrapper |
| | | excelWriter.fill(new FillWrapper(map.getKey(), (Collection<?>) map.getValue()), fillConfig, writeSheet); |
| | | } else { |
| | | excelWriter.fill(map.getValue(), writeSheet); |
| | | } |
| | | } |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | /** |
| | | * 多sheet模板导出 模板格式为 {key.属性} |
| | | * |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | | * 例如: excel/temp.xlsx |
| | | * 重点: 模板文件必须放置到启动类对应的 resource 目录下 |
| | | * @param data 模板需要的数据 |
| | | * @param os 输出流 |
| | | */ |
| | | public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) { |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | for (int i = 0; i < data.size(); i++) { |
| | | WriteSheet writeSheet = EasyExcel.writerSheet(i).build(); |
| | | for (Map.Entry<String, Object> map : data.get(i).entrySet()) { |
| | | // 设置列表后续还有数据 |
| | | FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
| | | if (map.getValue() instanceof Collection) { |
| | | // 多表导出必须使用 FillWrapper |
| | | excelWriter.fill(new FillWrapper(map.getKey(), (Collection<?>) map.getValue()), fillConfig, writeSheet); |
| | | } else { |
| | | excelWriter.fill(map.getValue(), writeSheet); |
| | | } |
| | | } |
| | | } |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | /** |
| | | * 重置响应体 |
| | | */ |
| | | private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { |
| | | String filename = encodingFilename(sheetName); |
| | | FileUtils.setAttachmentResponseHeader(response, filename); |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); |
| | | } |
| | | |
| | | /** |
| | | * 解析导出值 0=男,1=女,2=未知 |
| | | * |
| | | * @param propertyValue 参数值 |
| | | * @param converterExp 翻译注解 |
| | | * @param separator 分隔符 |
| | | * @return 解析后值 |
| | | */ |
| | | public static String convertByExp(String propertyValue, String converterExp, String separator) { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(StringUtils.SEPARATOR); |
| | | for (String item : convertSource) { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(propertyValue, separator)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[0].equals(value)) { |
| | | propertyString.append(itemArray[1] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (itemArray[0].equals(propertyValue)) { |
| | | return itemArray[1]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * 反向解析值 男=0,女=1,未知=2 |
| | | * |
| | | * @param propertyValue 参数值 |
| | | * @param converterExp 翻译注解 |
| | | * @param separator 分隔符 |
| | | * @return 解析后值 |
| | | */ |
| | | public static String reverseByExp(String propertyValue, String converterExp, String separator) { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(StringUtils.SEPARATOR); |
| | | for (String item : convertSource) { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(propertyValue, separator)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[1].equals(value)) { |
| | | propertyString.append(itemArray[0] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (itemArray[1].equals(propertyValue)) { |
| | | return itemArray[0]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * 编码文件名 |
| | | */ |
| | | public static String encodingFilename(String filename) { |
| | | return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-idempotent</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-idempotent 幂等功能 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-crypto</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-core</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.idempotent.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 自定义注解防止表单重复提交 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Inherited |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface RepeatSubmit { |
| | | |
| | | /** |
| | | * 间隔时间(ms),小于此时间视为重复提交 |
| | | */ |
| | | int interval() default 5000; |
| | | |
| | | TimeUnit timeUnit() default TimeUnit.MILLISECONDS; |
| | | |
| | | /** |
| | | * 提示消息 支持国际化 格式为 {code} |
| | | */ |
| | | String message() default "{repeat.submit.message}"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.idempotent.aspectj; |
| | | |
| | | import cn.dev33.satoken.SaManager; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.MessageUtils; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.idempotent.annotation.RepeatSubmit; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.AfterReturning; |
| | | import org.aspectj.lang.annotation.AfterThrowing; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Before; |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | | import java.util.Map; |
| | | import java.util.StringJoiner; |
| | | |
| | | /** |
| | | * 防止重复提交(参考美团GTIS防重系统) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Aspect |
| | | public class RepeatSubmitAspect { |
| | | |
| | | private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>(); |
| | | |
| | | @Before("@annotation(repeatSubmit)") |
| | | public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable { |
| | | // 如果注解不为0 则使用注解数值 |
| | | long interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval()); |
| | | |
| | | if (interval < 1000) { |
| | | throw new ServiceException("重复提交间隔时间不能小于'1'秒"); |
| | | } |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | String nowParams = argsArrayToString(point.getArgs()); |
| | | |
| | | // 请求地址(作为存放cache的key值) |
| | | String url = request.getRequestURI(); |
| | | |
| | | // 唯一值(没有消息头则使用请求地址) |
| | | String submitKey = StringUtils.trimToEmpty(request.getHeader(SaManager.getConfig().getTokenName())); |
| | | |
| | | submitKey = SecureUtil.md5(submitKey + ":" + nowParams); |
| | | // 唯一标识(指定key + url + 消息头) |
| | | String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey; |
| | | if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) { |
| | | KEY_CACHE.set(cacheRepeatKey); |
| | | } else { |
| | | String message = repeatSubmit.message(); |
| | | if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) { |
| | | message = MessageUtils.message(StringUtils.substring(message, 1, message.length() - 1)); |
| | | } |
| | | throw new ServiceException(message); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理完请求后执行 |
| | | * |
| | | * @param joinPoint 切点 |
| | | */ |
| | | @AfterReturning(pointcut = "@annotation(repeatSubmit)", returning = "jsonResult") |
| | | public void doAfterReturning(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Object jsonResult) { |
| | | if (jsonResult instanceof R<?> r) { |
| | | try { |
| | | // 成功则不删除redis数据 保证在有效时间内无法重复提交 |
| | | if (r.getCode() == R.SUCCESS) { |
| | | return; |
| | | } |
| | | RedisUtils.deleteObject(KEY_CACHE.get()); |
| | | } finally { |
| | | KEY_CACHE.remove(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 拦截异常操作 |
| | | * |
| | | * @param joinPoint 切点 |
| | | * @param e 异常 |
| | | */ |
| | | @AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e") |
| | | public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) { |
| | | RedisUtils.deleteObject(KEY_CACHE.get()); |
| | | KEY_CACHE.remove(); |
| | | } |
| | | |
| | | /** |
| | | * 参数拼装 |
| | | */ |
| | | private String argsArrayToString(Object[] paramsArray) { |
| | | StringJoiner params = new StringJoiner(" "); |
| | | if (ArrayUtil.isEmpty(paramsArray)) { |
| | | return params.toString(); |
| | | } |
| | | for (Object o : paramsArray) { |
| | | if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { |
| | | params.add(JsonUtils.toJsonString(o)); |
| | | } |
| | | } |
| | | return params.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否需要过滤的对象。 |
| | | * |
| | | * @param o 对象信息。 |
| | | * @return 如果是需要过滤的对象,则返回true;否则返回false。 |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | public boolean isFilterObject(final Object o) { |
| | | Class<?> clazz = o.getClass(); |
| | | if (clazz.isArray()) { |
| | | return clazz.getComponentType().isAssignableFrom(MultipartFile.class); |
| | | } else if (Collection.class.isAssignableFrom(clazz)) { |
| | | Collection collection = (Collection) o; |
| | | for (Object value : collection) { |
| | | return value instanceof MultipartFile; |
| | | } |
| | | } else if (Map.class.isAssignableFrom(clazz)) { |
| | | Map map = (Map) o; |
| | | for (Object value : map.values()) { |
| | | return value instanceof MultipartFile; |
| | | } |
| | | } |
| | | return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse |
| | | || o instanceof BindingResult; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.idempotent.config; |
| | | |
| | | import org.dromara.common.idempotent.aspectj.RepeatSubmitAspect; |
| | | import org.dromara.common.redis.config.RedisConfiguration; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * 幂等功能配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration(after = RedisConfiguration.class) |
| | | public class IdempotentAutoConfiguration { |
| | | |
| | | @Bean |
| | | public RepeatSubmitAspect repeatSubmitAspect() { |
| | | return new RepeatSubmitAspect(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.idempotent.config.IdempotentAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-job</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-job 定时任务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-autoconfigure</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- 服务发现组件 --> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-commons</artifactId> |
| | | </dependency> |
| | | |
| | | <!--PowerJob--> |
| | | <dependency> |
| | | <groupId>tech.powerjob</groupId> |
| | | <artifactId>powerjob-worker</artifactId> |
| | | <exclusions> |
| | | <exclusion> |
| | | <artifactId>powerjob-remote-impl-akka</artifactId> |
| | | <groupId>tech.powerjob</groupId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>tech.powerjob</groupId> |
| | | <artifactId>powerjob-official-processors</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.job.config; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.job.config.properties.PowerJobProperties; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.cloud.client.ServiceInstance; |
| | | import org.springframework.cloud.client.discovery.DiscoveryClient; |
| | | import org.springframework.context.annotation.Bean; |
| | | import tech.powerjob.common.utils.CommonUtils; |
| | | import tech.powerjob.common.utils.NetUtils; |
| | | import tech.powerjob.worker.PowerJobSpringWorker; |
| | | import tech.powerjob.worker.common.PowerJobWorkerConfig; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Autoconfiguration class for PowerJob-worker. |
| | | * |
| | | * @author songyinyin |
| | | * @since 2020/7/26 16:37 |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(PowerJobProperties.class) |
| | | @ConditionalOnProperty(prefix = "powerjob.worker", name = "enabled", havingValue = "true", matchIfMissing = true) |
| | | public class PowerJobConfig{ |
| | | |
| | | @Bean |
| | | public PowerJobSpringWorker initPowerJob(PowerJobProperties properties, DiscoveryClient discoveryClient) { |
| | | |
| | | PowerJobProperties.Worker worker = properties.getWorker(); |
| | | |
| | | /* |
| | | * Address of PowerJob-server node(s). Do not mistake for ActorSystem port. Do not add |
| | | * any prefix, i.e. http://. |
| | | */ |
| | | List<String> serverAddress; |
| | | if (StringUtils.isNotBlank(worker.getServerName())) { |
| | | List<ServiceInstance> instances = discoveryClient.getInstances(worker.getServerName()); |
| | | if (CollUtil.isEmpty(instances)) { |
| | | throw new RuntimeException("调度中心不存在!"); |
| | | } |
| | | serverAddress = StreamUtils.toList(instances, instance -> |
| | | String.format("%s:%s", instance.getHost(), instance.getPort())); |
| | | } else { |
| | | CommonUtils.requireNonNull(worker.getServerAddress(), "serverAddress can't be empty! " + |
| | | "if you don't want to enable powerjob, please config program arguments: powerjob.worker.enabled=false"); |
| | | serverAddress = Arrays.asList(worker.getServerAddress().split(",")); |
| | | } |
| | | /* |
| | | * Create OhMyConfig object for setting properties. |
| | | */ |
| | | PowerJobWorkerConfig config = new PowerJobWorkerConfig(); |
| | | /* |
| | | * Configuration of worker port. Random port is enabled when port is set with non-positive number. |
| | | */ |
| | | if (worker.getPort() != null) { |
| | | config.setPort(worker.getPort()); |
| | | } else { |
| | | int port = worker.getAkkaPort(); |
| | | if (port <= 0) { |
| | | port = NetUtils.getRandomPort(); |
| | | } |
| | | config.setPort(port); |
| | | } |
| | | /* |
| | | * appName, name of the application. Applications should be registered in advance to prevent |
| | | * error. This property should be the same with what you entered for appName when getting |
| | | * registered. |
| | | */ |
| | | config.setAppName(worker.getAppName()); |
| | | config.setServerAddress(serverAddress); |
| | | config.setProtocol(worker.getProtocol()); |
| | | /* |
| | | * For non-Map/MapReduce tasks, {@code memory} is recommended for speeding up calculation. |
| | | * Map/MapReduce tasks may produce batches of subtasks, which could lead to OutOfMemory |
| | | * exception or error, {@code disk} should be applied. |
| | | */ |
| | | config.setStoreStrategy(worker.getStoreStrategy()); |
| | | /* |
| | | * When enabledTestMode is set as true, PowerJob-worker no longer connects to PowerJob-server |
| | | * or validate appName. |
| | | */ |
| | | config.setAllowLazyConnectServer(worker.isAllowLazyConnectServer()); |
| | | /* |
| | | * Max length of appended workflow context . Appended workflow context value that is longer than the value will be ignored. |
| | | */ |
| | | config.setMaxAppendedWfContextLength(worker.getMaxAppendedWfContextLength()); |
| | | |
| | | config.setTag(worker.getTag()); |
| | | |
| | | config.setMaxHeavyweightTaskNum(worker.getMaxHeavyweightTaskNum()); |
| | | |
| | | config.setMaxLightweightTaskNum(worker.getMaxLightweightTaskNum()); |
| | | |
| | | config.setHealthReportInterval(worker.getHealthReportInterval()); |
| | | /* |
| | | * Create PowerJobSpringWorker object and set properties. |
| | | */ |
| | | return new PowerJobSpringWorker(config); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.job.config.properties; |
| | | |
| | | import lombok.Getter; |
| | | import lombok.Setter; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import tech.powerjob.common.RemoteConstant; |
| | | import tech.powerjob.common.enums.Protocol; |
| | | import tech.powerjob.worker.common.constants.StoreStrategy; |
| | | import tech.powerjob.worker.core.processor.ProcessResult; |
| | | import tech.powerjob.worker.core.processor.WorkflowContext; |
| | | |
| | | /** |
| | | * PowerJob properties configuration class. |
| | | * |
| | | * @author songyinyin |
| | | * @since 2020/7/26 16:37 |
| | | */ |
| | | @ConfigurationProperties(prefix = "powerjob") |
| | | public class PowerJobProperties { |
| | | |
| | | private final Worker worker = new Worker(); |
| | | |
| | | public Worker getWorker() { |
| | | return worker; |
| | | } |
| | | |
| | | /** |
| | | * Powerjob worker configuration properties. |
| | | */ |
| | | @Setter |
| | | @Getter |
| | | public static class Worker { |
| | | |
| | | /** |
| | | * Whether to enable PowerJob Worker |
| | | */ |
| | | private boolean enabled = true; |
| | | |
| | | /** |
| | | * Name of application, String type. Total length of this property should be no more than 255 |
| | | * characters. This is one of the required properties when registering a new application. This |
| | | * property should be assigned with the same value as what you entered for the appName. |
| | | */ |
| | | private String appName; |
| | | /** |
| | | * Akka port of Powerjob-worker, optional value. Default value of this property is 27777. |
| | | * If multiple PowerJob-worker nodes were deployed, different, unique ports should be assigned. |
| | | * Deprecated, please use 'port' |
| | | */ |
| | | @Deprecated |
| | | private int akkaPort = RemoteConstant.DEFAULT_WORKER_PORT; |
| | | /** |
| | | * port |
| | | */ |
| | | private Integer port; |
| | | /** |
| | | * Address(es) of Powerjob-server node(s). Ip:port or domain. |
| | | * Example of single Powerjob-server node: |
| | | * <p> |
| | | * 127.0.0.1:7700 |
| | | * </p> |
| | | * Example of Powerjob-server cluster: |
| | | * <p> |
| | | * 192.168.0.10:7700,192.168.0.11:7700,192.168.0.12:7700 |
| | | * </p> |
| | | */ |
| | | private String serverAddress; |
| | | |
| | | private String serverName; |
| | | /** |
| | | * Protocol for communication between WORKER and server |
| | | */ |
| | | private Protocol protocol = Protocol.AKKA; |
| | | /** |
| | | * Local store strategy for H2 database. {@code disk} or {@code memory}. |
| | | */ |
| | | private StoreStrategy storeStrategy = StoreStrategy.DISK; |
| | | /** |
| | | * Max length of response result. Result that is longer than the value will be truncated. |
| | | * {@link ProcessResult} max length for #msg |
| | | */ |
| | | private int maxResultLength = 8192; |
| | | /** |
| | | * If allowLazyConnectServer is set as true, PowerJob worker allows launching without a direct connection to the server. |
| | | * allowLazyConnectServer is used for conditions that your have no powerjob-server in your develop env so you can't startup the application |
| | | */ |
| | | private boolean allowLazyConnectServer = false; |
| | | /** |
| | | * Max length of appended workflow context value length. Appended workflow context value that is longer than the value will be ignored. |
| | | * {@link WorkflowContext} max length for #appendedContextData |
| | | */ |
| | | private int maxAppendedWfContextLength = 8192; |
| | | |
| | | private String tag; |
| | | /** |
| | | * Max numbers of LightTaskTacker |
| | | */ |
| | | private Integer maxLightweightTaskNum = 1024; |
| | | /** |
| | | * Max numbers of HeavyTaskTacker |
| | | */ |
| | | private Integer maxHeavyweightTaskNum = 64; |
| | | /** |
| | | * Interval(s) of worker health report |
| | | */ |
| | | private Integer healthReportInterval = 10; |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.job.config.PowerJobConfig |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-json 序列化模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- JSON工具类 --> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-databind</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.datatype</groupId> |
| | | <artifactId>jackson-datatype-jsr310</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.json.config; |
| | | |
| | | import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| | | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
| | | import org.dromara.common.json.handler.BigNumberSerializer; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; |
| | | import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.BigInteger; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.TimeZone; |
| | | |
| | | /** |
| | | * jackson 配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @AutoConfiguration(before = JacksonAutoConfiguration.class) |
| | | public class JacksonConfig { |
| | | |
| | | @Bean |
| | | public Jackson2ObjectMapperBuilderCustomizer customizer() { |
| | | return builder -> { |
| | | // 全局配置序列化返回 JSON 处理 |
| | | JavaTimeModule javaTimeModule = new JavaTimeModule(); |
| | | javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE); |
| | | javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE); |
| | | javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE); |
| | | javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); |
| | | javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); |
| | | builder.modules(javaTimeModule); |
| | | builder.timeZone(TimeZone.getDefault()); |
| | | log.info("初始化 jackson 配置"); |
| | | }; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.json.handler; |
| | | |
| | | import com.fasterxml.jackson.core.JsonGenerator; |
| | | import com.fasterxml.jackson.databind.SerializerProvider; |
| | | import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; |
| | | import com.fasterxml.jackson.databind.ser.std.NumberSerializer; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * 超出 JS 最大最小值 处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @JacksonStdImpl |
| | | public class BigNumberSerializer extends NumberSerializer { |
| | | |
| | | /** |
| | | * 根据 JS Number.MAX_SAFE_INTEGER 与 Number.MIN_SAFE_INTEGER 得来 |
| | | */ |
| | | private static final long MAX_SAFE_INTEGER = 9007199254740991L; |
| | | private static final long MIN_SAFE_INTEGER = -9007199254740991L; |
| | | |
| | | /** |
| | | * 提供实例 |
| | | */ |
| | | public static final BigNumberSerializer INSTANCE = new BigNumberSerializer(Number.class); |
| | | |
| | | public BigNumberSerializer(Class<? extends Number> rawType) { |
| | | super(rawType); |
| | | } |
| | | |
| | | @Override |
| | | public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException { |
| | | // 超出范围 序列化位字符串 |
| | | if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) { |
| | | super.serialize(value, gen, provider); |
| | | } else { |
| | | gen.writeString(value.toString()); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.json.utils; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * JSON 工具类 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class JsonUtils { |
| | | |
| | | private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); |
| | | |
| | | public static ObjectMapper getObjectMapper() { |
| | | return OBJECT_MAPPER; |
| | | } |
| | | |
| | | public static String toJsonString(Object object) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.writeValueAsString(object); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static <T> T parseObject(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
| | | if (ArrayUtil.isEmpty(bytes)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(bytes, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, typeReference); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static Dict parseMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); |
| | | } catch (MismatchedInputException e) { |
| | | // 类型不匹配说明不是json |
| | | return null; |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static List<Dict> parseArrayMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static <T> List<T> parseArray(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.json.config.JacksonConfig |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-loadbalancer</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-loadbalancer 自定义负载均衡(多团队开发使用) |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-loadbalancer</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-starter</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.loadbalance.config; |
| | | |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.env.EnvironmentPostProcessor; |
| | | import org.springframework.core.Ordered; |
| | | import org.springframework.core.env.ConfigurableEnvironment; |
| | | |
| | | /** |
| | | * dubbo自定义负载均衡配置注入 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { |
| | | |
| | | @Override |
| | | public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| | | System.setProperty("dubbo.consumer.loadbalance", "customDubboLoadBalancer"); |
| | | } |
| | | |
| | | @Override |
| | | public int getOrder() { |
| | | return Ordered.HIGHEST_PRECEDENCE; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.loadbalance.config; |
| | | |
| | | import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; |
| | | |
| | | /** |
| | | * 自定义负载均衡自动配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @LoadBalancerClients(defaultConfiguration = CustomLoadBalanceClientConfiguration.class) |
| | | public class CustomLoadBalanceAutoConfiguration { |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.loadbalance.config; |
| | | |
| | | import org.dromara.common.loadbalance.core.CustomSpringCloudLoadBalancer; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| | | import org.springframework.cloud.client.ServiceInstance; |
| | | import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; |
| | | import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; |
| | | import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.core.env.Environment; |
| | | |
| | | /** |
| | | * 自定义负载均衡客户端配置 |
| | | * |
| | | * @author LionLi |
| | | */ |
| | | @SuppressWarnings("all") |
| | | @Configuration(proxyBeanMethods = false) |
| | | public class CustomLoadBalanceClientConfiguration { |
| | | |
| | | @Bean |
| | | @ConditionalOnBean(LoadBalancerClientFactory.class) |
| | | public ReactorLoadBalancer<ServiceInstance> customLoadBalancer(Environment environment, |
| | | LoadBalancerClientFactory loadBalancerClientFactory) { |
| | | String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); |
| | | return new CustomSpringCloudLoadBalancer(name, |
| | | loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.loadbalance.core; |
| | | |
| | | import cn.hutool.core.net.NetUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.dubbo.common.URL; |
| | | import org.apache.dubbo.rpc.Invocation; |
| | | import org.apache.dubbo.rpc.Invoker; |
| | | import org.apache.dubbo.rpc.cluster.loadbalance.AbstractLoadBalance; |
| | | |
| | | import java.util.List; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | |
| | | /** |
| | | * 自定义 Dubbo 负载均衡算法 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class CustomDubboLoadBalancer extends AbstractLoadBalance { |
| | | |
| | | @Override |
| | | protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { |
| | | for (Invoker<T> invoker : invokers) { |
| | | if (NetUtil.localIpv4s().contains(invoker.getUrl().getHost())) { |
| | | return invoker; |
| | | } |
| | | } |
| | | return invokers.get(ThreadLocalRandom.current().nextInt(invokers.size())); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.loadbalance.core; |
| | | |
| | | import cn.hutool.core.net.NetUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.ObjectProvider; |
| | | import org.springframework.cloud.client.ServiceInstance; |
| | | import org.springframework.cloud.client.loadbalancer.DefaultResponse; |
| | | import org.springframework.cloud.client.loadbalancer.EmptyResponse; |
| | | import org.springframework.cloud.client.loadbalancer.Request; |
| | | import org.springframework.cloud.client.loadbalancer.Response; |
| | | import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier; |
| | | import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; |
| | | import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback; |
| | | import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import java.util.List; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | |
| | | /** |
| | | * 自定义 SpringCloud 负载均衡算法 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | public class CustomSpringCloudLoadBalancer implements ReactorServiceInstanceLoadBalancer { |
| | | |
| | | private final String serviceId; |
| | | |
| | | private final ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider; |
| | | |
| | | @Override |
| | | public Mono<Response<ServiceInstance>> choose(Request request) { |
| | | ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new); |
| | | return supplier.get(request).next().map(serviceInstances -> processInstanceResponse(supplier, serviceInstances)); |
| | | } |
| | | |
| | | private Response<ServiceInstance> processInstanceResponse(ServiceInstanceListSupplier supplier, |
| | | List<ServiceInstance> serviceInstances) { |
| | | Response<ServiceInstance> serviceInstanceResponse = getInstanceResponse(serviceInstances); |
| | | if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) { |
| | | ((SelectedInstanceCallback) supplier).selectedServiceInstance(serviceInstanceResponse.getServer()); |
| | | } |
| | | return serviceInstanceResponse; |
| | | } |
| | | |
| | | private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances) { |
| | | if (instances.isEmpty()) { |
| | | if (log.isWarnEnabled()) { |
| | | log.warn("No servers available for service: " + serviceId); |
| | | } |
| | | return new EmptyResponse(); |
| | | } |
| | | for (ServiceInstance instance : instances) { |
| | | if (NetUtil.localIpv4s().contains(instance.getHost())) { |
| | | return new DefaultResponse(instance); |
| | | } |
| | | } |
| | | return new DefaultResponse(instances.get(ThreadLocalRandom.current().nextInt(instances.size()))); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | customDubboLoadBalancer=org.dromara.common.loadbalance.core.CustomDubboLoadBalancer |
New file |
| | |
| | | org.springframework.boot.env.EnvironmentPostProcessor=\ |
| | | org.dromara.common.loadbalance.config.CustomEnvironmentPostProcessor |
New file |
| | |
| | | org.dromara.common.loadbalance.config.CustomLoadBalanceAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-log</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-log 日志记录 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>transmittable-thread-local</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo</groupId> |
| | | <artifactId>dubbo-spring-boot-starter</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.log.annotation; |
| | | |
| | | import org.dromara.common.log.enums.BusinessType; |
| | | import org.dromara.common.log.enums.OperatorType; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 自定义操作日志记录注解 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Target({ElementType.PARAMETER, ElementType.METHOD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface Log { |
| | | /** |
| | | * 模块 |
| | | */ |
| | | String title() default ""; |
| | | |
| | | /** |
| | | * 功能 |
| | | */ |
| | | BusinessType businessType() default BusinessType.OTHER; |
| | | |
| | | /** |
| | | * 操作人类别 |
| | | */ |
| | | OperatorType operatorType() default OperatorType.MANAGE; |
| | | |
| | | /** |
| | | * 是否保存请求的参数 |
| | | */ |
| | | boolean isSaveRequestData() default true; |
| | | |
| | | /** |
| | | * 是否保存响应的参数 |
| | | */ |
| | | boolean isSaveResponseData() default true; |
| | | |
| | | |
| | | /** |
| | | * 排除指定的请求参数 |
| | | */ |
| | | String[] excludeParamNames() default {}; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.aspect; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.ttl.TransmittableThreadLocal; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.time.StopWatch; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.AfterReturning; |
| | | import org.aspectj.lang.annotation.AfterThrowing; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Before; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import org.dromara.common.log.annotation.Log; |
| | | import org.dromara.common.log.enums.BusinessStatus; |
| | | import org.dromara.common.log.event.OperLogEvent; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Map; |
| | | import java.util.StringJoiner; |
| | | |
| | | /** |
| | | * 操作日志记录处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @Aspect |
| | | @AutoConfiguration |
| | | public class LogAspect { |
| | | |
| | | /** |
| | | * 排除敏感属性字段 |
| | | */ |
| | | public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" }; |
| | | |
| | | |
| | | /** |
| | | * 计算操作消耗时间 |
| | | */ |
| | | private static final ThreadLocal<StopWatch> TIME_THREADLOCAL = new TransmittableThreadLocal<>(); |
| | | |
| | | /** |
| | | * 处理请求前执行 |
| | | */ |
| | | @Before(value = "@annotation(controllerLog)") |
| | | public void boBefore(JoinPoint joinPoint, Log controllerLog) { |
| | | StopWatch stopWatch = new StopWatch(); |
| | | TIME_THREADLOCAL.set(stopWatch); |
| | | stopWatch.start(); |
| | | } |
| | | |
| | | /** |
| | | * 处理完请求后执行 |
| | | * |
| | | * @param joinPoint 切点 |
| | | */ |
| | | @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult") |
| | | public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult) { |
| | | handleLog(joinPoint, controllerLog, null, jsonResult); |
| | | } |
| | | |
| | | /** |
| | | * 拦截异常操作 |
| | | * |
| | | * @param joinPoint 切点 |
| | | * @param e 异常 |
| | | */ |
| | | @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e") |
| | | public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e) { |
| | | handleLog(joinPoint, controllerLog, e, null); |
| | | } |
| | | |
| | | protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) { |
| | | try { |
| | | |
| | | // *========数据库日志=========*// |
| | | OperLogEvent operLog = new OperLogEvent(); |
| | | operLog.setTenantId(LoginHelper.getTenantId()); |
| | | operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); |
| | | // 请求的地址 |
| | | String ip = ServletUtils.getClientIP(); |
| | | operLog.setOperIp(ip); |
| | | operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255)); |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | operLog.setOperName(loginUser.getUsername()); |
| | | operLog.setDeptName(loginUser.getDeptName()); |
| | | |
| | | if (e != null) { |
| | | operLog.setStatus(BusinessStatus.FAIL.ordinal()); |
| | | operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); |
| | | } |
| | | // 设置方法名称 |
| | | String className = joinPoint.getTarget().getClass().getName(); |
| | | String methodName = joinPoint.getSignature().getName(); |
| | | operLog.setMethod(className + "." + methodName + "()"); |
| | | // 设置请求方式 |
| | | operLog.setRequestMethod(ServletUtils.getRequest().getMethod()); |
| | | // 处理设置注解上的参数 |
| | | getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult); |
| | | // 设置消耗时间 |
| | | StopWatch stopWatch = TIME_THREADLOCAL.get(); |
| | | stopWatch.stop(); |
| | | operLog.setCostTime(stopWatch.getTime()); |
| | | // 发布事件保存数据库 |
| | | SpringUtils.context().publishEvent(operLog); |
| | | } catch (Exception exp) { |
| | | // 记录本地异常日志 |
| | | log.error("异常信息:{}", exp.getMessage()); |
| | | exp.printStackTrace(); |
| | | } finally { |
| | | TIME_THREADLOCAL.remove(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取注解中对方法的描述信息 用于Controller层注解 |
| | | * |
| | | * @param log 日志 |
| | | * @param operLog 操作日志 |
| | | * @throws Exception |
| | | */ |
| | | public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogEvent operLog, Object jsonResult) throws Exception { |
| | | // 设置action动作 |
| | | operLog.setBusinessType(log.businessType().ordinal()); |
| | | // 设置标题 |
| | | operLog.setTitle(log.title()); |
| | | // 设置操作人类别 |
| | | operLog.setOperatorType(log.operatorType().ordinal()); |
| | | // 是否需要保存request,参数和值 |
| | | if (log.isSaveRequestData()) { |
| | | // 获取参数的信息,传入到数据库中。 |
| | | setRequestValue(joinPoint, operLog, log.excludeParamNames()); |
| | | } |
| | | // 是否需要保存response,参数和值 |
| | | if (log.isSaveResponseData() && ObjectUtil.isNotNull(jsonResult)) { |
| | | operLog.setJsonResult(StringUtils.substring(JsonUtils.toJsonString(jsonResult), 0, 2000)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取请求的参数,放到log中 |
| | | * |
| | | * @param operLog 操作日志 |
| | | * @throws Exception 异常 |
| | | */ |
| | | private void setRequestValue(JoinPoint joinPoint, OperLogEvent operLog, String[] excludeParamNames) throws Exception { |
| | | Map<String, String> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest()); |
| | | String requestMethod = operLog.getRequestMethod(); |
| | | if (MapUtil.isEmpty(paramsMap) |
| | | && HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) { |
| | | String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames); |
| | | operLog.setOperParam(StringUtils.substring(params, 0, 2000)); |
| | | } else { |
| | | MapUtil.removeAny(paramsMap, EXCLUDE_PROPERTIES); |
| | | MapUtil.removeAny(paramsMap, excludeParamNames); |
| | | operLog.setOperParam(StringUtils.substring(JsonUtils.toJsonString(paramsMap), 0, 2000)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 参数拼装 |
| | | */ |
| | | private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames) { |
| | | StringJoiner params = new StringJoiner(" "); |
| | | if (ArrayUtil.isEmpty(paramsArray)) { |
| | | return params.toString(); |
| | | } |
| | | for (Object o : paramsArray) { |
| | | if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { |
| | | String str = JsonUtils.toJsonString(o); |
| | | Dict dict = JsonUtils.parseMap(str); |
| | | if (MapUtil.isNotEmpty(dict)) { |
| | | MapUtil.removeAny(dict, EXCLUDE_PROPERTIES); |
| | | MapUtil.removeAny(dict, excludeParamNames); |
| | | str = JsonUtils.toJsonString(dict); |
| | | } |
| | | params.add(str); |
| | | } |
| | | } |
| | | return params.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否需要过滤的对象。 |
| | | * |
| | | * @param o 对象信息。 |
| | | * @return 如果是需要过滤的对象,则返回true;否则返回false。 |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | public boolean isFilterObject(final Object o) { |
| | | Class<?> clazz = o.getClass(); |
| | | if (clazz.isArray()) { |
| | | return clazz.getComponentType().isAssignableFrom(MultipartFile.class); |
| | | } else if (Collection.class.isAssignableFrom(clazz)) { |
| | | Collection collection = (Collection) o; |
| | | for (Object value : collection) { |
| | | return value instanceof MultipartFile; |
| | | } |
| | | } else if (Map.class.isAssignableFrom(clazz)) { |
| | | Map map = (Map) o; |
| | | for (Object value : map.values()) { |
| | | return value instanceof MultipartFile; |
| | | } |
| | | } |
| | | return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse |
| | | || o instanceof BindingResult; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.enums; |
| | | |
| | | /** |
| | | * 操作状态 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public enum BusinessStatus { |
| | | /** |
| | | * 成功 |
| | | */ |
| | | SUCCESS, |
| | | |
| | | /** |
| | | * 失败 |
| | | */ |
| | | FAIL, |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.enums; |
| | | |
| | | /** |
| | | * 业务操作类型 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public enum BusinessType { |
| | | /** |
| | | * 其它 |
| | | */ |
| | | OTHER, |
| | | |
| | | /** |
| | | * 新增 |
| | | */ |
| | | INSERT, |
| | | |
| | | /** |
| | | * 修改 |
| | | */ |
| | | UPDATE, |
| | | |
| | | /** |
| | | * 删除 |
| | | */ |
| | | DELETE, |
| | | |
| | | /** |
| | | * 授权 |
| | | */ |
| | | GRANT, |
| | | |
| | | /** |
| | | * 导出 |
| | | */ |
| | | EXPORT, |
| | | |
| | | /** |
| | | * 导入 |
| | | */ |
| | | IMPORT, |
| | | |
| | | /** |
| | | * 强退 |
| | | */ |
| | | FORCE, |
| | | |
| | | /** |
| | | * 生成代码 |
| | | */ |
| | | GENCODE, |
| | | |
| | | /** |
| | | * 清空数据 |
| | | */ |
| | | CLEAN, |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.enums; |
| | | |
| | | /** |
| | | * 操作人类别 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public enum OperatorType { |
| | | /** |
| | | * 其它 |
| | | */ |
| | | OTHER, |
| | | |
| | | /** |
| | | * 后台用户 |
| | | */ |
| | | MANAGE, |
| | | |
| | | /** |
| | | * 手机端用户 |
| | | */ |
| | | MOBILE |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.event; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.useragent.UserAgent; |
| | | import cn.hutool.http.useragent.UserAgentUtil; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | import org.dromara.common.core.constant.Constants; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.ip.AddressUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.RemoteClientService; |
| | | import org.dromara.system.api.RemoteLogService; |
| | | import org.dromara.system.api.domain.bo.RemoteLogininforBo; |
| | | import org.dromara.system.api.domain.bo.RemoteOperLogBo; |
| | | import org.dromara.system.api.domain.vo.RemoteClientVo; |
| | | import org.springframework.context.event.EventListener; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * 异步调用日志服务 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | @Slf4j |
| | | public class LogEventListener { |
| | | |
| | | @DubboReference |
| | | private RemoteLogService remoteLogService; |
| | | @DubboReference |
| | | private RemoteClientService remoteClientService; |
| | | |
| | | /** |
| | | * 保存系统日志记录 |
| | | */ |
| | | @Async |
| | | @EventListener |
| | | public void saveLog(OperLogEvent operLogEvent) { |
| | | RemoteOperLogBo sysOperLog = BeanUtil.toBean(operLogEvent, RemoteOperLogBo.class); |
| | | remoteLogService.saveLog(sysOperLog); |
| | | } |
| | | |
| | | @Async |
| | | @EventListener |
| | | public void saveLogininfor(LogininforEvent logininforEvent) { |
| | | HttpServletRequest request = logininforEvent.getRequest(); |
| | | final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent")); |
| | | final String ip = ServletUtils.getClientIP(request); |
| | | // 客户端信息 |
| | | String clientid = request.getHeader(LoginHelper.CLIENT_KEY); |
| | | RemoteClientVo clientVo = null; |
| | | if (StringUtils.isNotBlank(clientid)) { |
| | | clientVo = remoteClientService.queryByClientId(clientid); |
| | | } |
| | | |
| | | String address = AddressUtils.getRealAddressByIP(ip); |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(getBlock(ip)); |
| | | s.append(address); |
| | | s.append(getBlock(logininforEvent.getUsername())); |
| | | s.append(getBlock(logininforEvent.getStatus())); |
| | | s.append(getBlock(logininforEvent.getMessage())); |
| | | // 打印信息到日志 |
| | | log.info(s.toString(), logininforEvent.getArgs()); |
| | | // 获取客户端操作系统 |
| | | String os = userAgent.getOs().getName(); |
| | | // 获取客户端浏览器 |
| | | String browser = userAgent.getBrowser().getName(); |
| | | // 封装对象 |
| | | RemoteLogininforBo logininfor = new RemoteLogininforBo(); |
| | | logininfor.setTenantId(logininforEvent.getTenantId()); |
| | | logininfor.setUserName(logininforEvent.getUsername()); |
| | | if (ObjectUtil.isNotNull(clientVo)) { |
| | | logininfor.setClientKey(clientVo.getClientKey()); |
| | | logininfor.setDeviceType(clientVo.getDeviceType()); |
| | | } |
| | | logininfor.setIpaddr(ip); |
| | | logininfor.setLoginLocation(address); |
| | | logininfor.setBrowser(browser); |
| | | logininfor.setOs(os); |
| | | logininfor.setMsg(logininforEvent.getMessage()); |
| | | // 日志状态 |
| | | if (StringUtils.equalsAny(logininforEvent.getStatus(), Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) { |
| | | logininfor.setStatus(Constants.SUCCESS); |
| | | } else if (Constants.LOGIN_FAIL.equals(logininforEvent.getStatus())) { |
| | | logininfor.setStatus(Constants.FAIL); |
| | | } |
| | | remoteLogService.saveLogininfor(logininfor); |
| | | } |
| | | |
| | | private String getBlock(Object msg) { |
| | | if (msg == null) { |
| | | msg = ""; |
| | | } |
| | | return "[" + msg + "]"; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.event; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * 登录事件 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | |
| | | @Data |
| | | public class LogininforEvent implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 租户ID |
| | | */ |
| | | private String tenantId; |
| | | |
| | | /** |
| | | * 用户账号 |
| | | */ |
| | | private String username; |
| | | |
| | | /** |
| | | * 登录状态 0成功 1失败 |
| | | */ |
| | | private String status; |
| | | |
| | | /** |
| | | * 提示消息 |
| | | */ |
| | | private String message; |
| | | |
| | | /** |
| | | * 请求体 |
| | | */ |
| | | private HttpServletRequest request; |
| | | |
| | | /** |
| | | * 其他参数 |
| | | */ |
| | | private Object[] args; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.log.event; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 操作日志事件 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | |
| | | @Data |
| | | public class OperLogEvent implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 日志主键 |
| | | */ |
| | | private Long operId; |
| | | |
| | | /** |
| | | * 租户ID |
| | | */ |
| | | private String tenantId; |
| | | |
| | | /** |
| | | * 操作模块 |
| | | */ |
| | | private String title; |
| | | |
| | | /** |
| | | * 业务类型(0其它 1新增 2修改 3删除) |
| | | */ |
| | | private Integer businessType; |
| | | |
| | | /** |
| | | * 业务类型数组 |
| | | */ |
| | | private Integer[] businessTypes; |
| | | |
| | | /** |
| | | * 请求方法 |
| | | */ |
| | | private String method; |
| | | |
| | | /** |
| | | * 请求方式 |
| | | */ |
| | | private String requestMethod; |
| | | |
| | | /** |
| | | * 操作类别(0其它 1后台用户 2手机端用户) |
| | | */ |
| | | private Integer operatorType; |
| | | |
| | | /** |
| | | * 操作人员 |
| | | */ |
| | | private String operName; |
| | | |
| | | /** |
| | | * 部门名称 |
| | | */ |
| | | private String deptName; |
| | | |
| | | /** |
| | | * 请求url |
| | | */ |
| | | private String operUrl; |
| | | |
| | | /** |
| | | * 操作地址 |
| | | */ |
| | | private String operIp; |
| | | |
| | | /** |
| | | * 操作地点 |
| | | */ |
| | | private String operLocation; |
| | | |
| | | /** |
| | | * 请求参数 |
| | | */ |
| | | private String operParam; |
| | | |
| | | /** |
| | | * 返回参数 |
| | | */ |
| | | private String jsonResult; |
| | | |
| | | /** |
| | | * 操作状态(0正常 1异常) |
| | | */ |
| | | private Integer status; |
| | | |
| | | /** |
| | | * 错误消息 |
| | | */ |
| | | private String errorMsg; |
| | | |
| | | /** |
| | | * 操作时间 |
| | | */ |
| | | private Date operTime; |
| | | |
| | | /** |
| | | * 消耗时间 |
| | | */ |
| | | private Long costTime; |
| | | } |
New file |
| | |
| | | org.dromara.common.log.event.LogEventListener |
| | | org.dromara.common.log.aspect.LogAspect |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-logstash</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-logstash logstash日志推送模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>net.logstash.logback</groupId> |
| | | <artifactId>logstash-logback-encoder</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | |
| | | <included> |
| | | |
| | | <springProperty scope="context" name="appName" source="spring.application.name"/> |
| | | |
| | | <!--输出到logstash的appender--> |
| | | <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> |
| | | <!--可以访问的logstash日志收集端口--> |
| | | <destination>${logstash.address}</destination> |
| | | <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"> |
| | | <customFields>{"spring.application.name":"${appName}"}</customFields> |
| | | </encoder> |
| | | </appender> |
| | | |
| | | <root level="info"> |
| | | <appender-ref ref="logstash"/> |
| | | </root> |
| | | </included> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-mail</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-mail 邮件模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>jakarta.mail</groupId> |
| | | <artifactId>jakarta.mail-api</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.eclipse.angus</groupId> |
| | | <artifactId>jakarta.mail</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.mail.config; |
| | | |
| | | import org.dromara.common.mail.config.properties.MailProperties; |
| | | import org.dromara.common.mail.utils.MailAccount; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * JavaMail 配置 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(MailProperties.class) |
| | | public class MailConfig { |
| | | |
| | | @Bean |
| | | @ConditionalOnProperty(value = "mail.enabled", havingValue = "true") |
| | | public MailAccount mailAccount(MailProperties mailProperties) { |
| | | MailAccount account = new MailAccount(); |
| | | account.setHost(mailProperties.getHost()); |
| | | account.setPort(mailProperties.getPort()); |
| | | account.setAuth(mailProperties.getAuth()); |
| | | account.setFrom(mailProperties.getFrom()); |
| | | account.setUser(mailProperties.getUser()); |
| | | account.setPass(mailProperties.getPass()); |
| | | account.setSocketFactoryPort(mailProperties.getPort()); |
| | | account.setStarttlsEnable(mailProperties.getStarttlsEnable()); |
| | | account.setSslEnable(mailProperties.getSslEnable()); |
| | | account.setTimeout(mailProperties.getTimeout()); |
| | | account.setConnectionTimeout(mailProperties.getConnectionTimeout()); |
| | | return account; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.config; |
| | | |
| | | import org.dromara.common.mail.utils.MailAccount; |
| | | import org.dromara.common.mail.config.properties.MailProperties; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * JavaMail 配置 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(MailProperties.class) |
| | | public class MailConfiguration { |
| | | |
| | | @Bean |
| | | @ConditionalOnProperty(value = "mail.enabled", havingValue = "true") |
| | | public MailAccount mailAccount(MailProperties mailProperties) { |
| | | MailAccount account = new MailAccount(); |
| | | account.setHost(mailProperties.getHost()); |
| | | account.setPort(mailProperties.getPort()); |
| | | account.setAuth(mailProperties.getAuth()); |
| | | account.setFrom(mailProperties.getFrom()); |
| | | account.setUser(mailProperties.getUser()); |
| | | account.setPass(mailProperties.getPass()); |
| | | account.setSocketFactoryPort(mailProperties.getPort()); |
| | | account.setStarttlsEnable(mailProperties.getStarttlsEnable()); |
| | | account.setSslEnable(mailProperties.getSslEnable()); |
| | | account.setTimeout(mailProperties.getTimeout()); |
| | | account.setConnectionTimeout(mailProperties.getConnectionTimeout()); |
| | | return account; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.config.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * JavaMail 配置属性 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "mail") |
| | | public class MailProperties { |
| | | |
| | | /** |
| | | * 过滤开关 |
| | | */ |
| | | private Boolean enabled; |
| | | |
| | | /** |
| | | * SMTP服务器域名 |
| | | */ |
| | | private String host; |
| | | |
| | | /** |
| | | * SMTP服务端口 |
| | | */ |
| | | private Integer port; |
| | | |
| | | /** |
| | | * 是否需要用户名密码验证 |
| | | */ |
| | | private Boolean auth; |
| | | |
| | | /** |
| | | * 用户名 |
| | | */ |
| | | private String user; |
| | | |
| | | /** |
| | | * 密码 |
| | | */ |
| | | private String pass; |
| | | |
| | | /** |
| | | * 发送方,遵循RFC-822标准 |
| | | */ |
| | | private String from; |
| | | |
| | | /** |
| | | * 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。 |
| | | */ |
| | | private Boolean starttlsEnable; |
| | | |
| | | /** |
| | | * 使用 SSL安全连接 |
| | | */ |
| | | private Boolean sslEnable; |
| | | |
| | | /** |
| | | * SMTP超时时长,单位毫秒,缺省值不超时 |
| | | */ |
| | | private Long timeout; |
| | | |
| | | /** |
| | | * Socket连接超时值,单位毫秒,缺省值不超时 |
| | | */ |
| | | private Long connectionTimeout; |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.io.IORuntimeException; |
| | | |
| | | /** |
| | | * 全局邮件帐户,依赖于邮件配置文件{@link MailAccount#MAIL_SETTING_PATHS} |
| | | * |
| | | * @author looly |
| | | */ |
| | | public enum GlobalMailAccount { |
| | | INSTANCE; |
| | | |
| | | private final MailAccount mailAccount; |
| | | |
| | | /** |
| | | * 构造 |
| | | */ |
| | | GlobalMailAccount() { |
| | | mailAccount = createDefaultAccount(); |
| | | } |
| | | |
| | | /** |
| | | * 获得邮件帐户 |
| | | * |
| | | * @return 邮件帐户 |
| | | */ |
| | | public MailAccount getAccount() { |
| | | return this.mailAccount; |
| | | } |
| | | |
| | | /** |
| | | * 创建默认帐户 |
| | | * |
| | | * @return MailAccount |
| | | */ |
| | | private MailAccount createDefaultAccount() { |
| | | for (String mailSettingPath : MailAccount.MAIL_SETTING_PATHS) { |
| | | try { |
| | | return new MailAccount(mailSettingPath); |
| | | } catch (IORuntimeException ignore) { |
| | | //ignore |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import jakarta.mail.internet.AddressException; |
| | | import jakarta.mail.internet.InternetAddress; |
| | | import jakarta.mail.internet.MimeUtility; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.charset.Charset; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 邮件内部工具类 |
| | | * |
| | | * @author looly |
| | | * @since 3.2.3 |
| | | */ |
| | | public class InternalMailUtil { |
| | | |
| | | /** |
| | | * 将多个字符串邮件地址转为{@link InternetAddress}列表<br> |
| | | * 单个字符串地址可以是多个地址合并的字符串 |
| | | * |
| | | * @param addrStrs 地址数组 |
| | | * @param charset 编码(主要用于中文用户名的编码) |
| | | * @return 地址数组 |
| | | * @since 4.0.3 |
| | | */ |
| | | public static InternetAddress[] parseAddressFromStrs(String[] addrStrs, Charset charset) { |
| | | final List<InternetAddress> resultList = new ArrayList<>(addrStrs.length); |
| | | InternetAddress[] addrs; |
| | | for (String addrStr : addrStrs) { |
| | | addrs = parseAddress(addrStr, charset); |
| | | if (ArrayUtil.isNotEmpty(addrs)) { |
| | | Collections.addAll(resultList, addrs); |
| | | } |
| | | } |
| | | return resultList.toArray(new InternetAddress[0]); |
| | | } |
| | | |
| | | /** |
| | | * 解析第一个地址 |
| | | * |
| | | * @param address 地址字符串 |
| | | * @param charset 编码,{@code null}表示使用系统属性定义的编码或系统编码 |
| | | * @return 地址列表 |
| | | */ |
| | | public static InternetAddress parseFirstAddress(String address, Charset charset) { |
| | | final InternetAddress[] internetAddresses = parseAddress(address, charset); |
| | | if (ArrayUtil.isEmpty(internetAddresses)) { |
| | | try { |
| | | return new InternetAddress(address); |
| | | } catch (AddressException e) { |
| | | throw new MailException(e); |
| | | } |
| | | } |
| | | return internetAddresses[0]; |
| | | } |
| | | |
| | | /** |
| | | * 将一个地址字符串解析为多个地址<br> |
| | | * 地址间使用" "、","、";"分隔 |
| | | * |
| | | * @param address 地址字符串 |
| | | * @param charset 编码,{@code null}表示使用系统属性定义的编码或系统编码 |
| | | * @return 地址列表 |
| | | */ |
| | | public static InternetAddress[] parseAddress(String address, Charset charset) { |
| | | InternetAddress[] addresses; |
| | | try { |
| | | addresses = InternetAddress.parse(address); |
| | | } catch (AddressException e) { |
| | | throw new MailException(e); |
| | | } |
| | | //编码用户名 |
| | | if (ArrayUtil.isNotEmpty(addresses)) { |
| | | final String charsetStr = null == charset ? null : charset.name(); |
| | | for (InternetAddress internetAddress : addresses) { |
| | | try { |
| | | internetAddress.setPersonal(internetAddress.getPersonal(), charsetStr); |
| | | } catch (UnsupportedEncodingException e) { |
| | | throw new MailException(e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return addresses; |
| | | } |
| | | |
| | | /** |
| | | * 编码中文字符<br> |
| | | * 编码失败返回原字符串 |
| | | * |
| | | * @param text 被编码的文本 |
| | | * @param charset 编码 |
| | | * @return 编码后的结果 |
| | | */ |
| | | public static String encodeText(String text, Charset charset) { |
| | | try { |
| | | return MimeUtility.encodeText(text, charset.name(), null); |
| | | } catch (UnsupportedEncodingException e) { |
| | | // ignore |
| | | } |
| | | return text; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.builder.Builder; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.io.IORuntimeException; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import jakarta.activation.DataHandler; |
| | | import jakarta.activation.DataSource; |
| | | import jakarta.activation.FileDataSource; |
| | | import jakarta.activation.FileTypeMap; |
| | | import jakarta.mail.*; |
| | | import jakarta.mail.internet.MimeBodyPart; |
| | | import jakarta.mail.internet.MimeMessage; |
| | | import jakarta.mail.internet.MimeMultipart; |
| | | import jakarta.mail.internet.MimeUtility; |
| | | import jakarta.mail.util.ByteArrayDataSource; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 邮件发送客户端 |
| | | * |
| | | * @author looly |
| | | * @since 3.2.0 |
| | | */ |
| | | public class Mail implements Builder<MimeMessage> { |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 邮箱帐户信息以及一些客户端配置信息 |
| | | */ |
| | | private final MailAccount mailAccount; |
| | | /** |
| | | * 收件人列表 |
| | | */ |
| | | private String[] tos; |
| | | /** |
| | | * 抄送人列表(carbon copy) |
| | | */ |
| | | private String[] ccs; |
| | | /** |
| | | * 密送人列表(blind carbon copy) |
| | | */ |
| | | private String[] bccs; |
| | | /** |
| | | * 回复地址(reply-to) |
| | | */ |
| | | private String[] reply; |
| | | /** |
| | | * 标题 |
| | | */ |
| | | private String title; |
| | | /** |
| | | * 内容 |
| | | */ |
| | | private String content; |
| | | /** |
| | | * 是否为HTML |
| | | */ |
| | | private boolean isHtml; |
| | | /** |
| | | * 正文、附件和图片的混合部分 |
| | | */ |
| | | private final Multipart multipart = new MimeMultipart(); |
| | | /** |
| | | * 是否使用全局会话,默认为false |
| | | */ |
| | | private boolean useGlobalSession = false; |
| | | |
| | | /** |
| | | * debug输出位置,可以自定义debug日志 |
| | | */ |
| | | private PrintStream debugOutput; |
| | | |
| | | /** |
| | | * 创建邮件客户端 |
| | | * |
| | | * @param mailAccount 邮件帐号 |
| | | * @return Mail |
| | | */ |
| | | public static Mail create(MailAccount mailAccount) { |
| | | return new Mail(mailAccount); |
| | | } |
| | | |
| | | /** |
| | | * 创建邮件客户端,使用全局邮件帐户 |
| | | * |
| | | * @return Mail |
| | | */ |
| | | public static Mail create() { |
| | | return new Mail(); |
| | | } |
| | | |
| | | // --------------------------------------------------------------- Constructor start |
| | | |
| | | /** |
| | | * 构造,使用全局邮件帐户 |
| | | */ |
| | | public Mail() { |
| | | this(GlobalMailAccount.INSTANCE.getAccount()); |
| | | } |
| | | |
| | | /** |
| | | * 构造 |
| | | * |
| | | * @param mailAccount 邮件帐户,如果为null使用默认配置文件的全局邮件配置 |
| | | */ |
| | | public Mail(MailAccount mailAccount) { |
| | | mailAccount = (null != mailAccount) ? mailAccount : GlobalMailAccount.INSTANCE.getAccount(); |
| | | this.mailAccount = mailAccount.defaultIfEmpty(); |
| | | } |
| | | // --------------------------------------------------------------- Constructor end |
| | | |
| | | // --------------------------------------------------------------- Getters and Setters start |
| | | |
| | | /** |
| | | * 设置收件人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @return this |
| | | * @see #setTos(String...) |
| | | */ |
| | | public Mail to(String... tos) { |
| | | return setTos(tos); |
| | | } |
| | | |
| | | /** |
| | | * 设置多个收件人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @return this |
| | | */ |
| | | public Mail setTos(String... tos) { |
| | | this.tos = tos; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置多个抄送人(carbon copy) |
| | | * |
| | | * @param ccs 抄送人列表 |
| | | * @return this |
| | | * @since 4.0.3 |
| | | */ |
| | | public Mail setCcs(String... ccs) { |
| | | this.ccs = ccs; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置多个密送人(blind carbon copy) |
| | | * |
| | | * @param bccs 密送人列表 |
| | | * @return this |
| | | * @since 4.0.3 |
| | | */ |
| | | public Mail setBccs(String... bccs) { |
| | | this.bccs = bccs; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置多个回复地址(reply-to) |
| | | * |
| | | * @param reply 回复地址(reply-to)列表 |
| | | * @return this |
| | | * @since 4.6.0 |
| | | */ |
| | | public Mail setReply(String... reply) { |
| | | this.reply = reply; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置标题 |
| | | * |
| | | * @param title 标题 |
| | | * @return this |
| | | */ |
| | | public Mail setTitle(String title) { |
| | | this.title = title; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置正文<br> |
| | | * 正文可以是普通文本也可以是HTML(默认普通文本),可以通过调用{@link #setHtml(boolean)} 设置是否为HTML |
| | | * |
| | | * @param content 正文 |
| | | * @return this |
| | | */ |
| | | public Mail setContent(String content) { |
| | | this.content = content; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否是HTML |
| | | * |
| | | * @param isHtml 是否为HTML |
| | | * @return this |
| | | */ |
| | | public Mail setHtml(boolean isHtml) { |
| | | this.isHtml = isHtml; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置正文 |
| | | * |
| | | * @param content 正文内容 |
| | | * @param isHtml 是否为HTML |
| | | * @return this |
| | | */ |
| | | public Mail setContent(String content, boolean isHtml) { |
| | | setContent(content); |
| | | return setHtml(isHtml); |
| | | } |
| | | |
| | | /** |
| | | * 设置文件类型附件,文件可以是图片文件,此时自动设置cid(正文中引用图片),默认cid为文件名 |
| | | * |
| | | * @param files 附件文件列表 |
| | | * @return this |
| | | */ |
| | | public Mail setFiles(File... files) { |
| | | if (ArrayUtil.isEmpty(files)) { |
| | | return this; |
| | | } |
| | | |
| | | final DataSource[] attachments = new DataSource[files.length]; |
| | | for (int i = 0; i < files.length; i++) { |
| | | attachments[i] = new FileDataSource(files[i]); |
| | | } |
| | | return setAttachments(attachments); |
| | | } |
| | | |
| | | /** |
| | | * 增加附件或图片,附件使用{@link DataSource} 形式表示,可以使用{@link FileDataSource}包装文件表示文件附件 |
| | | * |
| | | * @param attachments 附件列表 |
| | | * @return this |
| | | * @since 4.0.9 |
| | | */ |
| | | public Mail setAttachments(DataSource... attachments) { |
| | | if (ArrayUtil.isNotEmpty(attachments)) { |
| | | final Charset charset = this.mailAccount.getCharset(); |
| | | MimeBodyPart bodyPart; |
| | | String nameEncoded; |
| | | try { |
| | | for (DataSource attachment : attachments) { |
| | | bodyPart = new MimeBodyPart(); |
| | | bodyPart.setDataHandler(new DataHandler(attachment)); |
| | | nameEncoded = attachment.getName(); |
| | | if (this.mailAccount.isEncodefilename()) { |
| | | nameEncoded = InternalMailUtil.encodeText(nameEncoded, charset); |
| | | } |
| | | // 普通附件文件名 |
| | | bodyPart.setFileName(nameEncoded); |
| | | if (StrUtil.startWith(attachment.getContentType(), "image/")) { |
| | | // 图片附件,用于正文中引用图片 |
| | | bodyPart.setContentID(nameEncoded); |
| | | } |
| | | this.multipart.addBodyPart(bodyPart); |
| | | } |
| | | } catch (MessagingException e) { |
| | | throw new MailException(e); |
| | | } |
| | | } |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 增加图片,图片的键对应到邮件模板中的占位字符串,图片类型默认为"image/jpeg" |
| | | * |
| | | * @param cid 图片与占位符,占位符格式为cid:${cid} |
| | | * @param imageStream 图片文件 |
| | | * @return this |
| | | * @since 4.6.3 |
| | | */ |
| | | public Mail addImage(String cid, InputStream imageStream) { |
| | | return addImage(cid, imageStream, null); |
| | | } |
| | | |
| | | /** |
| | | * 增加图片,图片的键对应到邮件模板中的占位字符串 |
| | | * |
| | | * @param cid 图片与占位符,占位符格式为cid:${cid} |
| | | * @param imageStream 图片流,不关闭 |
| | | * @param contentType 图片类型,null赋值默认的"image/jpeg" |
| | | * @return this |
| | | * @since 4.6.3 |
| | | */ |
| | | public Mail addImage(String cid, InputStream imageStream, String contentType) { |
| | | ByteArrayDataSource imgSource; |
| | | try { |
| | | imgSource = new ByteArrayDataSource(imageStream, ObjectUtil.defaultIfNull(contentType, "image/jpeg")); |
| | | } catch (IOException e) { |
| | | throw new IORuntimeException(e); |
| | | } |
| | | imgSource.setName(cid); |
| | | return setAttachments(imgSource); |
| | | } |
| | | |
| | | /** |
| | | * 增加图片,图片的键对应到邮件模板中的占位字符串 |
| | | * |
| | | * @param cid 图片与占位符,占位符格式为cid:${cid} |
| | | * @param imageFile 图片文件 |
| | | * @return this |
| | | * @since 4.6.3 |
| | | */ |
| | | public Mail addImage(String cid, File imageFile) { |
| | | InputStream in = null; |
| | | try { |
| | | in = FileUtil.getInputStream(imageFile); |
| | | return addImage(cid, in, FileTypeMap.getDefaultFileTypeMap().getContentType(imageFile)); |
| | | } finally { |
| | | IoUtil.close(in); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置字符集编码 |
| | | * |
| | | * @param charset 字符集编码 |
| | | * @return this |
| | | * @see MailAccount#setCharset(Charset) |
| | | */ |
| | | public Mail setCharset(Charset charset) { |
| | | this.mailAccount.setCharset(charset); |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否使用全局会话,默认为true |
| | | * |
| | | * @param isUseGlobalSession 是否使用全局会话,默认为true |
| | | * @return this |
| | | * @since 4.0.2 |
| | | */ |
| | | public Mail setUseGlobalSession(boolean isUseGlobalSession) { |
| | | this.useGlobalSession = isUseGlobalSession; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置debug输出位置,可以自定义debug日志 |
| | | * |
| | | * @param debugOutput debug输出位置 |
| | | * @return this |
| | | * @since 5.5.6 |
| | | */ |
| | | public Mail setDebugOutput(PrintStream debugOutput) { |
| | | this.debugOutput = debugOutput; |
| | | return this; |
| | | } |
| | | // --------------------------------------------------------------- Getters and Setters end |
| | | |
| | | @Override |
| | | public MimeMessage build() { |
| | | try { |
| | | return buildMsg(); |
| | | } catch (MessagingException e) { |
| | | throw new MailException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送 |
| | | * |
| | | * @return message-id |
| | | * @throws MailException 邮件发送异常 |
| | | */ |
| | | public String send() throws MailException { |
| | | try { |
| | | return doSend(); |
| | | } catch (MessagingException e) { |
| | | if (e instanceof SendFailedException) { |
| | | // 当地址无效时,显示更加详细的无效地址信息 |
| | | final Address[] invalidAddresses = ((SendFailedException) e).getInvalidAddresses(); |
| | | final String msg = StrUtil.format("Invalid Addresses: {}", ArrayUtil.toString(invalidAddresses)); |
| | | throw new MailException(msg, e); |
| | | } |
| | | throw new MailException(e); |
| | | } |
| | | } |
| | | |
| | | // --------------------------------------------------------------- Private method start |
| | | |
| | | /** |
| | | * 执行发送 |
| | | * |
| | | * @return message-id |
| | | * @throws MessagingException 发送异常 |
| | | */ |
| | | private String doSend() throws MessagingException { |
| | | final MimeMessage mimeMessage = buildMsg(); |
| | | Transport.send(mimeMessage); |
| | | return mimeMessage.getMessageID(); |
| | | } |
| | | |
| | | /** |
| | | * 构建消息 |
| | | * |
| | | * @return {@link MimeMessage}消息 |
| | | * @throws MessagingException 消息异常 |
| | | */ |
| | | private MimeMessage buildMsg() throws MessagingException { |
| | | final Charset charset = this.mailAccount.getCharset(); |
| | | final MimeMessage msg = new MimeMessage(getSession()); |
| | | // 发件人 |
| | | final String from = this.mailAccount.getFrom(); |
| | | if (StrUtil.isEmpty(from)) { |
| | | // 用户未提供发送方,则从Session中自动获取 |
| | | msg.setFrom(); |
| | | } else { |
| | | msg.setFrom(InternalMailUtil.parseFirstAddress(from, charset)); |
| | | } |
| | | // 标题 |
| | | msg.setSubject(this.title, (null == charset) ? null : charset.name()); |
| | | // 发送时间 |
| | | msg.setSentDate(new Date()); |
| | | // 内容和附件 |
| | | msg.setContent(buildContent(charset)); |
| | | // 收件人 |
| | | msg.setRecipients(MimeMessage.RecipientType.TO, InternalMailUtil.parseAddressFromStrs(this.tos, charset)); |
| | | // 抄送人 |
| | | if (ArrayUtil.isNotEmpty(this.ccs)) { |
| | | msg.setRecipients(MimeMessage.RecipientType.CC, InternalMailUtil.parseAddressFromStrs(this.ccs, charset)); |
| | | } |
| | | // 密送人 |
| | | if (ArrayUtil.isNotEmpty(this.bccs)) { |
| | | msg.setRecipients(MimeMessage.RecipientType.BCC, InternalMailUtil.parseAddressFromStrs(this.bccs, charset)); |
| | | } |
| | | // 回复地址(reply-to) |
| | | if (ArrayUtil.isNotEmpty(this.reply)) { |
| | | msg.setReplyTo(InternalMailUtil.parseAddressFromStrs(this.reply, charset)); |
| | | } |
| | | |
| | | return msg; |
| | | } |
| | | |
| | | /** |
| | | * 构建邮件信息主体 |
| | | * |
| | | * @param charset 编码,{@code null}则使用{@link MimeUtility#getDefaultJavaCharset()} |
| | | * @return 邮件信息主体 |
| | | * @throws MessagingException 消息异常 |
| | | */ |
| | | private Multipart buildContent(Charset charset) throws MessagingException { |
| | | final String charsetStr = null != charset ? charset.name() : MimeUtility.getDefaultJavaCharset(); |
| | | // 正文 |
| | | final MimeBodyPart body = new MimeBodyPart(); |
| | | body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charsetStr)); |
| | | this.multipart.addBodyPart(body); |
| | | |
| | | return this.multipart; |
| | | } |
| | | |
| | | /** |
| | | * 获取默认邮件会话<br> |
| | | * 如果为全局单例的会话,则全局只允许一个邮件帐号,否则每次发送邮件会新建一个新的会话 |
| | | * |
| | | * @return 邮件会话 {@link Session} |
| | | */ |
| | | private Session getSession() { |
| | | final Session session = MailUtils.getSession(this.mailAccount, this.useGlobalSession); |
| | | |
| | | if (null != this.debugOutput) { |
| | | session.setDebugOut(debugOutput); |
| | | } |
| | | |
| | | return session; |
| | | } |
| | | // --------------------------------------------------------------- Private method end |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.util.CharsetUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.setting.Setting; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.nio.charset.Charset; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * 邮件账户对象 |
| | | * |
| | | * @author Luxiaolei |
| | | */ |
| | | public class MailAccount implements Serializable { |
| | | @Serial |
| | | private static final long serialVersionUID = -6937313421815719204L; |
| | | |
| | | private static final String MAIL_PROTOCOL = "mail.transport.protocol"; |
| | | private static final String SMTP_HOST = "mail.smtp.host"; |
| | | private static final String SMTP_PORT = "mail.smtp.port"; |
| | | private static final String SMTP_AUTH = "mail.smtp.auth"; |
| | | private static final String SMTP_TIMEOUT = "mail.smtp.timeout"; |
| | | private static final String SMTP_CONNECTION_TIMEOUT = "mail.smtp.connectiontimeout"; |
| | | private static final String SMTP_WRITE_TIMEOUT = "mail.smtp.writetimeout"; |
| | | |
| | | // SSL |
| | | private static final String STARTTLS_ENABLE = "mail.smtp.starttls.enable"; |
| | | private static final String SSL_ENABLE = "mail.smtp.ssl.enable"; |
| | | private static final String SSL_PROTOCOLS = "mail.smtp.ssl.protocols"; |
| | | private static final String SOCKET_FACTORY = "mail.smtp.socketFactory.class"; |
| | | private static final String SOCKET_FACTORY_FALLBACK = "mail.smtp.socketFactory.fallback"; |
| | | private static final String SOCKET_FACTORY_PORT = "smtp.socketFactory.port"; |
| | | |
| | | // System Properties |
| | | private static final String SPLIT_LONG_PARAMS = "mail.mime.splitlongparameters"; |
| | | //private static final String ENCODE_FILE_NAME = "mail.mime.encodefilename"; |
| | | //private static final String CHARSET = "mail.mime.charset"; |
| | | |
| | | // 其他 |
| | | private static final String MAIL_DEBUG = "mail.debug"; |
| | | |
| | | public static final String[] MAIL_SETTING_PATHS = new String[]{"config/mail.setting", "config/mailAccount.setting", "mail.setting"}; |
| | | |
| | | /** |
| | | * SMTP服务器域名 |
| | | */ |
| | | private String host; |
| | | /** |
| | | * SMTP服务端口 |
| | | */ |
| | | private Integer port; |
| | | /** |
| | | * 是否需要用户名密码验证 |
| | | */ |
| | | private Boolean auth; |
| | | /** |
| | | * 用户名 |
| | | */ |
| | | private String user; |
| | | /** |
| | | * 密码 |
| | | */ |
| | | private String pass; |
| | | /** |
| | | * 发送方,遵循RFC-822标准 |
| | | */ |
| | | private String from; |
| | | |
| | | /** |
| | | * 是否打开调试模式,调试模式会显示与邮件服务器通信过程,默认不开启 |
| | | */ |
| | | private boolean debug; |
| | | /** |
| | | * 编码用于编码邮件正文和发送人、收件人等中文 |
| | | */ |
| | | private Charset charset = CharsetUtil.CHARSET_UTF_8; |
| | | /** |
| | | * 对于超长参数是否切分为多份,默认为false(国内邮箱附件不支持切分的附件名) |
| | | */ |
| | | private boolean splitlongparameters = false; |
| | | /** |
| | | * 对于文件名是否使用{@link #charset}编码,默认为 {@code true} |
| | | */ |
| | | private boolean encodefilename = true; |
| | | |
| | | /** |
| | | * 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。 |
| | | */ |
| | | private boolean starttlsEnable = false; |
| | | /** |
| | | * 使用 SSL安全连接 |
| | | */ |
| | | private Boolean sslEnable; |
| | | |
| | | /** |
| | | * SSL协议,多个协议用空格分隔 |
| | | */ |
| | | private String sslProtocols; |
| | | |
| | | /** |
| | | * 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字 |
| | | */ |
| | | private String socketFactoryClass = "javax.net.ssl.SSLSocketFactory"; |
| | | /** |
| | | * 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true |
| | | */ |
| | | private boolean socketFactoryFallback; |
| | | /** |
| | | * 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 |
| | | */ |
| | | private int socketFactoryPort = 465; |
| | | |
| | | /** |
| | | * SMTP超时时长,单位毫秒,缺省值不超时 |
| | | */ |
| | | private long timeout; |
| | | /** |
| | | * Socket连接超时值,单位毫秒,缺省值不超时 |
| | | */ |
| | | private long connectionTimeout; |
| | | /** |
| | | * Socket写出超时值,单位毫秒,缺省值不超时 |
| | | */ |
| | | private long writeTimeout; |
| | | |
| | | /** |
| | | * 自定义的其他属性,此自定义属性会覆盖默认属性 |
| | | */ |
| | | private final Map<String, Object> customProperty = new HashMap<>(); |
| | | |
| | | // -------------------------------------------------------------- Constructor start |
| | | |
| | | /** |
| | | * 构造,所有参数需自行定义或保持默认值 |
| | | */ |
| | | public MailAccount() { |
| | | } |
| | | |
| | | /** |
| | | * 构造 |
| | | * |
| | | * @param settingPath 配置文件路径 |
| | | */ |
| | | public MailAccount(String settingPath) { |
| | | this(new Setting(settingPath)); |
| | | } |
| | | |
| | | /** |
| | | * 构造 |
| | | * |
| | | * @param setting 配置文件 |
| | | */ |
| | | public MailAccount(Setting setting) { |
| | | setting.toBean(this); |
| | | } |
| | | |
| | | // -------------------------------------------------------------- Constructor end |
| | | |
| | | /** |
| | | * 获得SMTP服务器域名 |
| | | * |
| | | * @return SMTP服务器域名 |
| | | */ |
| | | public String getHost() { |
| | | return host; |
| | | } |
| | | |
| | | /** |
| | | * 设置SMTP服务器域名 |
| | | * |
| | | * @param host SMTP服务器域名 |
| | | * @return this |
| | | */ |
| | | public MailAccount setHost(String host) { |
| | | this.host = host; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获得SMTP服务端口 |
| | | * |
| | | * @return SMTP服务端口 |
| | | */ |
| | | public Integer getPort() { |
| | | return port; |
| | | } |
| | | |
| | | /** |
| | | * 设置SMTP服务端口 |
| | | * |
| | | * @param port SMTP服务端口 |
| | | * @return this |
| | | */ |
| | | public MailAccount setPort(Integer port) { |
| | | this.port = port; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 是否需要用户名密码验证 |
| | | * |
| | | * @return 是否需要用户名密码验证 |
| | | */ |
| | | public Boolean isAuth() { |
| | | return auth; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否需要用户名密码验证 |
| | | * |
| | | * @param isAuth 是否需要用户名密码验证 |
| | | * @return this |
| | | */ |
| | | public MailAccount setAuth(boolean isAuth) { |
| | | this.auth = isAuth; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户名 |
| | | * |
| | | * @return 用户名 |
| | | */ |
| | | public String getUser() { |
| | | return user; |
| | | } |
| | | |
| | | /** |
| | | * 设置用户名 |
| | | * |
| | | * @param user 用户名 |
| | | * @return this |
| | | */ |
| | | public MailAccount setUser(String user) { |
| | | this.user = user; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取密码 |
| | | * |
| | | * @return 密码 |
| | | */ |
| | | public String getPass() { |
| | | return pass; |
| | | } |
| | | |
| | | /** |
| | | * 设置密码 |
| | | * |
| | | * @param pass 密码 |
| | | * @return this |
| | | */ |
| | | public MailAccount setPass(String pass) { |
| | | this.pass = pass; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取发送方,遵循RFC-822标准 |
| | | * |
| | | * @return 发送方,遵循RFC-822标准 |
| | | */ |
| | | public String getFrom() { |
| | | return from; |
| | | } |
| | | |
| | | /** |
| | | * 设置发送方,遵循RFC-822标准<br> |
| | | * 发件人可以是以下形式: |
| | | * |
| | | * <pre> |
| | | * 1. user@xxx.xx |
| | | * 2. name <user@xxx.xx> |
| | | * </pre> |
| | | * |
| | | * @param from 发送方,遵循RFC-822标准 |
| | | * @return this |
| | | */ |
| | | public MailAccount setFrom(String from) { |
| | | this.from = from; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 是否打开调试模式,调试模式会显示与邮件服务器通信过程,默认不开启 |
| | | * |
| | | * @return 是否打开调试模式,调试模式会显示与邮件服务器通信过程,默认不开启 |
| | | * @since 4.0.2 |
| | | */ |
| | | public boolean isDebug() { |
| | | return debug; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否打开调试模式,调试模式会显示与邮件服务器通信过程,默认不开启 |
| | | * |
| | | * @param debug 是否打开调试模式,调试模式会显示与邮件服务器通信过程,默认不开启 |
| | | * @return this |
| | | * @since 4.0.2 |
| | | */ |
| | | public MailAccount setDebug(boolean debug) { |
| | | this.debug = debug; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取字符集编码 |
| | | * |
| | | * @return 编码,可能为{@code null} |
| | | */ |
| | | public Charset getCharset() { |
| | | return charset; |
| | | } |
| | | |
| | | /** |
| | | * 设置字符集编码,此选项不会修改全局配置,若修改全局配置,请设置此项为{@code null}并设置: |
| | | * <pre> |
| | | * System.setProperty("mail.mime.charset", charset); |
| | | * </pre> |
| | | * |
| | | * @param charset 字符集编码,{@code null} 则表示使用全局设置的默认编码,全局编码为mail.mime.charset系统属性 |
| | | * @return this |
| | | */ |
| | | public MailAccount setCharset(Charset charset) { |
| | | this.charset = charset; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 对于超长参数是否切分为多份,默认为false(国内邮箱附件不支持切分的附件名) |
| | | * |
| | | * @return 对于超长参数是否切分为多份 |
| | | */ |
| | | public boolean isSplitlongparameters() { |
| | | return splitlongparameters; |
| | | } |
| | | |
| | | /** |
| | | * 设置对于超长参数是否切分为多份,默认为false(国内邮箱附件不支持切分的附件名)<br> |
| | | * 注意此项为全局设置,此项会调用 |
| | | * <pre> |
| | | * System.setProperty("mail.mime.splitlongparameters", true) |
| | | * </pre> |
| | | * |
| | | * @param splitlongparameters 对于超长参数是否切分为多份 |
| | | */ |
| | | public void setSplitlongparameters(boolean splitlongparameters) { |
| | | this.splitlongparameters = splitlongparameters; |
| | | } |
| | | |
| | | /** |
| | | * 对于文件名是否使用{@link #charset}编码,默认为 {@code true} |
| | | * |
| | | * @return 对于文件名是否使用{@link #charset}编码,默认为 {@code true} |
| | | * @since 5.7.16 |
| | | */ |
| | | public boolean isEncodefilename() { |
| | | |
| | | return encodefilename; |
| | | } |
| | | |
| | | /** |
| | | * 设置对于文件名是否使用{@link #charset}编码,此选项不会修改全局配置<br> |
| | | * 如果此选项设置为{@code false},则是否编码取决于两个系统属性: |
| | | * <ul> |
| | | * <li>mail.mime.encodefilename 是否编码附件文件名</li> |
| | | * <li>mail.mime.charset 编码文件名的编码</li> |
| | | * </ul> |
| | | * |
| | | * @param encodefilename 对于文件名是否使用{@link #charset}编码 |
| | | * @since 5.7.16 |
| | | */ |
| | | public void setEncodefilename(boolean encodefilename) { |
| | | this.encodefilename = encodefilename; |
| | | } |
| | | |
| | | /** |
| | | * 是否使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。 |
| | | * |
| | | * @return 是否使用 STARTTLS安全连接 |
| | | */ |
| | | public boolean isStarttlsEnable() { |
| | | return this.starttlsEnable; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否使用STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。 |
| | | * |
| | | * @param startttlsEnable 是否使用STARTTLS安全连接 |
| | | * @return this |
| | | */ |
| | | public MailAccount setStarttlsEnable(boolean startttlsEnable) { |
| | | this.starttlsEnable = startttlsEnable; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 是否使用 SSL安全连接 |
| | | * |
| | | * @return 是否使用 SSL安全连接 |
| | | */ |
| | | public Boolean isSslEnable() { |
| | | return this.sslEnable; |
| | | } |
| | | |
| | | /** |
| | | * 设置是否使用SSL安全连接 |
| | | * |
| | | * @param sslEnable 是否使用SSL安全连接 |
| | | * @return this |
| | | */ |
| | | public MailAccount setSslEnable(Boolean sslEnable) { |
| | | this.sslEnable = sslEnable; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取SSL协议,多个协议用空格分隔 |
| | | * |
| | | * @return SSL协议,多个协议用空格分隔 |
| | | * @since 5.5.7 |
| | | */ |
| | | public String getSslProtocols() { |
| | | return sslProtocols; |
| | | } |
| | | |
| | | /** |
| | | * 设置SSL协议,多个协议用空格分隔 |
| | | * |
| | | * @param sslProtocols SSL协议,多个协议用空格分隔 |
| | | * @since 5.5.7 |
| | | */ |
| | | public void setSslProtocols(String sslProtocols) { |
| | | this.sslProtocols = sslProtocols; |
| | | } |
| | | |
| | | /** |
| | | * 获取指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字 |
| | | * |
| | | * @return 指定实现javax.net.SocketFactory接口的类的名称, 这个类将被用于创建SMTP的套接字 |
| | | */ |
| | | public String getSocketFactoryClass() { |
| | | return socketFactoryClass; |
| | | } |
| | | |
| | | /** |
| | | * 设置指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字 |
| | | * |
| | | * @param socketFactoryClass 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字 |
| | | * @return this |
| | | */ |
| | | public MailAccount setSocketFactoryClass(String socketFactoryClass) { |
| | | this.socketFactoryClass = socketFactoryClass; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true |
| | | * |
| | | * @return 如果设置为true, 未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true |
| | | */ |
| | | public boolean isSocketFactoryFallback() { |
| | | return socketFactoryFallback; |
| | | } |
| | | |
| | | /** |
| | | * 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true |
| | | * |
| | | * @param socketFactoryFallback 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true |
| | | * @return this |
| | | */ |
| | | public MailAccount setSocketFactoryFallback(boolean socketFactoryFallback) { |
| | | this.socketFactoryFallback = socketFactoryFallback; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 |
| | | * |
| | | * @return 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 |
| | | */ |
| | | public int getSocketFactoryPort() { |
| | | return socketFactoryPort; |
| | | } |
| | | |
| | | /** |
| | | * 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 |
| | | * |
| | | * @param socketFactoryPort 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 |
| | | * @return this |
| | | */ |
| | | public MailAccount setSocketFactoryPort(int socketFactoryPort) { |
| | | this.socketFactoryPort = socketFactoryPort; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置SMTP超时时长,单位毫秒,缺省值不超时 |
| | | * |
| | | * @param timeout SMTP超时时长,单位毫秒,缺省值不超时 |
| | | * @return this |
| | | * @since 4.1.17 |
| | | */ |
| | | public MailAccount setTimeout(long timeout) { |
| | | this.timeout = timeout; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置Socket连接超时值,单位毫秒,缺省值不超时 |
| | | * |
| | | * @param connectionTimeout Socket连接超时值,单位毫秒,缺省值不超时 |
| | | * @return this |
| | | * @since 4.1.17 |
| | | */ |
| | | public MailAccount setConnectionTimeout(long connectionTimeout) { |
| | | this.connectionTimeout = connectionTimeout; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置Socket写出超时值,单位毫秒,缺省值不超时 |
| | | * |
| | | * @param writeTimeout Socket写出超时值,单位毫秒,缺省值不超时 |
| | | * @return this |
| | | * @since 5.8.3 |
| | | */ |
| | | public MailAccount setWriteTimeout(long writeTimeout) { |
| | | this.writeTimeout = writeTimeout; |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获取自定义属性列表 |
| | | * |
| | | * @return 自定义参数列表 |
| | | * @since 5.6.4 |
| | | */ |
| | | public Map<String, Object> getCustomProperty() { |
| | | return customProperty; |
| | | } |
| | | |
| | | /** |
| | | * 设置自定义属性,如mail.smtp.ssl.socketFactory |
| | | * |
| | | * @param key 属性名,空白被忽略 |
| | | * @param value 属性值, null被忽略 |
| | | * @return this |
| | | * @since 5.6.4 |
| | | */ |
| | | public MailAccount setCustomProperty(String key, Object value) { |
| | | if (StrUtil.isNotBlank(key) && ObjectUtil.isNotNull(value)) { |
| | | this.customProperty.put(key, value); |
| | | } |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 获得SMTP相关信息 |
| | | * |
| | | * @return {@link Properties} |
| | | */ |
| | | public Properties getSmtpProps() { |
| | | //全局系统参数 |
| | | System.setProperty(SPLIT_LONG_PARAMS, String.valueOf(this.splitlongparameters)); |
| | | |
| | | final Properties p = new Properties(); |
| | | p.put(MAIL_PROTOCOL, "smtp"); |
| | | p.put(SMTP_HOST, this.host); |
| | | p.put(SMTP_PORT, String.valueOf(this.port)); |
| | | p.put(SMTP_AUTH, String.valueOf(this.auth)); |
| | | if (this.timeout > 0) { |
| | | p.put(SMTP_TIMEOUT, String.valueOf(this.timeout)); |
| | | } |
| | | if (this.connectionTimeout > 0) { |
| | | p.put(SMTP_CONNECTION_TIMEOUT, String.valueOf(this.connectionTimeout)); |
| | | } |
| | | // issue#2355 |
| | | if (this.writeTimeout > 0) { |
| | | p.put(SMTP_WRITE_TIMEOUT, String.valueOf(this.writeTimeout)); |
| | | } |
| | | |
| | | p.put(MAIL_DEBUG, String.valueOf(this.debug)); |
| | | |
| | | if (this.starttlsEnable) { |
| | | //STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。 |
| | | p.put(STARTTLS_ENABLE, "true"); |
| | | |
| | | if (null == this.sslEnable) { |
| | | //为了兼容旧版本,当用户没有此项配置时,按照starttlsEnable开启状态时对待 |
| | | this.sslEnable = true; |
| | | } |
| | | } |
| | | |
| | | // SSL |
| | | if (null != this.sslEnable && this.sslEnable) { |
| | | p.put(SSL_ENABLE, "true"); |
| | | p.put(SOCKET_FACTORY, socketFactoryClass); |
| | | p.put(SOCKET_FACTORY_FALLBACK, String.valueOf(this.socketFactoryFallback)); |
| | | p.put(SOCKET_FACTORY_PORT, String.valueOf(this.socketFactoryPort)); |
| | | // issue#IZN95@Gitee,在Linux下需自定义SSL协议版本 |
| | | if (StrUtil.isNotBlank(this.sslProtocols)) { |
| | | p.put(SSL_PROTOCOLS, this.sslProtocols); |
| | | } |
| | | } |
| | | |
| | | // 补充自定义属性,允许自定属性覆盖已经设置的值 |
| | | p.putAll(this.customProperty); |
| | | |
| | | return p; |
| | | } |
| | | |
| | | /** |
| | | * 如果某些值为null,使用默认值 |
| | | * |
| | | * @return this |
| | | */ |
| | | public MailAccount defaultIfEmpty() { |
| | | // 去掉发件人的姓名部分 |
| | | final String fromAddress = InternalMailUtil.parseFirstAddress(this.from, this.charset).getAddress(); |
| | | |
| | | if (StrUtil.isBlank(this.host)) { |
| | | // 如果SMTP地址为空,默认使用smtp.<发件人邮箱后缀> |
| | | this.host = StrUtil.format("smtp.{}", StrUtil.subSuf(fromAddress, fromAddress.indexOf('@') + 1)); |
| | | } |
| | | if (StrUtil.isBlank(user)) { |
| | | // 如果用户名为空,默认为发件人(issue#I4FYVY@Gitee) |
| | | //this.user = StrUtil.subPre(fromAddress, fromAddress.indexOf('@')); |
| | | this.user = fromAddress; |
| | | } |
| | | if (null == this.auth) { |
| | | // 如果密码非空白,则使用认证模式 |
| | | this.auth = (false == StrUtil.isBlank(this.pass)); |
| | | } |
| | | if (null == this.port) { |
| | | // 端口在SSL状态下默认与socketFactoryPort一致,非SSL状态下默认为25 |
| | | this.port = (null != this.sslEnable && this.sslEnable) ? this.socketFactoryPort : 25; |
| | | } |
| | | if (null == this.charset) { |
| | | // 默认UTF-8编码 |
| | | this.charset = CharsetUtil.CHARSET_UTF_8; |
| | | } |
| | | |
| | | return this; |
| | | } |
| | | |
| | | @Override |
| | | public String toString() { |
| | | return "MailAccount [host=" + host + ", port=" + port + ", auth=" + auth + ", user=" + user + ", pass=" + (StrUtil.isEmpty(this.pass) ? "" : "******") + ", from=" + from + ", startttlsEnable=" |
| | | + starttlsEnable + ", socketFactoryClass=" + socketFactoryClass + ", socketFactoryFallback=" + socketFactoryFallback + ", socketFactoryPort=" + socketFactoryPort + "]"; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.exceptions.ExceptionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 邮件异常 |
| | | * |
| | | * @author xiaoleilu |
| | | */ |
| | | public class MailException extends RuntimeException { |
| | | @Serial |
| | | private static final long serialVersionUID = 8247610319171014183L; |
| | | |
| | | public MailException(Throwable e) { |
| | | super(ExceptionUtil.getMessage(e), e); |
| | | } |
| | | |
| | | public MailException(String message) { |
| | | super(message); |
| | | } |
| | | |
| | | public MailException(String messageTemplate, Object... params) { |
| | | super(StrUtil.format(messageTemplate, params)); |
| | | } |
| | | |
| | | public MailException(String message, Throwable throwable) { |
| | | super(message, throwable); |
| | | } |
| | | |
| | | public MailException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) { |
| | | super(message, throwable, enableSuppression, writableStackTrace); |
| | | } |
| | | |
| | | public MailException(Throwable throwable, String messageTemplate, Object... params) { |
| | | super(StrUtil.format(messageTemplate, params), throwable); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.CharUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import jakarta.mail.Authenticator; |
| | | import jakarta.mail.Session; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | import java.io.File; |
| | | import java.io.InputStream; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | |
| | | /** |
| | | * 邮件工具类 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class MailUtils { |
| | | |
| | | private static final MailAccount ACCOUNT = SpringUtils.getBean(MailAccount.class); |
| | | |
| | | /** |
| | | * 获取邮件发送实例 |
| | | */ |
| | | public static MailAccount getMailAccount() { |
| | | return ACCOUNT; |
| | | } |
| | | |
| | | /** |
| | | * 获取邮件发送实例 (自定义发送人以及授权码) |
| | | * |
| | | * @param user 发送人 |
| | | * @param pass 授权码 |
| | | */ |
| | | public static MailAccount getMailAccount(String from, String user, String pass) { |
| | | ACCOUNT.setFrom(StringUtils.blankToDefault(from, ACCOUNT.getFrom())); |
| | | ACCOUNT.setUser(StringUtils.blankToDefault(user, ACCOUNT.getUser())); |
| | | ACCOUNT.setPass(StringUtils.blankToDefault(pass, ACCOUNT.getPass())); |
| | | return ACCOUNT; |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送文本邮件,发送给单个或多个收件人<br> |
| | | * 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String sendText(String to, String subject, String content, File... files) { |
| | | return send(to, subject, content, false, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送HTML邮件,发送给单个或多个收件人<br> |
| | | * 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String sendHtml(String to, String subject, String content, File... files) { |
| | | return send(to, subject, content, true, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br> |
| | | * 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String send(String to, String subject, String content, boolean isHtml, File... files) { |
| | | return send(splitAddress(to), subject, content, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br> |
| | | * 多个收件人、抄送人、密送人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param cc 抄送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param bcc 密送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.0.3 |
| | | */ |
| | | public static String send(String to, String cc, String bcc, String subject, String content, boolean isHtml, File... files) { |
| | | return send(splitAddress(to), splitAddress(cc), splitAddress(bcc), subject, content, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送文本邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String sendText(Collection<String> tos, String subject, String content, File... files) { |
| | | return send(tos, subject, content, false, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送HTML邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String sendHtml(Collection<String> tos, String subject, String content, File... files) { |
| | | return send(tos, subject, content, true, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String send(Collection<String> tos, String subject, String content, boolean isHtml, File... files) { |
| | | return send(tos, null, null, subject, content, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param ccs 抄送人列表,可以为null或空 |
| | | * @param bccs 密送人列表,可以为null或空 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.0.3 |
| | | */ |
| | | public static String send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, boolean isHtml, File... files) { |
| | | return send(getMailAccount(), true, tos, ccs, bccs, subject, content, null, isHtml, files); |
| | | } |
| | | |
| | | // ------------------------------------------------------------------------------------------------------------------------------- Custom MailAccount |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件认证对象 |
| | | * @param to 收件人,多个收件人逗号或者分号隔开 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String send(MailAccount mailAccount, String to, String subject, String content, boolean isHtml, File... files) { |
| | | return send(mailAccount, splitAddress(to), subject, content, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件帐户信息 |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String send(MailAccount mailAccount, Collection<String> tos, String subject, String content, boolean isHtml, File... files) { |
| | | return send(mailAccount, tos, null, null, subject, content, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件帐户信息 |
| | | * @param tos 收件人列表 |
| | | * @param ccs 抄送人列表,可以为null或空 |
| | | * @param bccs 密送人列表,可以为null或空 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.0.3 |
| | | */ |
| | | public static String send(MailAccount mailAccount, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, boolean isHtml, File... files) { |
| | | return send(mailAccount, false, tos, ccs, bccs, subject, content, null, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送HTML邮件,发送给单个或多个收件人<br> |
| | | * 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String sendHtml(String to, String subject, String content, Map<String, InputStream> imageMap, File... files) { |
| | | return send(to, subject, content, imageMap, true, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br> |
| | | * 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String send(String to, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(splitAddress(to), subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br> |
| | | * 多个收件人、抄送人、密送人可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * |
| | | * @param to 收件人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param cc 抄送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param bcc 密送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.0.3 |
| | | */ |
| | | public static String send(String to, String cc, String bcc, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(splitAddress(to), splitAddress(cc), splitAddress(bcc), subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送HTML邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String sendHtml(Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, File... files) { |
| | | return send(tos, subject, content, imageMap, true, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | */ |
| | | public static String send(Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(tos, null, null, subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 使用配置文件中设置的账户发送邮件,发送给多人 |
| | | * |
| | | * @param tos 收件人列表 |
| | | * @param ccs 抄送人列表,可以为null或空 |
| | | * @param bccs 密送人列表,可以为null或空 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.0.3 |
| | | */ |
| | | public static String send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(getMailAccount(), true, tos, ccs, bccs, subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | // ------------------------------------------------------------------------------------------------------------------------------- Custom MailAccount |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件认证对象 |
| | | * @param to 收件人,多个收件人逗号或者分号隔开 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String send(MailAccount mailAccount, String to, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(mailAccount, splitAddress(to), subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件帐户信息 |
| | | * @param tos 收件人列表 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.6.3 |
| | | */ |
| | | public static String send(MailAccount mailAccount, Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | return send(mailAccount, tos, null, null, subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件帐户信息 |
| | | * @param tos 收件人列表 |
| | | * @param ccs 抄送人列表,可以为null或空 |
| | | * @param bccs 密送人列表,可以为null或空 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.6.3 |
| | | */ |
| | | public static String send(MailAccount mailAccount, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, Map<String, InputStream> imageMap, |
| | | boolean isHtml, File... files) { |
| | | return send(mailAccount, false, tos, ccs, bccs, subject, content, imageMap, isHtml, files); |
| | | } |
| | | |
| | | /** |
| | | * 根据配置文件,获取邮件客户端会话 |
| | | * |
| | | * @param mailAccount 邮件账户配置 |
| | | * @param isSingleton 是否单例(全局共享会话) |
| | | * @return {@link Session} |
| | | * @since 5.5.7 |
| | | */ |
| | | public static Session getSession(MailAccount mailAccount, boolean isSingleton) { |
| | | Authenticator authenticator = null; |
| | | if (mailAccount.isAuth()) { |
| | | authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass()); |
| | | } |
| | | |
| | | return isSingleton ? Session.getDefaultInstance(mailAccount.getSmtpProps(), authenticator) // |
| | | : Session.getInstance(mailAccount.getSmtpProps(), authenticator); |
| | | } |
| | | |
| | | // ------------------------------------------------------------------------------------------------------------------------ Private method start |
| | | |
| | | /** |
| | | * 发送邮件给多人 |
| | | * |
| | | * @param mailAccount 邮件帐户信息 |
| | | * @param useGlobalSession 是否全局共享Session |
| | | * @param tos 收件人列表 |
| | | * @param ccs 抄送人列表,可以为null或空 |
| | | * @param bccs 密送人列表,可以为null或空 |
| | | * @param subject 标题 |
| | | * @param content 正文 |
| | | * @param imageMap 图片与占位符,占位符格式为cid:${cid} |
| | | * @param isHtml 是否为HTML格式 |
| | | * @param files 附件列表 |
| | | * @return message-id |
| | | * @since 4.6.3 |
| | | */ |
| | | private static String send(MailAccount mailAccount, boolean useGlobalSession, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, |
| | | Map<String, InputStream> imageMap, boolean isHtml, File... files) { |
| | | final Mail mail = Mail.create(mailAccount).setUseGlobalSession(useGlobalSession); |
| | | |
| | | // 可选抄送人 |
| | | if (CollUtil.isNotEmpty(ccs)) { |
| | | mail.setCcs(ccs.toArray(new String[0])); |
| | | } |
| | | // 可选密送人 |
| | | if (CollUtil.isNotEmpty(bccs)) { |
| | | mail.setBccs(bccs.toArray(new String[0])); |
| | | } |
| | | |
| | | mail.setTos(tos.toArray(new String[0])); |
| | | mail.setTitle(subject); |
| | | mail.setContent(content); |
| | | mail.setHtml(isHtml); |
| | | mail.setFiles(files); |
| | | |
| | | // 图片 |
| | | if (MapUtil.isNotEmpty(imageMap)) { |
| | | for (Map.Entry<String, InputStream> entry : imageMap.entrySet()) { |
| | | mail.addImage(entry.getKey(), entry.getValue()); |
| | | // 关闭流 |
| | | IoUtil.close(entry.getValue()); |
| | | } |
| | | } |
| | | |
| | | return mail.send(); |
| | | } |
| | | |
| | | /** |
| | | * 将多个联系人转为列表,分隔符为逗号或者分号 |
| | | * |
| | | * @param addresses 多个联系人,如果为空返回null |
| | | * @return 联系人列表 |
| | | */ |
| | | private static List<String> splitAddress(String addresses) { |
| | | if (StrUtil.isBlank(addresses)) { |
| | | return null; |
| | | } |
| | | |
| | | List<String> result; |
| | | if (StrUtil.contains(addresses, CharUtil.COMMA)) { |
| | | result = StrUtil.splitTrim(addresses, CharUtil.COMMA); |
| | | } else if (StrUtil.contains(addresses, ';')) { |
| | | result = StrUtil.splitTrim(addresses, ';'); |
| | | } else { |
| | | result = CollUtil.newArrayList(addresses); |
| | | } |
| | | return result; |
| | | } |
| | | // ------------------------------------------------------------------------------------------------------------------------ Private method end |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mail.utils; |
| | | |
| | | import jakarta.mail.Authenticator; |
| | | import jakarta.mail.PasswordAuthentication; |
| | | |
| | | /** |
| | | * 用户名密码验证器 |
| | | * |
| | | * @author looly |
| | | * @since 3.1.2 |
| | | */ |
| | | public class UserPassAuthenticator extends Authenticator { |
| | | |
| | | private final String user; |
| | | private final String pass; |
| | | |
| | | /** |
| | | * 构造 |
| | | * |
| | | * @param user 用户名 |
| | | * @param pass 密码 |
| | | */ |
| | | public UserPassAuthenticator(String user, String pass) { |
| | | this.user = user; |
| | | this.pass = pass; |
| | | } |
| | | |
| | | @Override |
| | | protected PasswordAuthentication getPasswordAuthentication() { |
| | | return new PasswordAuthentication(this.user, this.pass); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.mail.config.MailConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-mybatis</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-mybatis 数据库服务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-dubbo</artifactId> |
| | | <optional>true</optional> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.mybatis.spring.boot</groupId> |
| | | <artifactId>mybatis-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>mybatis-plus-boot-starter</artifactId> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.mybatis</groupId> |
| | | <artifactId>mybatis-spring</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | <!-- sql性能分析插件 --> |
| | | <dependency> |
| | | <groupId>p6spy</groupId> |
| | | <artifactId>p6spy</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Dynamic DataSource --> |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>dynamic-datasource-spring-boot3-starter</artifactId> |
| | | <version>${dynamic-ds.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- Mysql Connector --> |
| | | <dependency> |
| | | <groupId>com.mysql</groupId> |
| | | <artifactId>mysql-connector-j</artifactId> |
| | | </dependency> |
| | | <!-- Oracle --> |
| | | <dependency> |
| | | <groupId>com.oracle.database.jdbc</groupId> |
| | | <artifactId>ojdbc8</artifactId> |
| | | </dependency> |
| | | <!-- PostgreSql --> |
| | | <dependency> |
| | | <groupId>org.postgresql</groupId> |
| | | <artifactId>postgresql</artifactId> |
| | | </dependency> |
| | | <!-- SqlServer --> |
| | | <dependency> |
| | | <groupId>com.microsoft.sqlserver</groupId> |
| | | <artifactId>mssql-jdbc</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.mybatis.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 数据权限 |
| | | * |
| | | * 一个注解只能对应一个模板 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface DataColumn { |
| | | |
| | | /** |
| | | * 占位符关键字 |
| | | */ |
| | | String[] key() default "deptName"; |
| | | |
| | | /** |
| | | * 占位符替换值 |
| | | */ |
| | | String[] value() default "dept_id"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 数据权限组 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | @Target({ElementType.METHOD, ElementType.TYPE}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface DataPermission { |
| | | |
| | | DataColumn[] value(); |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.config; |
| | | |
| | | import cn.hutool.core.net.NetUtil; |
| | | import com.baomidou.mybatisplus.autoconfigure.DdlApplicationRunner; |
| | | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| | | import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; |
| | | import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator; |
| | | import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; |
| | | import com.baomidou.mybatisplus.extension.ddl.IDdl; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; |
| | | import org.dromara.common.core.factory.YmlPropertySourceFactory; |
| | | import org.dromara.common.mybatis.handler.InjectionMetaObjectHandler; |
| | | import org.dromara.common.mybatis.interceptor.PlusDataPermissionInterceptor; |
| | | import org.mybatis.spring.annotation.MapperScan; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.PropertySource; |
| | | import org.springframework.transaction.annotation.EnableTransactionManagement; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * mybatis-plus配置类(下方注释有插件介绍) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @EnableTransactionManagement(proxyTargetClass = true) |
| | | @AutoConfiguration(before = MybatisPlusAutoConfiguration.class) |
| | | @MapperScan("${mybatis-plus.mapperPackage}") |
| | | @PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class) |
| | | public class MybatisPlusConfiguration { |
| | | |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | // 数据权限处理 |
| | | interceptor.addInnerInterceptor(dataPermissionInterceptor()); |
| | | // 分页插件 |
| | | interceptor.addInnerInterceptor(paginationInnerInterceptor()); |
| | | // 乐观锁插件 |
| | | interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); |
| | | return interceptor; |
| | | } |
| | | |
| | | /** |
| | | * 数据权限拦截器 |
| | | */ |
| | | public PlusDataPermissionInterceptor dataPermissionInterceptor() { |
| | | return new PlusDataPermissionInterceptor(); |
| | | } |
| | | |
| | | /** |
| | | * 分页插件,自动识别数据库类型 |
| | | */ |
| | | public PaginationInnerInterceptor paginationInnerInterceptor() { |
| | | PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); |
| | | // 设置最大单页限制数量,默认 500 条,-1 不受限制 |
| | | paginationInnerInterceptor.setMaxLimit(-1L); |
| | | // 分页合理化 |
| | | paginationInnerInterceptor.setOverflow(true); |
| | | return paginationInnerInterceptor; |
| | | } |
| | | |
| | | /** |
| | | * 乐观锁插件 |
| | | */ |
| | | public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { |
| | | return new OptimisticLockerInnerInterceptor(); |
| | | } |
| | | |
| | | /** |
| | | * 元对象字段填充控制器 |
| | | */ |
| | | @Bean |
| | | public MetaObjectHandler metaObjectHandler() { |
| | | return new InjectionMetaObjectHandler(); |
| | | } |
| | | |
| | | /** |
| | | * 使用网卡信息绑定雪花生成器 |
| | | * 防止集群雪花ID重复 |
| | | */ |
| | | @Bean |
| | | public IdentifierGenerator idGenerator() { |
| | | return new DefaultIdentifierGenerator(NetUtil.getLocalhost()); |
| | | } |
| | | |
| | | /** |
| | | * PaginationInnerInterceptor 分页插件,自动识别数据库类型 |
| | | * https://baomidou.com/pages/97710a/ |
| | | * OptimisticLockerInnerInterceptor 乐观锁插件 |
| | | * https://baomidou.com/pages/0d93c0/ |
| | | * MetaObjectHandler 元对象字段填充控制器 |
| | | * https://baomidou.com/pages/4c6bcf/ |
| | | * ISqlInjector sql注入器 |
| | | * https://baomidou.com/pages/42ea4a/ |
| | | * BlockAttackInnerInterceptor 如果是对全表的删除或更新操作,就会终止该操作 |
| | | * https://baomidou.com/pages/f9a237/ |
| | | * IllegalSQLInnerInterceptor sql性能规范插件(垃圾SQL拦截) |
| | | * IdentifierGenerator 自定义主键策略 |
| | | * https://baomidou.com/pages/568eb2/ |
| | | * TenantLineInnerInterceptor 多租户插件 |
| | | * https://baomidou.com/pages/aef2f2/ |
| | | * DynamicTableNameInnerInterceptor 动态表名插件 |
| | | * https://baomidou.com/pages/2a45ff/ |
| | | */ |
| | | |
| | | @Bean |
| | | public DdlApplicationRunner ddlApplicationRunner(@Autowired(required = false) List<IDdl> ddlList) { |
| | | return new DdlApplicationRunner(ddlList); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.core.domain; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.FieldFill; |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.fasterxml.jackson.annotation.JsonIgnore; |
| | | import com.fasterxml.jackson.annotation.JsonInclude; |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Entity基类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | |
| | | @Data |
| | | public class BaseEntity implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 搜索值 |
| | | */ |
| | | @JsonIgnore |
| | | @TableField(exist = false) |
| | | private String searchValue; |
| | | |
| | | /** |
| | | * 创建部门 |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT) |
| | | private Long createDept; |
| | | |
| | | /** |
| | | * 创建者 |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT) |
| | | private Long createBy; |
| | | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT) |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 更新者 |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT_UPDATE) |
| | | private Long updateBy; |
| | | |
| | | /** |
| | | * 更新时间 |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT_UPDATE) |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * 请求参数 |
| | | */ |
| | | @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| | | @TableField(exist = false) |
| | | private Map<String, Object> params = new HashMap<>(); |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.core.mapper; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.toolkit.Db; |
| | | import org.apache.ibatis.logging.Log; |
| | | import org.apache.ibatis.logging.LogFactory; |
| | | import org.dromara.common.core.utils.MapstructUtils; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 自定义 Mapper 接口, 实现 自定义扩展 |
| | | * |
| | | * @param <T> table 泛型 |
| | | * @param <V> vo 泛型 |
| | | * @author Lion Li |
| | | * @since 2021-05-13 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public interface BaseMapperPlus<T, V> extends BaseMapper<T> { |
| | | |
| | | Log log = LogFactory.getLog(BaseMapperPlus.class); |
| | | |
| | | default Class<V> currentVoClass() { |
| | | return (Class<V>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperPlus.class, 1); |
| | | } |
| | | |
| | | default Class<T> currentModelClass() { |
| | | return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperPlus.class, 0); |
| | | } |
| | | |
| | | default List<T> selectList() { |
| | | return this.selectList(new QueryWrapper<>()); |
| | | } |
| | | |
| | | /** |
| | | * 批量插入 |
| | | */ |
| | | default boolean insertBatch(Collection<T> entityList) { |
| | | return Db.saveBatch(entityList); |
| | | } |
| | | |
| | | /** |
| | | * 批量更新 |
| | | */ |
| | | default boolean updateBatchById(Collection<T> entityList) { |
| | | return Db.updateBatchById(entityList); |
| | | } |
| | | |
| | | /** |
| | | * 批量插入或更新 |
| | | */ |
| | | default boolean insertOrUpdateBatch(Collection<T> entityList) { |
| | | return Db.saveOrUpdateBatch(entityList); |
| | | } |
| | | |
| | | /** |
| | | * 批量插入(包含限制条数) |
| | | */ |
| | | default boolean insertBatch(Collection<T> entityList, int batchSize) { |
| | | return Db.saveBatch(entityList, batchSize); |
| | | } |
| | | |
| | | /** |
| | | * 批量更新(包含限制条数) |
| | | */ |
| | | default boolean updateBatchById(Collection<T> entityList, int batchSize) { |
| | | return Db.updateBatchById(entityList, batchSize); |
| | | } |
| | | |
| | | /** |
| | | * 批量插入或更新(包含限制条数) |
| | | */ |
| | | default boolean insertOrUpdateBatch(Collection<T> entityList, int batchSize) { |
| | | return Db.saveOrUpdateBatch(entityList, batchSize); |
| | | } |
| | | |
| | | /** |
| | | * 插入或更新(包含限制条数) |
| | | */ |
| | | default boolean insertOrUpdate(T entity) { |
| | | return Db.saveOrUpdate(entity); |
| | | } |
| | | |
| | | default V selectVoById(Serializable id) { |
| | | return selectVoById(id, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 根据 ID 查询 |
| | | */ |
| | | default <C> C selectVoById(Serializable id, Class<C> voClass) { |
| | | T obj = this.selectById(id); |
| | | if (ObjectUtil.isNull(obj)) { |
| | | return null; |
| | | } |
| | | return MapstructUtils.convert(obj, voClass); |
| | | } |
| | | |
| | | default List<V> selectVoBatchIds(Collection<? extends Serializable> idList) { |
| | | return selectVoBatchIds(idList, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 查询(根据ID 批量查询) |
| | | */ |
| | | default <C> List<C> selectVoBatchIds(Collection<? extends Serializable> idList, Class<C> voClass) { |
| | | List<T> list = this.selectBatchIds(idList); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return MapstructUtils.convert(list, voClass); |
| | | } |
| | | |
| | | default List<V> selectVoByMap(Map<String, Object> map) { |
| | | return selectVoByMap(map, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 查询(根据 columnMap 条件) |
| | | */ |
| | | default <C> List<C> selectVoByMap(Map<String, Object> map, Class<C> voClass) { |
| | | List<T> list = this.selectByMap(map); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return MapstructUtils.convert(list, voClass); |
| | | } |
| | | |
| | | default V selectVoOne(Wrapper<T> wrapper) { |
| | | return selectVoOne(wrapper, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 根据 entity 条件,查询一条记录 |
| | | */ |
| | | default <C> C selectVoOne(Wrapper<T> wrapper, Class<C> voClass) { |
| | | T obj = this.selectOne(wrapper); |
| | | if (ObjectUtil.isNull(obj)) { |
| | | return null; |
| | | } |
| | | return MapstructUtils.convert(obj, voClass); |
| | | } |
| | | |
| | | default List<V> selectVoList() { |
| | | return selectVoList(new QueryWrapper<>(), this.currentVoClass()); |
| | | } |
| | | |
| | | default List<V> selectVoList(Wrapper<T> wrapper) { |
| | | return selectVoList(wrapper, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 根据 entity 条件,查询全部记录 |
| | | */ |
| | | default <C> List<C> selectVoList(Wrapper<T> wrapper, Class<C> voClass) { |
| | | List<T> list = this.selectList(wrapper); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return MapstructUtils.convert(list, voClass); |
| | | } |
| | | |
| | | default <P extends IPage<V>> P selectVoPage(IPage<T> page, Wrapper<T> wrapper) { |
| | | return selectVoPage(page, wrapper, this.currentVoClass()); |
| | | } |
| | | |
| | | /** |
| | | * 分页查询VO |
| | | */ |
| | | default <C, P extends IPage<C>> P selectVoPage(IPage<T> page, Wrapper<T> wrapper, Class<C> voClass) { |
| | | List<T> list = this.selectList(page, wrapper); |
| | | IPage<C> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal()); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return (P) voPage; |
| | | } |
| | | voPage.setRecords(MapstructUtils.convert(list, voClass)); |
| | | return (P) voPage; |
| | | } |
| | | |
| | | default <C> List<C> selectObjs(Wrapper<T> wrapper, Function<? super Object, C> mapper) { |
| | | return this.selectObjs(wrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList()); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.core.page; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.OrderItem; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.sql.SqlUtil; |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 分页查询实体类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | |
| | | @Data |
| | | public class PageQuery implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 分页大小 |
| | | */ |
| | | private Integer pageSize; |
| | | |
| | | /** |
| | | * 当前页数 |
| | | */ |
| | | private Integer pageNum; |
| | | |
| | | /** |
| | | * 排序列 |
| | | */ |
| | | private String orderByColumn; |
| | | |
| | | /** |
| | | * 排序的方向desc或者asc |
| | | */ |
| | | private String isAsc; |
| | | |
| | | /** |
| | | * 当前记录起始索引 默认值 |
| | | */ |
| | | public static final int DEFAULT_PAGE_NUM = 1; |
| | | |
| | | /** |
| | | * 每页显示记录数 默认值 默认查全部 |
| | | */ |
| | | public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE; |
| | | |
| | | public <T> Page<T> build() { |
| | | Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM); |
| | | Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE); |
| | | if (pageNum <= 0) { |
| | | pageNum = DEFAULT_PAGE_NUM; |
| | | } |
| | | Page<T> page = new Page<>(pageNum, pageSize); |
| | | List<OrderItem> orderItems = buildOrderItem(); |
| | | if (CollUtil.isNotEmpty(orderItems)) { |
| | | page.addOrder(orderItems); |
| | | } |
| | | return page; |
| | | } |
| | | |
| | | /** |
| | | * 构建排序 |
| | | * |
| | | * 支持的用法如下: |
| | | * {isAsc:"asc",orderByColumn:"id"} order by id asc |
| | | * {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc |
| | | * {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc |
| | | * {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc |
| | | */ |
| | | private List<OrderItem> buildOrderItem() { |
| | | if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) { |
| | | return null; |
| | | } |
| | | String orderBy = SqlUtil.escapeOrderBySql(orderByColumn); |
| | | orderBy = StringUtils.toUnderScoreCase(orderBy); |
| | | |
| | | // 兼容前端排序类型 |
| | | isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"}); |
| | | |
| | | String[] orderByArr = orderBy.split(StringUtils.SEPARATOR); |
| | | String[] isAscArr = isAsc.split(StringUtils.SEPARATOR); |
| | | if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) { |
| | | throw new ServiceException("排序参数有误"); |
| | | } |
| | | |
| | | List<OrderItem> list = new ArrayList<>(); |
| | | // 每个字段各自排序 |
| | | for (int i = 0; i < orderByArr.length; i++) { |
| | | String orderByStr = orderByArr[i]; |
| | | String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i]; |
| | | if ("asc".equals(isAscStr)) { |
| | | list.add(OrderItem.asc(orderByStr)); |
| | | } else if ("desc".equals(isAscStr)) { |
| | | list.add(OrderItem.desc(orderByStr)); |
| | | } else { |
| | | throw new ServiceException("排序参数有误"); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.core.page; |
| | | |
| | | import cn.hutool.http.HttpStatus; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 表格分页数据对象 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class TableDataInfo<T> implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 总记录数 |
| | | */ |
| | | private long total; |
| | | |
| | | /** |
| | | * 列表数据 |
| | | */ |
| | | private List<T> rows; |
| | | |
| | | /** |
| | | * 消息状态码 |
| | | */ |
| | | private int code; |
| | | |
| | | /** |
| | | * 消息内容 |
| | | */ |
| | | private String msg; |
| | | |
| | | /** |
| | | * 分页 |
| | | * |
| | | * @param list 列表数据 |
| | | * @param total 总记录数 |
| | | */ |
| | | public TableDataInfo(List<T> list, long total) { |
| | | this.rows = list; |
| | | this.total = total; |
| | | } |
| | | |
| | | public static <T> TableDataInfo<T> build(IPage<T> page) { |
| | | TableDataInfo<T> rspData = new TableDataInfo<>(); |
| | | rspData.setCode(HttpStatus.HTTP_OK); |
| | | rspData.setMsg("查询成功"); |
| | | rspData.setRows(page.getRecords()); |
| | | rspData.setTotal(page.getTotal()); |
| | | return rspData; |
| | | } |
| | | |
| | | public static <T> TableDataInfo<T> build(List<T> list) { |
| | | TableDataInfo<T> rspData = new TableDataInfo<>(); |
| | | rspData.setCode(HttpStatus.HTTP_OK); |
| | | rspData.setMsg("查询成功"); |
| | | rspData.setRows(list); |
| | | rspData.setTotal(list.size()); |
| | | return rspData; |
| | | } |
| | | |
| | | public static <T> TableDataInfo<T> build() { |
| | | TableDataInfo<T> rspData = new TableDataInfo<>(); |
| | | rspData.setCode(HttpStatus.HTTP_OK); |
| | | rspData.setMsg("查询成功"); |
| | | return rspData; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.enums; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 数据库类型 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum DataBaseType { |
| | | |
| | | /** |
| | | * MySQL |
| | | */ |
| | | MY_SQL("MySQL"), |
| | | |
| | | /** |
| | | * Oracle |
| | | */ |
| | | ORACLE("Oracle"), |
| | | |
| | | /** |
| | | * PostgreSQL |
| | | */ |
| | | POSTGRE_SQL("PostgreSQL"), |
| | | |
| | | /** |
| | | * SQL Server |
| | | */ |
| | | SQL_SERVER("Microsoft SQL Server"); |
| | | |
| | | private final String type; |
| | | |
| | | public static DataBaseType find(String databaseProductName) { |
| | | if (StringUtils.isBlank(databaseProductName)) { |
| | | return null; |
| | | } |
| | | for (DataBaseType type : values()) { |
| | | if (type.getType().equals(databaseProductName)) { |
| | | return type; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.enums; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | |
| | | /** |
| | | * 数据权限类型 |
| | | * <p> |
| | | * 语法支持 spel 模板表达式 |
| | | * <p> |
| | | * 内置数据 user 当前用户 内容参考 LoginUser |
| | | * 如需扩展数据 可使用 {@link DataPermissionHelper} 操作 |
| | | * 内置服务 sdss 系统数据权限服务 内容参考 SysDataScopeService |
| | | * 如需扩展更多自定义服务 可以参考 sdss 自行编写 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum DataScopeType { |
| | | |
| | | /** |
| | | * 全部数据权限 |
| | | */ |
| | | ALL("1", "", ""), |
| | | |
| | | /** |
| | | * 自定数据权限 |
| | | */ |
| | | CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) ", " 1 = 0 "), |
| | | |
| | | /** |
| | | * 部门数据权限 |
| | | */ |
| | | DEPT("3", " #{#deptName} = #{#user.deptId} ", " 1 = 0 "), |
| | | |
| | | /** |
| | | * 部门及以下数据权限 |
| | | */ |
| | | DEPT_AND_CHILD("4", " #{#deptName} IN ( #{@sdss.getDeptAndChild( #user.deptId )} )", " 1 = 0 "), |
| | | |
| | | /** |
| | | * 仅本人数据权限 |
| | | */ |
| | | SELF("5", " #{#userName} = #{#user.userId} ", " 1 = 0 "); |
| | | |
| | | private final String code; |
| | | |
| | | /** |
| | | * 语法 采用 spel 模板表达式 |
| | | */ |
| | | private final String sqlTemplate; |
| | | |
| | | /** |
| | | * 不满足 sqlTemplate 则填充 |
| | | */ |
| | | private final String elseSql; |
| | | |
| | | public static DataScopeType findCode(String code) { |
| | | if (StringUtils.isBlank(code)) { |
| | | return null; |
| | | } |
| | | for (DataScopeType type : values()) { |
| | | if (type.getCode().equals(code)) { |
| | | return type; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.filter; |
| | | |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.dubbo.common.constants.CommonConstants; |
| | | import org.apache.dubbo.common.extension.Activate; |
| | | import org.apache.dubbo.rpc.*; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * dubbo 数据权限参数传递 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @Activate(group = {CommonConstants.CONSUMER}) |
| | | public class DubboDataPermissionFilter implements Filter { |
| | | |
| | | @Override |
| | | public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { |
| | | RpcServiceContext context = RpcContext.getServiceContext(); |
| | | Map<String, Object> dataPermissionContext = DataPermissionHelper.getContext(); |
| | | context.setObjectAttachment(DataPermissionHelper.DATA_PERMISSION_KEY, dataPermissionContext); |
| | | return invoker.invoke(invocation); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.handler; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpStatus; |
| | | import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.reflection.MetaObject; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.mybatis.core.domain.BaseEntity; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * MP注入处理器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class InjectionMetaObjectHandler implements MetaObjectHandler { |
| | | |
| | | @Override |
| | | public void insertFill(MetaObject metaObject) { |
| | | try { |
| | | if (ObjectUtil.isNotNull(metaObject) |
| | | && metaObject.getOriginalObject() instanceof BaseEntity baseEntity) { |
| | | Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) |
| | | ? baseEntity.getCreateTime() : new Date(); |
| | | baseEntity.setCreateTime(current); |
| | | baseEntity.setUpdateTime(current); |
| | | LoginUser loginUser = getLoginUser(); |
| | | if (ObjectUtil.isNotNull(loginUser)) { |
| | | Long userId = ObjectUtil.isNotNull(baseEntity.getCreateBy()) |
| | | ? baseEntity.getCreateBy() : loginUser.getUserId(); |
| | | // 当前已登录 且 创建人为空 则填充 |
| | | baseEntity.setCreateBy(userId); |
| | | // 当前已登录 且 更新人为空 则填充 |
| | | baseEntity.setUpdateBy(userId); |
| | | baseEntity.setCreateDept(ObjectUtil.isNotNull(baseEntity.getCreateDept()) |
| | | ? baseEntity.getCreateDept() : loginUser.getDeptId()); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateFill(MetaObject metaObject) { |
| | | try { |
| | | if (ObjectUtil.isNotNull(metaObject) |
| | | && metaObject.getOriginalObject() instanceof BaseEntity baseEntity) { |
| | | Date current = new Date(); |
| | | // 更新时间填充(不管为不为空) |
| | | baseEntity.setUpdateTime(current); |
| | | LoginUser loginUser = getLoginUser(); |
| | | // 当前已登录 更新人填充(不管为不为空) |
| | | if (ObjectUtil.isNotNull(loginUser)) { |
| | | baseEntity.setUpdateBy(loginUser.getUserId()); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取登录用户 |
| | | */ |
| | | private LoginUser getLoginUser() { |
| | | LoginUser loginUser; |
| | | try { |
| | | loginUser = LoginHelper.getLoginUser(); |
| | | } catch (Exception e) { |
| | | log.warn("自动注入警告 => 用户未登录"); |
| | | return null; |
| | | } |
| | | return loginUser; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.handler; |
| | | |
| | | import org.dromara.common.core.domain.R; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.mybatis.spring.MyBatisSystemException; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | |
| | | /** |
| | | * Mybatis异常处理器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RestControllerAdvice |
| | | public class MybatisExceptionHandler { |
| | | |
| | | /** |
| | | * 主键或UNIQUE索引,数据重复异常 |
| | | */ |
| | | @ExceptionHandler(DuplicateKeyException.class) |
| | | public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage()); |
| | | return R.fail("数据库中已存在该记录,请联系管理员确认"); |
| | | } |
| | | |
| | | /** |
| | | * Mybatis系统异常 通用处理 |
| | | */ |
| | | @ExceptionHandler(MyBatisSystemException.class) |
| | | public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | String message = e.getMessage(); |
| | | if ("CannotFindDataSourceException".contains(message)) { |
| | | log.error("请求地址'{}', 未找到数据源", requestURI); |
| | | return R.fail("未找到数据源,请联系管理员确认"); |
| | | } |
| | | log.error("请求地址'{}', Mybatis系统异常", requestURI, e); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.handler; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ClassUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.JSQLParserException; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.Parenthesis; |
| | | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| | | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.mybatis.annotation.DataColumn; |
| | | import org.dromara.common.mybatis.annotation.DataPermission; |
| | | import org.dromara.common.mybatis.enums.DataScopeType; |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | import org.dromara.system.api.model.RoleDTO; |
| | | import org.springframework.context.expression.BeanFactoryResolver; |
| | | import org.springframework.expression.BeanResolver; |
| | | import org.springframework.expression.ExpressionParser; |
| | | import org.springframework.expression.ParserContext; |
| | | import org.springframework.expression.common.TemplateParserContext; |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * 数据权限过滤 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | @Slf4j |
| | | public class PlusDataPermissionHandler { |
| | | |
| | | /** |
| | | * 方法或类(名称) 与 注解的映射关系缓存 |
| | | */ |
| | | private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * spel 解析器 |
| | | */ |
| | | private final ExpressionParser parser = new SpelExpressionParser(); |
| | | private final ParserContext parserContext = new TemplateParserContext(); |
| | | /** |
| | | * bean解析器 用于处理 spel 表达式中对 bean 的调用 |
| | | */ |
| | | private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory()); |
| | | |
| | | |
| | | public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) { |
| | | DataColumn[] dataColumns = findAnnotation(mappedStatementId); |
| | | LoginUser currentUser = DataPermissionHelper.getVariable("user"); |
| | | if (ObjectUtil.isNull(currentUser)) { |
| | | currentUser = LoginHelper.getLoginUser(); |
| | | DataPermissionHelper.setVariable("user", currentUser); |
| | | } |
| | | // 如果是超级管理员或租户管理员,则不过滤数据 |
| | | if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) { |
| | | return where; |
| | | } |
| | | String dataFilterSql = buildDataFilter(dataColumns, isSelect); |
| | | if (StringUtils.isBlank(dataFilterSql)) { |
| | | return where; |
| | | } |
| | | try { |
| | | Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql); |
| | | // 数据权限使用单独的括号 防止与其他条件冲突 |
| | | Parenthesis parenthesis = new Parenthesis(expression); |
| | | if (ObjectUtil.isNotNull(where)) { |
| | | return new AndExpression(where, parenthesis); |
| | | } else { |
| | | return parenthesis; |
| | | } |
| | | } catch (JSQLParserException e) { |
| | | throw new ServiceException("数据权限解析异常 => " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 构造数据过滤sql |
| | | */ |
| | | private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) { |
| | | // 更新或删除需满足所有条件 |
| | | String joinStr = isSelect ? " OR " : " AND "; |
| | | LoginUser user = DataPermissionHelper.getVariable("user"); |
| | | StandardEvaluationContext context = new StandardEvaluationContext(); |
| | | context.setBeanResolver(beanResolver); |
| | | DataPermissionHelper.getContext().forEach(context::setVariable); |
| | | Set<String> conditions = new HashSet<>(); |
| | | for (RoleDTO role : user.getRoles()) { |
| | | user.setRoleId(role.getRoleId()); |
| | | // 获取角色权限泛型 |
| | | DataScopeType type = DataScopeType.findCode(role.getDataScope()); |
| | | if (ObjectUtil.isNull(type)) { |
| | | throw new ServiceException("角色数据范围异常 => " + role.getDataScope()); |
| | | } |
| | | // 全部数据权限直接返回 |
| | | if (type == DataScopeType.ALL) { |
| | | return ""; |
| | | } |
| | | boolean isSuccess = false; |
| | | for (DataColumn dataColumn : dataColumns) { |
| | | if (dataColumn.key().length != dataColumn.value().length) { |
| | | throw new ServiceException("角色数据范围异常 => key与value长度不匹配"); |
| | | } |
| | | // 不包含 key 变量 则不处理 |
| | | if (!StringUtils.containsAny(type.getSqlTemplate(), |
| | | Arrays.stream(dataColumn.key()).map(key -> "#" + key).toArray(String[]::new) |
| | | )) { |
| | | continue; |
| | | } |
| | | // 设置注解变量 key 为表达式变量 value 为变量值 |
| | | for (int i = 0; i < dataColumn.key().length; i++) { |
| | | context.setVariable(dataColumn.key()[i], dataColumn.value()[i]); |
| | | } |
| | | |
| | | // 解析sql模板并填充 |
| | | String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class); |
| | | conditions.add(joinStr + sql); |
| | | isSuccess = true; |
| | | } |
| | | // 未处理成功则填充兜底方案 |
| | | if (!isSuccess && StringUtils.isNotBlank(type.getElseSql())) { |
| | | conditions.add(joinStr + type.getElseSql()); |
| | | } |
| | | } |
| | | |
| | | if (CollUtil.isNotEmpty(conditions)) { |
| | | String sql = StreamUtils.join(conditions, Function.identity(), ""); |
| | | return sql.substring(joinStr.length()); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | public DataColumn[] findAnnotation(String mappedStatementId) { |
| | | StringBuilder sb = new StringBuilder(mappedStatementId); |
| | | int index = sb.lastIndexOf("."); |
| | | String clazzName = sb.substring(0, index); |
| | | String methodName = sb.substring(index + 1, sb.length()); |
| | | Class<?> clazz; |
| | | try { |
| | | clazz = ClassUtil.loadClass(clazzName); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz)) |
| | | .filter(method -> method.getName().equals(methodName)).toList(); |
| | | DataPermission dataPermission; |
| | | // 获取方法注解 |
| | | for (Method method : methods) { |
| | | dataPermission = dataPermissionCacheMap.get(mappedStatementId); |
| | | if (ObjectUtil.isNotNull(dataPermission)) { |
| | | return dataPermission.value(); |
| | | } |
| | | if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class); |
| | | dataPermissionCacheMap.put(mappedStatementId, dataPermission); |
| | | return dataPermission.value(); |
| | | } |
| | | } |
| | | dataPermission = dataPermissionCacheMap.get(clazz.getName()); |
| | | if (ObjectUtil.isNotNull(dataPermission)) { |
| | | return dataPermission.value(); |
| | | } |
| | | // 获取类注解 |
| | | if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class); |
| | | dataPermissionCacheMap.put(clazz.getName(), dataPermission); |
| | | return dataPermission.value(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.helper; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.mybatis.enums.DataBaseType; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.sql.Connection; |
| | | import java.sql.DatabaseMetaData; |
| | | import java.sql.SQLException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 数据库助手 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class DataBaseHelper { |
| | | |
| | | private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class); |
| | | |
| | | /** |
| | | * 获取当前数据库类型 |
| | | */ |
| | | public static DataBaseType getDataBaseType() { |
| | | DataSource dataSource = DS.determineDataSource(); |
| | | try (Connection conn = dataSource.getConnection()) { |
| | | DatabaseMetaData metaData = conn.getMetaData(); |
| | | String databaseProductName = metaData.getDatabaseProductName(); |
| | | return DataBaseType.find(databaseProductName); |
| | | } catch (SQLException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | public static boolean isMySql() { |
| | | return DataBaseType.MY_SQL == getDataBaseType(); |
| | | } |
| | | |
| | | public static boolean isOracle() { |
| | | return DataBaseType.ORACLE == getDataBaseType(); |
| | | } |
| | | |
| | | public static boolean isPostgerSql() { |
| | | return DataBaseType.POSTGRE_SQL == getDataBaseType(); |
| | | } |
| | | |
| | | public static boolean isSqlServer() { |
| | | return DataBaseType.SQL_SERVER == getDataBaseType(); |
| | | } |
| | | |
| | | public static String findInSet(Object var1, String var2) { |
| | | DataBaseType dataBasyType = getDataBaseType(); |
| | | String var = Convert.toStr(var1); |
| | | if (dataBasyType == DataBaseType.SQL_SERVER) { |
| | | // charindex(',100,' , ',0,100,101,') <> 0 |
| | | return "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2); |
| | | } else if (dataBasyType == DataBaseType.POSTGRE_SQL) { |
| | | // (select position(',100,' in ',0,100,101,')) <> 0 |
| | | return "(select position(',%s,' in ','||%s||',')) <> 0".formatted(var, var2); |
| | | } else if (dataBasyType == DataBaseType.ORACLE) { |
| | | // instr(',0,100,101,' , ',100,') <> 0 |
| | | return "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var); |
| | | } |
| | | // find_in_set('100' , '0,100,101') |
| | | return "find_in_set('%s' , %s) <> 0".formatted(var, var2); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前加载的数据库名 |
| | | */ |
| | | public static List<String> getDataSourceNameList() { |
| | | return new ArrayList<>(DS.getDataSources().keySet()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.helper; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.context.model.SaStorage; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; |
| | | import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | | * 数据权限助手 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | @SuppressWarnings("unchecked cast") |
| | | public class DataPermissionHelper { |
| | | |
| | | public static final String DATA_PERMISSION_KEY = "data:permission"; |
| | | |
| | | public static <T> T getVariable(String key) { |
| | | Map<String, Object> context = getContext(); |
| | | return (T) context.get(key); |
| | | } |
| | | |
| | | |
| | | public static void setVariable(String key, Object value) { |
| | | Map<String, Object> context = getContext(); |
| | | context.put(key, value); |
| | | } |
| | | |
| | | public static Map<String, Object> getContext() { |
| | | SaStorage saStorage = SaHolder.getStorage(); |
| | | Object attribute = saStorage.get(DATA_PERMISSION_KEY); |
| | | if (ObjectUtil.isNull(attribute)) { |
| | | saStorage.set(DATA_PERMISSION_KEY, new HashMap<>()); |
| | | attribute = saStorage.get(DATA_PERMISSION_KEY); |
| | | } |
| | | if (attribute instanceof Map map) { |
| | | return map; |
| | | } |
| | | throw new NullPointerException("data permission context type exception"); |
| | | } |
| | | |
| | | /** |
| | | * 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭) |
| | | */ |
| | | public static void enableIgnore() { |
| | | InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); |
| | | } |
| | | |
| | | /** |
| | | * 关闭忽略数据权限 |
| | | */ |
| | | public static void disableIgnore() { |
| | | InterceptorIgnoreHelper.clearIgnoreStrategy(); |
| | | } |
| | | |
| | | /** |
| | | * 在忽略数据权限中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static void ignore(Runnable handle) { |
| | | enableIgnore(); |
| | | try { |
| | | handle.run(); |
| | | } finally { |
| | | disableIgnore(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 在忽略数据权限中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static <T> T ignore(Supplier<T> handle) { |
| | | enableIgnore(); |
| | | try { |
| | | return handle.get(); |
| | | } finally { |
| | | disableIgnore(); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.mybatis.interceptor; |
| | | |
| | | import cn.hutool.core.collection.ConcurrentHashSet; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
| | | import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
| | | import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
| | | import org.dromara.common.mybatis.annotation.DataColumn; |
| | | import org.dromara.common.mybatis.handler.PlusDataPermissionHandler; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.statement.delete.Delete; |
| | | import net.sf.jsqlparser.statement.select.PlainSelect; |
| | | import net.sf.jsqlparser.statement.select.Select; |
| | | import net.sf.jsqlparser.statement.select.SelectBody; |
| | | import net.sf.jsqlparser.statement.select.SetOperationList; |
| | | import net.sf.jsqlparser.statement.update.Update; |
| | | import org.apache.ibatis.executor.Executor; |
| | | import org.apache.ibatis.executor.statement.StatementHandler; |
| | | import org.apache.ibatis.mapping.BoundSql; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlCommandType; |
| | | import org.apache.ibatis.session.ResultHandler; |
| | | import org.apache.ibatis.session.RowBounds; |
| | | |
| | | import java.sql.Connection; |
| | | import java.sql.SQLException; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 数据权限拦截器 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.5.0 |
| | | */ |
| | | public class PlusDataPermissionInterceptor extends JsqlParserSupport implements InnerInterceptor { |
| | | |
| | | private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler(); |
| | | /** |
| | | * 无效注解方法缓存用于快速返回 |
| | | */ |
| | | private final Set<String> invalidCacheSet = new ConcurrentHashSet<>(); |
| | | |
| | | @Override |
| | | public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
| | | // 检查忽略注解 |
| | | if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
| | | return; |
| | | } |
| | | // 检查是否无效 无数据权限注解 |
| | | if (invalidCacheSet.contains(ms.getId())) { |
| | | return; |
| | | } |
| | | DataColumn[] dataColumns = dataPermissionHandler.findAnnotation(ms.getId()); |
| | | if (ArrayUtil.isEmpty(dataColumns)) { |
| | | invalidCacheSet.add(ms.getId()); |
| | | return; |
| | | } |
| | | // 解析 sql 分配对应方法 |
| | | PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
| | | mpBs.sql(parserSingle(mpBs.sql(), ms.getId())); |
| | | } |
| | | |
| | | @Override |
| | | public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { |
| | | PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); |
| | | MappedStatement ms = mpSh.mappedStatement(); |
| | | SqlCommandType sct = ms.getSqlCommandType(); |
| | | if (sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { |
| | | if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
| | | return; |
| | | } |
| | | // 检查是否无效 无数据权限注解 |
| | | if (invalidCacheSet.contains(ms.getId())) { |
| | | return; |
| | | } |
| | | DataColumn[] dataColumns = dataPermissionHandler.findAnnotation(ms.getId()); |
| | | if (ArrayUtil.isEmpty(dataColumns)) { |
| | | invalidCacheSet.add(ms.getId()); |
| | | return; |
| | | } |
| | | PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); |
| | | mpBs.sql(parserMulti(mpBs.sql(), ms.getId())); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processSelect(Select select, int index, String sql, Object obj) { |
| | | SelectBody selectBody = select.getSelectBody(); |
| | | if (selectBody instanceof PlainSelect plainSelect) { |
| | | this.setWhere(plainSelect, (String) obj); |
| | | } else if (selectBody instanceof SetOperationList setOperationList) { |
| | | List<SelectBody> selectBodyList = setOperationList.getSelects(); |
| | | selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, (String) obj)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processUpdate(Update update, int index, String sql, Object obj) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), (String) obj, false); |
| | | if (null != sqlSegment) { |
| | | update.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processDelete(Delete delete, int index, String sql, Object obj) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), (String) obj, false); |
| | | if (null != sqlSegment) { |
| | | delete.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置 where 条件 |
| | | * |
| | | * @param plainSelect 查询对象 |
| | | * @param mappedStatementId 执行方法id |
| | | */ |
| | | protected void setWhere(PlainSelect plainSelect, String mappedStatementId) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), mappedStatementId, true); |
| | | if (null != sqlSegment) { |
| | | plainSelect.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package org.dromara.common.mybatis.service; |
| | | |
| | | import org.dromara.system.api.RemoteDataScopeService; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * 数据权限 实现 |
| | | * <p> |
| | | * 注意: 此Service内不允许调用标注`数据权限`注解的方法 |
| | | * 例如: deptMapper.selectList 此 selectList 方法标注了`数据权限`注解 会出现循环解析的问题 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Service("sdss") |
| | | public class SysDataScopeService { |
| | | |
| | | @DubboReference |
| | | private RemoteDataScopeService remoteDataScopeService; |
| | | |
| | | public String getRoleCustom(Long roleId) { |
| | | return remoteDataScopeService.getRoleCustom(roleId); |
| | | } |
| | | |
| | | public String getDeptAndChild(Long deptId) { |
| | | return remoteDataScopeService.getDeptAndChild(deptId); |
| | | } |
| | | } |
New file |
| | |
| | | dubboDataPermissionFilter=org.dromara.common.mybatis.filter.DubboDataPermissionFilter |
New file |
| | |
| | | org.dromara.common.mybatis.config.MybatisPlusConfiguration |
| | | org.dromara.common.mybatis.handler.MybatisExceptionHandler |
| | | org.dromara.common.mybatis.service.SysDataScopeService |
New file |
| | |
| | | # 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖 |
| | | # MyBatisPlus配置 |
| | | # https://baomidou.com/config/ |
| | | mybatis-plus: |
| | | # 启动时是否检查 MyBatis XML 文件的存在,默认不检查 |
| | | checkConfigLocation: false |
| | | configuration: |
| | | # 自动驼峰命名规则(camel case)映射 |
| | | mapUnderscoreToCamelCase: true |
| | | # MyBatis 自动映射策略 |
| | | # NONE:不启用 PARTIAL:只对非嵌套 resultMap 自动映射 FULL:对所有 resultMap 自动映射 |
| | | autoMappingBehavior: FULL |
| | | # MyBatis 自动映射时未知列或未知属性处理策 |
| | | # NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息 |
| | | autoMappingUnknownColumnBehavior: NONE |
| | | # 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl |
| | | # 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl |
| | | # 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpl |
| | | logImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl |
| | | global-config: |
| | | # 是否打印 Logo banner |
| | | banner: true |
| | | dbConfig: |
| | | # 主键类型 |
| | | # AUTO 自增 NONE 空 INPUT 用户输入 ASSIGN_ID 雪花 ASSIGN_UUID 唯一 UUID |
| | | idType: ASSIGN_ID |
| | | # 逻辑已删除值(框架表均使用此值 禁止随意修改) |
| | | logicDeleteValue: 2 |
| | | # 逻辑未删除值 |
| | | logicNotDeleteValue: 0 |
| | | insertStrategy: NOT_NULL |
| | | updateStrategy: NOT_NULL |
| | | whereStrategy: NOT_NULL |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-oss</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-oss oss服务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.amazonaws</groupId> |
| | | <artifactId>aws-java-sdk-s3</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.oss.constant; |
| | | |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 对象存储常量 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface OssConstant { |
| | | |
| | | /** |
| | | * 默认配置KEY |
| | | */ |
| | | String DEFAULT_CONFIG_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "sys_oss:default_config"; |
| | | |
| | | /** |
| | | * 预览列表资源开关Key |
| | | */ |
| | | String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource"; |
| | | |
| | | /** |
| | | * 系统数据ids |
| | | */ |
| | | List<Long> SYSTEM_DATA_IDS = Arrays.asList(1L, 2L, 3L, 4L); |
| | | |
| | | /** |
| | | * 云服务商 |
| | | */ |
| | | String[] CLOUD_SERVICE = new String[] {"aliyun", "qcloud", "qiniu", "obs"}; |
| | | |
| | | /** |
| | | * https 状态 |
| | | */ |
| | | String IS_HTTPS = "Y"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.core; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.amazonaws.ClientConfiguration; |
| | | import com.amazonaws.HttpMethod; |
| | | import com.amazonaws.Protocol; |
| | | import com.amazonaws.auth.AWSCredentials; |
| | | import com.amazonaws.auth.AWSCredentialsProvider; |
| | | import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| | | import com.amazonaws.auth.BasicAWSCredentials; |
| | | import com.amazonaws.client.builder.AwsClientBuilder; |
| | | import com.amazonaws.services.s3.AmazonS3; |
| | | import com.amazonaws.services.s3.AmazonS3Client; |
| | | import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| | | import com.amazonaws.services.s3.model.*; |
| | | import org.dromara.common.core.utils.DateUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.oss.constant.OssConstant; |
| | | import org.dromara.common.oss.entity.UploadResult; |
| | | import org.dromara.common.oss.enumd.AccessPolicyType; |
| | | import org.dromara.common.oss.enumd.PolicyType; |
| | | import org.dromara.common.oss.exception.OssException; |
| | | import org.dromara.common.oss.properties.OssProperties; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * S3 存储协议 所有兼容S3协议的云厂商均支持 |
| | | * 阿里云 腾讯云 七牛云 minio |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class OssClient { |
| | | |
| | | private final String configKey; |
| | | |
| | | private final OssProperties properties; |
| | | |
| | | private final AmazonS3 client; |
| | | |
| | | public OssClient(String configKey, OssProperties ossProperties) { |
| | | this.configKey = configKey; |
| | | this.properties = ossProperties; |
| | | try { |
| | | AwsClientBuilder.EndpointConfiguration endpointConfig = |
| | | new AwsClientBuilder.EndpointConfiguration(properties.getEndpoint(), properties.getRegion()); |
| | | |
| | | AWSCredentials credentials = new BasicAWSCredentials(properties.getAccessKey(), properties.getSecretKey()); |
| | | AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials); |
| | | ClientConfiguration clientConfig = new ClientConfiguration(); |
| | | if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) { |
| | | clientConfig.setProtocol(Protocol.HTTPS); |
| | | } else { |
| | | clientConfig.setProtocol(Protocol.HTTP); |
| | | } |
| | | AmazonS3ClientBuilder build = AmazonS3Client.builder() |
| | | .withEndpointConfiguration(endpointConfig) |
| | | .withClientConfiguration(clientConfig) |
| | | .withCredentials(credentialsProvider) |
| | | .disableChunkedEncoding(); |
| | | if (!StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE)) { |
| | | // minio 使用https限制使用域名访问 需要此配置 站点填域名 |
| | | build.enablePathStyleAccess(); |
| | | } |
| | | this.client = build.build(); |
| | | |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | | if (e instanceof OssException) { |
| | | throw e; |
| | | } |
| | | throw new OssException("配置错误! 请检查系统配置:[" + e.getMessage() + "]"); |
| | | } |
| | | } |
| | | |
| | | public void createBucket() { |
| | | try { |
| | | String bucketName = properties.getBucketName(); |
| | | if (client.doesBucketExistV2(bucketName)) { |
| | | return; |
| | | } |
| | | CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); |
| | | AccessPolicyType accessPolicy = getAccessPolicy(); |
| | | createBucketRequest.setCannedAcl(accessPolicy.getAcl()); |
| | | client.createBucket(createBucketRequest); |
| | | client.setBucketPolicy(bucketName, getPolicy(bucketName, accessPolicy.getPolicyType())); |
| | | } catch (Exception e) { |
| | | throw new OssException("创建Bucket失败, 请核对配置信息:[" + e.getMessage() + "]"); |
| | | } |
| | | } |
| | | |
| | | public UploadResult upload(byte[] data, String path, String contentType) { |
| | | return upload(new ByteArrayInputStream(data), path, contentType); |
| | | } |
| | | |
| | | public UploadResult upload(InputStream inputStream, String path, String contentType) { |
| | | if (!(inputStream instanceof ByteArrayInputStream)) { |
| | | inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream)); |
| | | } |
| | | try { |
| | | ObjectMetadata metadata = new ObjectMetadata(); |
| | | metadata.setContentType(contentType); |
| | | metadata.setContentLength(inputStream.available()); |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata); |
| | | // 设置上传对象的 Acl 为公共读 |
| | | putObjectRequest.setCannedAcl(getAccessPolicy().getAcl()); |
| | | client.putObject(putObjectRequest); |
| | | } catch (Exception e) { |
| | | throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]"); |
| | | } |
| | | return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build(); |
| | | } |
| | | |
| | | public UploadResult upload(File file, String path) { |
| | | try { |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, file); |
| | | // 设置上传对象的 Acl 为公共读 |
| | | putObjectRequest.setCannedAcl(getAccessPolicy().getAcl()); |
| | | client.putObject(putObjectRequest); |
| | | } catch (Exception e) { |
| | | throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]"); |
| | | } |
| | | return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build(); |
| | | } |
| | | |
| | | public void delete(String path) { |
| | | path = path.replace(getUrl() + "/", ""); |
| | | try { |
| | | client.deleteObject(properties.getBucketName(), path); |
| | | } catch (Exception e) { |
| | | throw new OssException("删除文件失败,请检查配置信息:[" + e.getMessage() + "]"); |
| | | } |
| | | } |
| | | |
| | | public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) { |
| | | return upload(data, getPath(properties.getPrefix(), suffix), contentType); |
| | | } |
| | | |
| | | public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); |
| | | } |
| | | |
| | | public UploadResult uploadSuffix(File file, String suffix) { |
| | | return upload(file, getPath(properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件元数据 |
| | | * |
| | | * @param path 完整文件路径 |
| | | */ |
| | | public ObjectMetadata getObjectMetadata(String path) { |
| | | path = path.replace(getUrl() + "/", ""); |
| | | S3Object object = client.getObject(properties.getBucketName(), path); |
| | | return object.getObjectMetadata(); |
| | | } |
| | | |
| | | public InputStream getObjectContent(String path) { |
| | | path = path.replace(getUrl() + "/", ""); |
| | | S3Object object = client.getObject(properties.getBucketName(), path); |
| | | return object.getObjectContent(); |
| | | } |
| | | |
| | | public String getUrl() { |
| | | String domain = properties.getDomain(); |
| | | String endpoint = properties.getEndpoint(); |
| | | String header = OssConstant.IS_HTTPS.equals(properties.getIsHttps()) ? "https://" : "http://"; |
| | | // 云服务商直接返回 |
| | | if (StringUtils.containsAny(endpoint, OssConstant.CLOUD_SERVICE)) { |
| | | if (StringUtils.isNotBlank(domain)) { |
| | | return header + domain; |
| | | } |
| | | return header + properties.getBucketName() + "." + endpoint; |
| | | } |
| | | // minio 单独处理 |
| | | if (StringUtils.isNotBlank(domain)) { |
| | | return header + domain + "/" + properties.getBucketName(); |
| | | } |
| | | return header + endpoint + "/" + properties.getBucketName(); |
| | | } |
| | | |
| | | public String getPath(String prefix, String suffix) { |
| | | // 生成uuid |
| | | String uuid = IdUtil.fastSimpleUUID(); |
| | | // 文件路径 |
| | | String path = DateUtils.datePath() + "/" + uuid; |
| | | if (StringUtils.isNotBlank(prefix)) { |
| | | path = prefix + "/" + path; |
| | | } |
| | | return path + suffix; |
| | | } |
| | | |
| | | |
| | | public String getConfigKey() { |
| | | return configKey; |
| | | } |
| | | |
| | | /** |
| | | * 获取私有URL链接 |
| | | * |
| | | * @param objectKey 对象KEY |
| | | * @param second 授权时间 |
| | | */ |
| | | public String getPrivateUrl(String objectKey, Integer second) { |
| | | GeneratePresignedUrlRequest generatePresignedUrlRequest = |
| | | new GeneratePresignedUrlRequest(properties.getBucketName(), objectKey) |
| | | .withMethod(HttpMethod.GET) |
| | | .withExpiration(new Date(System.currentTimeMillis() + 1000L * second)); |
| | | URL url = client.generatePresignedUrl(generatePresignedUrlRequest); |
| | | return url.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 检查配置是否相同 |
| | | */ |
| | | public boolean checkPropertiesSame(OssProperties properties) { |
| | | return this.properties.equals(properties); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前桶权限类型 |
| | | * |
| | | * @return 当前桶权限类型code |
| | | */ |
| | | public AccessPolicyType getAccessPolicy() { |
| | | return AccessPolicyType.getByType(properties.getAccessPolicy()); |
| | | } |
| | | |
| | | private static String getPolicy(String bucketName, PolicyType policyType) { |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.append("{\n\"Statement\": [\n{\n\"Action\": [\n"); |
| | | builder.append(switch (policyType) { |
| | | case WRITE -> "\"s3:GetBucketLocation\",\n\"s3:ListBucketMultipartUploads\"\n"; |
| | | case READ_WRITE -> "\"s3:GetBucketLocation\",\n\"s3:ListBucket\",\n\"s3:ListBucketMultipartUploads\"\n"; |
| | | default -> "\"s3:GetBucketLocation\"\n"; |
| | | }); |
| | | builder.append("],\n\"Effect\": \"Allow\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::"); |
| | | builder.append(bucketName); |
| | | builder.append("\"\n},\n"); |
| | | if (policyType == PolicyType.READ) { |
| | | builder.append("{\n\"Action\": [\n\"s3:ListBucket\"\n],\n\"Effect\": \"Deny\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::"); |
| | | builder.append(bucketName); |
| | | builder.append("\"\n},\n"); |
| | | } |
| | | builder.append("{\n\"Action\": "); |
| | | builder.append(switch (policyType) { |
| | | case WRITE -> "[\n\"s3:AbortMultipartUpload\",\n\"s3:DeleteObject\",\n\"s3:ListMultipartUploadParts\",\n\"s3:PutObject\"\n],\n"; |
| | | case READ_WRITE -> "[\n\"s3:AbortMultipartUpload\",\n\"s3:DeleteObject\",\n\"s3:GetObject\",\n\"s3:ListMultipartUploadParts\",\n\"s3:PutObject\"\n],\n"; |
| | | default -> "\"s3:GetObject\",\n"; |
| | | }); |
| | | builder.append("\"Effect\": \"Allow\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::"); |
| | | builder.append(bucketName); |
| | | builder.append("/*\"\n}\n],\n\"Version\": \"2012-10-17\"\n}\n"); |
| | | return builder.toString(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.entity; |
| | | |
| | | import lombok.Builder; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * 上传返回体 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @Builder |
| | | public class UploadResult { |
| | | |
| | | /** |
| | | * 文件路径 |
| | | */ |
| | | private String url; |
| | | |
| | | /** |
| | | * 文件名 |
| | | */ |
| | | private String filename; |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.enumd; |
| | | |
| | | import com.amazonaws.services.s3.model.CannedAccessControlList; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 桶访问策略配置 |
| | | * |
| | | * @author 陈賝 |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum AccessPolicyType { |
| | | |
| | | /** |
| | | * private |
| | | */ |
| | | PRIVATE("0", CannedAccessControlList.Private, PolicyType.WRITE), |
| | | |
| | | /** |
| | | * public |
| | | */ |
| | | PUBLIC("1", CannedAccessControlList.PublicRead, PolicyType.READ), |
| | | |
| | | /** |
| | | * custom |
| | | */ |
| | | CUSTOM("2",CannedAccessControlList.PublicRead, PolicyType.READ); |
| | | |
| | | /** |
| | | * 桶 权限类型 |
| | | */ |
| | | private final String type; |
| | | |
| | | /** |
| | | * 文件对象 权限类型 |
| | | */ |
| | | private final CannedAccessControlList acl; |
| | | |
| | | /** |
| | | * 桶策略类型 |
| | | */ |
| | | private final PolicyType policyType; |
| | | |
| | | public static AccessPolicyType getByType(String type) { |
| | | for (AccessPolicyType value : values()) { |
| | | if (value.getType().equals(type)) { |
| | | return value; |
| | | } |
| | | } |
| | | throw new RuntimeException("'type' not found By " + type); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.enumd; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * minio策略配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum PolicyType { |
| | | |
| | | /** |
| | | * 只读 |
| | | */ |
| | | READ("read-only"), |
| | | |
| | | /** |
| | | * 只写 |
| | | */ |
| | | WRITE("write-only"), |
| | | |
| | | /** |
| | | * 读写 |
| | | */ |
| | | READ_WRITE("read-write"); |
| | | |
| | | /** |
| | | * 类型 |
| | | */ |
| | | private final String type; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.exception; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * OSS异常类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class OssException extends RuntimeException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public OssException(String msg) { |
| | | super(msg); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.factory; |
| | | |
| | | import org.dromara.common.core.constant.CacheNames; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import org.dromara.common.oss.constant.OssConstant; |
| | | import org.dromara.common.oss.core.OssClient; |
| | | import org.dromara.common.oss.exception.OssException; |
| | | import org.dromara.common.oss.properties.OssProperties; |
| | | import org.dromara.common.redis.utils.CacheUtils; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * 文件上传Factory |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class OssFactory { |
| | | |
| | | private static final Map<String, OssClient> CLIENT_CACHE = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * 获取默认实例 |
| | | */ |
| | | public static OssClient instance() { |
| | | // 获取redis 默认类型 |
| | | String configKey = RedisUtils.getCacheObject(OssConstant.DEFAULT_CONFIG_KEY); |
| | | if (StringUtils.isEmpty(configKey)) { |
| | | throw new OssException("文件存储服务类型无法找到!"); |
| | | } |
| | | return instance(configKey); |
| | | } |
| | | |
| | | /** |
| | | * 根据类型获取实例 |
| | | */ |
| | | public static synchronized OssClient instance(String configKey) { |
| | | String json = CacheUtils.get(CacheNames.SYS_OSS_CONFIG, configKey); |
| | | if (json == null) { |
| | | throw new OssException("系统异常, '" + configKey + "'配置信息不存在!"); |
| | | } |
| | | OssProperties properties = JsonUtils.parseObject(json, OssProperties.class); |
| | | // 使用租户标识避免多个租户相同key实例覆盖 |
| | | String key = properties.getTenantId() + ":" + configKey; |
| | | OssClient client = CLIENT_CACHE.get(key); |
| | | if (client == null) { |
| | | CLIENT_CACHE.put(key, new OssClient(configKey, properties)); |
| | | log.info("创建OSS实例 key => {}", configKey); |
| | | return CLIENT_CACHE.get(key); |
| | | } |
| | | // 配置不相同则重新构建 |
| | | if (!client.checkPropertiesSame(properties)) { |
| | | CLIENT_CACHE.put(key, new OssClient(configKey, properties)); |
| | | log.info("重载OSS实例 key => {}", configKey); |
| | | return CLIENT_CACHE.get(key); |
| | | } |
| | | return client; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * OSS对象存储 配置属性 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | public class OssProperties { |
| | | |
| | | /** |
| | | * 租户id |
| | | */ |
| | | private String tenantId; |
| | | |
| | | /** |
| | | * 访问站点 |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * 自定义域名 |
| | | */ |
| | | private String domain; |
| | | |
| | | /** |
| | | * 前缀 |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * ACCESS_KEY |
| | | */ |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * SECRET_KEY |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * 存储空间名 |
| | | */ |
| | | private String bucketName; |
| | | |
| | | /** |
| | | * 存储区域 |
| | | */ |
| | | private String region; |
| | | |
| | | /** |
| | | * 是否https(Y=是,N=否) |
| | | */ |
| | | private String isHttps; |
| | | |
| | | /** |
| | | * 桶权限类型(0private 1public 2custom) |
| | | */ |
| | | private String accessPolicy; |
| | | |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-prometheus</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-prometheus prometheus监控 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-actuator</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>io.micrometer</groupId> |
| | | <artifactId>micrometer-registry-prometheus</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.prometheus.config; |
| | | |
| | | import io.micrometer.core.instrument.MeterRegistry; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * prometheus 配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | public class PrometheusConfiguration { |
| | | |
| | | @Bean |
| | | public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) { |
| | | return (registry) -> registry.config().commonTags("application", applicationName); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.prometheus.config.PrometheusConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-ratelimiter</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-ratelimiter 限流功能 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.ratelimiter.annotation; |
| | | |
| | | import org.dromara.common.ratelimiter.enums.LimitType; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 限流注解 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface RateLimiter { |
| | | /** |
| | | * 限流key,支持使用Spring el表达式来动态获取方法上的参数值 |
| | | * 格式类似于 #code.id #{#code} |
| | | */ |
| | | String key() default ""; |
| | | |
| | | /** |
| | | * 限流时间,单位秒 |
| | | */ |
| | | int time() default 60; |
| | | |
| | | /** |
| | | * 限流次数 |
| | | */ |
| | | int count() default 100; |
| | | |
| | | /** |
| | | * 限流类型 |
| | | */ |
| | | LimitType limitType() default LimitType.DEFAULT; |
| | | |
| | | /** |
| | | * 提示消息 支持国际化 格式为 {code} |
| | | */ |
| | | String message() default "{rate.limiter.message}"; |
| | | } |
New file |
| | |
| | | package org.dromara.common.ratelimiter.aspectj; |
| | | |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.MessageUtils; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.ratelimiter.annotation.RateLimiter; |
| | | import org.dromara.common.ratelimiter.enums.LimitType; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Before; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.redisson.api.RateType; |
| | | import org.springframework.core.DefaultParameterNameDiscoverer; |
| | | import org.springframework.core.ParameterNameDiscoverer; |
| | | import org.springframework.expression.EvaluationContext; |
| | | import org.springframework.expression.Expression; |
| | | import org.springframework.expression.ExpressionParser; |
| | | import org.springframework.expression.ParserContext; |
| | | import org.springframework.expression.common.TemplateParserContext; |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * 限流处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @Aspect |
| | | public class RateLimiterAspect { |
| | | |
| | | /** |
| | | * 定义spel表达式解析器 |
| | | */ |
| | | private final ExpressionParser parser = new SpelExpressionParser(); |
| | | /** |
| | | * 定义spel解析模版 |
| | | */ |
| | | private final ParserContext parserContext = new TemplateParserContext(); |
| | | /** |
| | | * 定义spel上下文对象进行解析 |
| | | */ |
| | | private final EvaluationContext context = new StandardEvaluationContext(); |
| | | /** |
| | | * 方法参数解析器 |
| | | */ |
| | | private final ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer(); |
| | | |
| | | @Before("@annotation(rateLimiter)") |
| | | public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable { |
| | | int time = rateLimiter.time(); |
| | | int count = rateLimiter.count(); |
| | | String combineKey = getCombineKey(rateLimiter, point); |
| | | try { |
| | | RateType rateType = RateType.OVERALL; |
| | | if (rateLimiter.limitType() == LimitType.CLUSTER) { |
| | | rateType = RateType.PER_CLIENT; |
| | | } |
| | | long number = RedisUtils.rateLimiter(combineKey, rateType, count, time); |
| | | if (number == -1) { |
| | | String message = rateLimiter.message(); |
| | | if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) { |
| | | message = MessageUtils.message(StringUtils.substring(message, 1, message.length() - 1)); |
| | | } |
| | | throw new ServiceException(message); |
| | | } |
| | | log.info("限制令牌 => {}, 剩余令牌 => {}, 缓存key => '{}'", count, number, combineKey); |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | throw e; |
| | | } else { |
| | | throw new RuntimeException("服务器限流异常,请稍候再试"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) { |
| | | String key = rateLimiter.key(); |
| | | // 获取方法(通过方法签名来获取) |
| | | MethodSignature signature = (MethodSignature) point.getSignature(); |
| | | Method method = signature.getMethod(); |
| | | Class<?> targetClass = method.getDeclaringClass(); |
| | | // 判断是否是spel格式 |
| | | if (StringUtils.containsAny(key, "#")) { |
| | | // 获取参数值 |
| | | Object[] args = point.getArgs(); |
| | | // 获取方法上参数的名称 |
| | | String[] parameterNames = pnd.getParameterNames(method); |
| | | if (ArrayUtil.isEmpty(parameterNames)) { |
| | | throw new ServiceException("限流key解析异常!请联系管理员!"); |
| | | } |
| | | for (int i = 0; i < parameterNames.length; i++) { |
| | | context.setVariable(parameterNames[i], args[i]); |
| | | } |
| | | // 解析返回给key |
| | | try { |
| | | Expression expression; |
| | | if (StringUtils.startsWith(key, parserContext.getExpressionPrefix()) |
| | | && StringUtils.endsWith(key, parserContext.getExpressionSuffix())) { |
| | | expression = parser.parseExpression(key, parserContext); |
| | | } else { |
| | | expression = parser.parseExpression(key); |
| | | } |
| | | key = expression.getValue(context, String.class) + ":"; |
| | | } catch (Exception e) { |
| | | throw new ServiceException("限流key解析异常!请联系管理员!"); |
| | | } |
| | | } |
| | | StringBuilder stringBuffer = new StringBuilder(GlobalConstants.RATE_LIMIT_KEY); |
| | | stringBuffer.append(ServletUtils.getRequest().getRequestURI()).append(":"); |
| | | if (rateLimiter.limitType() == LimitType.IP) { |
| | | // 获取请求ip |
| | | stringBuffer.append(ServletUtils.getClientIP()).append(":"); |
| | | } else if (rateLimiter.limitType() == LimitType.CLUSTER) { |
| | | // 获取客户端实例id |
| | | stringBuffer.append(RedisUtils.getClient().getId()).append(":"); |
| | | } |
| | | return stringBuffer.append(key).toString(); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.ratelimiter.config; |
| | | |
| | | import org.dromara.common.ratelimiter.aspectj.RateLimiterAspect; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.data.redis.connection.RedisConfiguration; |
| | | |
| | | /** |
| | | * @author guangxin |
| | | * @date 2023/1/18 |
| | | */ |
| | | @AutoConfiguration(after = RedisConfiguration.class) |
| | | public class RateLimiterConfig { |
| | | |
| | | @Bean |
| | | public RateLimiterAspect rateLimiterAspect() { |
| | | return new RateLimiterAspect(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.ratelimiter.enums; |
| | | |
| | | /** |
| | | * 限流类型 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | |
| | | public enum LimitType { |
| | | /** |
| | | * 默认策略全局限流 |
| | | */ |
| | | DEFAULT, |
| | | |
| | | /** |
| | | * 根据请求者IP进行限流 |
| | | */ |
| | | IP, |
| | | |
| | | /** |
| | | * 实例限流(集群多后端实例) |
| | | */ |
| | | CLUSTER |
| | | } |
New file |
| | |
| | | org.dromara.common.ratelimiter.config.RateLimiterConfig |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-redis 缓存服务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <!-- RuoYi Common Core--> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <!--redisson--> |
| | | <dependency> |
| | | <groupId>org.redisson</groupId> |
| | | <artifactId>redisson-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>lock4j-redisson-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.redis.config; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| | | import com.fasterxml.jackson.annotation.PropertyAccessor; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.redis.config.properties.RedissonProperties; |
| | | import org.dromara.common.redis.handler.KeyPrefixHandler; |
| | | import org.dromara.common.redis.manager.PlusSpringCacheManager; |
| | | import org.redisson.client.codec.StringCodec; |
| | | import org.redisson.codec.CompositeCodec; |
| | | import org.redisson.codec.TypedJsonJacksonCodec; |
| | | import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.cache.CacheManager; |
| | | import org.springframework.cache.annotation.EnableCaching; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * redis配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @AutoConfiguration |
| | | @EnableCaching |
| | | @EnableConfigurationProperties(RedissonProperties.class) |
| | | public class RedisConfiguration { |
| | | |
| | | @Autowired |
| | | private RedissonProperties redissonProperties; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | @Bean |
| | | public RedissonAutoConfigurationCustomizer redissonCustomizer() { |
| | | return config -> { |
| | | ObjectMapper om = objectMapper.copy(); |
| | | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| | | // 指定序列化输入的类型,类必须是非final修饰的。序列化时将对象全类名一起保存下来 |
| | | om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); |
| | | TypedJsonJacksonCodec jsonCodec = new TypedJsonJacksonCodec(Object.class, om); |
| | | // 组合序列化 key 使用 String 内容使用通用 json 格式 |
| | | CompositeCodec codec = new CompositeCodec(StringCodec.INSTANCE, jsonCodec, jsonCodec); |
| | | config.setThreads(redissonProperties.getThreads()) |
| | | .setNettyThreads(redissonProperties.getNettyThreads()) |
| | | // 缓存 Lua 脚本 减少网络传输(redisson 大部分的功能都是基于 Lua 脚本实现) |
| | | .setUseScriptCache(true) |
| | | .setCodec(codec); |
| | | RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig(); |
| | | if (ObjectUtil.isNotNull(singleServerConfig)) { |
| | | // 使用单机模式 |
| | | config.useSingleServer() |
| | | //设置redis key前缀 |
| | | .setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix())) |
| | | .setTimeout(singleServerConfig.getTimeout()) |
| | | .setClientName(singleServerConfig.getClientName()) |
| | | .setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout()) |
| | | .setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize()) |
| | | .setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize()) |
| | | .setConnectionPoolSize(singleServerConfig.getConnectionPoolSize()); |
| | | } |
| | | // 集群配置方式 参考下方注释 |
| | | RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig(); |
| | | if (ObjectUtil.isNotNull(clusterServersConfig)) { |
| | | config.useClusterServers() |
| | | //设置redis key前缀 |
| | | .setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix())) |
| | | .setTimeout(clusterServersConfig.getTimeout()) |
| | | .setClientName(clusterServersConfig.getClientName()) |
| | | .setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout()) |
| | | .setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize()) |
| | | .setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize()) |
| | | .setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize()) |
| | | .setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize()) |
| | | .setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize()) |
| | | .setReadMode(clusterServersConfig.getReadMode()) |
| | | .setSubscriptionMode(clusterServersConfig.getSubscriptionMode()); |
| | | } |
| | | log.info("初始化 redis 配置"); |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 自定义缓存管理器 整合spring-cache |
| | | */ |
| | | @Bean |
| | | public CacheManager cacheManager() { |
| | | return new PlusSpringCacheManager(); |
| | | } |
| | | |
| | | /** |
| | | * redis集群配置 yml |
| | | * |
| | | * --- # redis 集群配置(单机与集群只能开启一个另一个需要注释掉) |
| | | * spring.data: |
| | | * redis: |
| | | * cluster: |
| | | * nodes: |
| | | * - 192.168.0.100:6379 |
| | | * - 192.168.0.101:6379 |
| | | * - 192.168.0.102:6379 |
| | | * # 密码 |
| | | * password: |
| | | * # 连接超时时间 |
| | | * timeout: 10s |
| | | * # 是否开启ssl |
| | | * ssl.enabled: false |
| | | * |
| | | * redisson: |
| | | * # 线程池数量 |
| | | * threads: 16 |
| | | * # Netty线程池数量 |
| | | * nettyThreads: 32 |
| | | * # 集群配置 |
| | | * clusterServersConfig: |
| | | * # 客户端名称 |
| | | * clientName: ${ruoyi.name} |
| | | * # master最小空闲连接数 |
| | | * masterConnectionMinimumIdleSize: 32 |
| | | * # master连接池大小 |
| | | * masterConnectionPoolSize: 64 |
| | | * # slave最小空闲连接数 |
| | | * slaveConnectionMinimumIdleSize: 32 |
| | | * # slave连接池大小 |
| | | * slaveConnectionPoolSize: 64 |
| | | * # 连接空闲超时,单位:毫秒 |
| | | * idleConnectionTimeout: 10000 |
| | | * # 命令等待超时,单位:毫秒 |
| | | * timeout: 3000 |
| | | * # 发布和订阅连接池大小 |
| | | * subscriptionConnectionPoolSize: 50 |
| | | * # 读取模式 |
| | | * readMode: "SLAVE" |
| | | * # 订阅模式 |
| | | * subscriptionMode: "MASTER" |
| | | */ |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.redis.config.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.config.ReadMode; |
| | | import org.redisson.config.SubscriptionMode; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * Redisson 配置属性 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "redisson") |
| | | public class RedissonProperties { |
| | | |
| | | /** |
| | | * redis缓存key前缀 |
| | | */ |
| | | private String keyPrefix; |
| | | |
| | | /** |
| | | * 线程池数量,默认值 = 当前处理核数量 * 2 |
| | | */ |
| | | private int threads; |
| | | |
| | | /** |
| | | * Netty线程池数量,默认值 = 当前处理核数量 * 2 |
| | | */ |
| | | private int nettyThreads; |
| | | |
| | | /** |
| | | * 单机服务配置 |
| | | */ |
| | | private SingleServerConfig singleServerConfig; |
| | | |
| | | /** |
| | | * 集群服务配置 |
| | | */ |
| | | private ClusterServersConfig clusterServersConfig; |
| | | |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class SingleServerConfig { |
| | | |
| | | /** |
| | | * 客户端名称 |
| | | */ |
| | | private String clientName; |
| | | |
| | | /** |
| | | * 最小空闲连接数 |
| | | */ |
| | | private int connectionMinimumIdleSize; |
| | | |
| | | /** |
| | | * 连接池大小 |
| | | */ |
| | | private int connectionPoolSize; |
| | | |
| | | /** |
| | | * 连接空闲超时,单位:毫秒 |
| | | */ |
| | | private int idleConnectionTimeout; |
| | | |
| | | /** |
| | | * 命令等待超时,单位:毫秒 |
| | | */ |
| | | private int timeout; |
| | | |
| | | /** |
| | | * 发布和订阅连接池大小 |
| | | */ |
| | | private int subscriptionConnectionPoolSize; |
| | | |
| | | } |
| | | |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class ClusterServersConfig { |
| | | |
| | | /** |
| | | * 客户端名称 |
| | | */ |
| | | private String clientName; |
| | | |
| | | /** |
| | | * master最小空闲连接数 |
| | | */ |
| | | private int masterConnectionMinimumIdleSize; |
| | | |
| | | /** |
| | | * master连接池大小 |
| | | */ |
| | | private int masterConnectionPoolSize; |
| | | |
| | | /** |
| | | * slave最小空闲连接数 |
| | | */ |
| | | private int slaveConnectionMinimumIdleSize; |
| | | |
| | | /** |
| | | * slave连接池大小 |
| | | */ |
| | | private int slaveConnectionPoolSize; |
| | | |
| | | /** |
| | | * 连接空闲超时,单位:毫秒 |
| | | */ |
| | | private int idleConnectionTimeout; |
| | | |
| | | /** |
| | | * 命令等待超时,单位:毫秒 |
| | | */ |
| | | private int timeout; |
| | | |
| | | /** |
| | | * 发布和订阅连接池大小 |
| | | */ |
| | | private int subscriptionConnectionPoolSize; |
| | | |
| | | /** |
| | | * 读取模式 |
| | | */ |
| | | private ReadMode readMode; |
| | | |
| | | /** |
| | | * 订阅模式 |
| | | */ |
| | | private SubscriptionMode subscriptionMode; |
| | | |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.redis.handler; |
| | | |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.redisson.api.NameMapper; |
| | | |
| | | /** |
| | | * redis缓存key前缀处理 |
| | | * |
| | | * @author ye |
| | | * @date 2022/7/14 17:44 |
| | | * @since 4.3.0 |
| | | */ |
| | | public class KeyPrefixHandler implements NameMapper { |
| | | |
| | | private final String keyPrefix; |
| | | |
| | | public KeyPrefixHandler(String keyPrefix) { |
| | | //前缀为空 则返回空前缀 |
| | | this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":"; |
| | | } |
| | | |
| | | /** |
| | | * 增加前缀 |
| | | */ |
| | | @Override |
| | | public String map(String name) { |
| | | if (StringUtils.isBlank(name)) { |
| | | return null; |
| | | } |
| | | if (StringUtils.isNotBlank(keyPrefix) && !name.startsWith(keyPrefix)) { |
| | | return keyPrefix + name; |
| | | } |
| | | return name; |
| | | } |
| | | |
| | | /** |
| | | * 去除前缀 |
| | | */ |
| | | @Override |
| | | public String unmap(String name) { |
| | | if (StringUtils.isBlank(name)) { |
| | | return null; |
| | | } |
| | | if (StringUtils.isNotBlank(keyPrefix) && name.startsWith(keyPrefix)) { |
| | | return name.substring(keyPrefix.length()); |
| | | } |
| | | return name; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | /** |
| | | * Copyright (c) 2013-2021 Nikita Koksharov |
| | | * |
| | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | * you may not use this file except in compliance with the License. |
| | | * You may obtain a copy of the License at |
| | | * |
| | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | | * |
| | | * Unless required by applicable law or agreed to in writing, software |
| | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | * See the License for the specific language governing permissions and |
| | | * limitations under the License. |
| | | */ |
| | | package org.dromara.common.redis.manager; |
| | | |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.redisson.api.RMap; |
| | | import org.redisson.api.RMapCache; |
| | | import org.redisson.spring.cache.CacheConfig; |
| | | import org.redisson.spring.cache.RedissonCache; |
| | | import org.springframework.boot.convert.DurationStyle; |
| | | import org.springframework.cache.Cache; |
| | | import org.springframework.cache.CacheManager; |
| | | import org.springframework.cache.transaction.TransactionAwareCacheDecorator; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.ConcurrentMap; |
| | | |
| | | /** |
| | | * A {@link CacheManager} implementation |
| | | * backed by Redisson instance. |
| | | * <p> |
| | | * 修改 RedissonSpringCacheManager 源码 |
| | | * 重写 cacheName 处理方法 支持多参数 |
| | | * |
| | | * @author Nikita Koksharov |
| | | * |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public class PlusSpringCacheManager implements CacheManager { |
| | | |
| | | private boolean dynamic = true; |
| | | |
| | | private boolean allowNullValues = true; |
| | | |
| | | private boolean transactionAware = true; |
| | | |
| | | Map<String, CacheConfig> configMap = new ConcurrentHashMap<>(); |
| | | ConcurrentMap<String, Cache> instanceMap = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * Creates CacheManager supplied by Redisson instance |
| | | */ |
| | | public PlusSpringCacheManager() { |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Defines possibility of storing {@code null} values. |
| | | * <p> |
| | | * Default is <code>true</code> |
| | | * |
| | | * @param allowNullValues stores if <code>true</code> |
| | | */ |
| | | public void setAllowNullValues(boolean allowNullValues) { |
| | | this.allowNullValues = allowNullValues; |
| | | } |
| | | |
| | | /** |
| | | * Defines if cache aware of Spring-managed transactions. |
| | | * If {@code true} put/evict operations are executed only for successful transaction in after-commit phase. |
| | | * <p> |
| | | * Default is <code>false</code> |
| | | * |
| | | * @param transactionAware cache is transaction aware if <code>true</code> |
| | | */ |
| | | public void setTransactionAware(boolean transactionAware) { |
| | | this.transactionAware = transactionAware; |
| | | } |
| | | |
| | | /** |
| | | * Defines 'fixed' cache names. |
| | | * A new cache instance will not be created in dynamic for non-defined names. |
| | | * <p> |
| | | * `null` parameter setups dynamic mode |
| | | * |
| | | * @param names of caches |
| | | */ |
| | | public void setCacheNames(Collection<String> names) { |
| | | if (names != null) { |
| | | for (String name : names) { |
| | | getCache(name); |
| | | } |
| | | dynamic = false; |
| | | } else { |
| | | dynamic = true; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Set cache config mapped by cache name |
| | | * |
| | | * @param config object |
| | | */ |
| | | public void setConfig(Map<String, ? extends CacheConfig> config) { |
| | | this.configMap = (Map<String, CacheConfig>) config; |
| | | } |
| | | |
| | | protected CacheConfig createDefaultConfig() { |
| | | return new CacheConfig(); |
| | | } |
| | | |
| | | @Override |
| | | public Cache getCache(String name) { |
| | | // 重写 cacheName 支持多参数 |
| | | String[] array = StringUtils.delimitedListToStringArray(name, "#"); |
| | | name = array[0]; |
| | | |
| | | Cache cache = instanceMap.get(name); |
| | | if (cache != null) { |
| | | return cache; |
| | | } |
| | | if (!dynamic) { |
| | | return cache; |
| | | } |
| | | |
| | | CacheConfig config = configMap.get(name); |
| | | if (config == null) { |
| | | config = createDefaultConfig(); |
| | | configMap.put(name, config); |
| | | } |
| | | |
| | | if (array.length > 1) { |
| | | config.setTTL(DurationStyle.detectAndParse(array[1]).toMillis()); |
| | | } |
| | | if (array.length > 2) { |
| | | config.setMaxIdleTime(DurationStyle.detectAndParse(array[2]).toMillis()); |
| | | } |
| | | if (array.length > 3) { |
| | | config.setMaxSize(Integer.parseInt(array[3])); |
| | | } |
| | | |
| | | if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { |
| | | return createMap(name, config); |
| | | } |
| | | |
| | | return createMapCache(name, config); |
| | | } |
| | | |
| | | private Cache createMap(String name, CacheConfig config) { |
| | | RMap<Object, Object> map = RedisUtils.getClient().getMap(name); |
| | | |
| | | Cache cache = new RedissonCache(map, allowNullValues); |
| | | if (transactionAware) { |
| | | cache = new TransactionAwareCacheDecorator(cache); |
| | | } |
| | | Cache oldCache = instanceMap.putIfAbsent(name, cache); |
| | | if (oldCache != null) { |
| | | cache = oldCache; |
| | | } |
| | | return cache; |
| | | } |
| | | |
| | | private Cache createMapCache(String name, CacheConfig config) { |
| | | RMapCache<Object, Object> map = RedisUtils.getClient().getMapCache(name); |
| | | |
| | | Cache cache = new RedissonCache(map, config, allowNullValues); |
| | | if (transactionAware) { |
| | | cache = new TransactionAwareCacheDecorator(cache); |
| | | } |
| | | Cache oldCache = instanceMap.putIfAbsent(name, cache); |
| | | if (oldCache != null) { |
| | | cache = oldCache; |
| | | } else { |
| | | map.setMaxSize(config.getMaxSize()); |
| | | } |
| | | return cache; |
| | | } |
| | | |
| | | @Override |
| | | public Collection<String> getCacheNames() { |
| | | return Collections.unmodifiableSet(configMap.keySet()); |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.redis.utils; |
| | | |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.api.RMap; |
| | | import org.springframework.cache.Cache; |
| | | import org.springframework.cache.CacheManager; |
| | | |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 缓存操作工具类 {@link } |
| | | * |
| | | * @author Michelle.Chung |
| | | * @date 2022/8/13 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | @SuppressWarnings(value = {"unchecked"}) |
| | | public class CacheUtils { |
| | | |
| | | private static final CacheManager CACHE_MANAGER = SpringUtils.getBean(CacheManager.class); |
| | | |
| | | /** |
| | | * 获取缓存组内所有的KEY |
| | | * |
| | | * @param cacheNames 缓存组名称 |
| | | */ |
| | | public static Set<Object> keys(String cacheNames) { |
| | | RMap<Object, Object> rmap = (RMap<Object, Object>) CACHE_MANAGER.getCache(cacheNames).getNativeCache(); |
| | | return rmap.keySet(); |
| | | } |
| | | |
| | | /** |
| | | * 获取缓存值 |
| | | * |
| | | * @param cacheNames 缓存组名称 |
| | | * @param key 缓存key |
| | | */ |
| | | public static <T> T get(String cacheNames, Object key) { |
| | | Cache.ValueWrapper wrapper = CACHE_MANAGER.getCache(cacheNames).get(key); |
| | | return wrapper != null ? (T) wrapper.get() : null; |
| | | } |
| | | |
| | | /** |
| | | * 保存缓存值 |
| | | * |
| | | * @param cacheNames 缓存组名称 |
| | | * @param key 缓存key |
| | | * @param value 缓存值 |
| | | */ |
| | | public static void put(String cacheNames, Object key, Object value) { |
| | | CACHE_MANAGER.getCache(cacheNames).put(key, value); |
| | | } |
| | | |
| | | /** |
| | | * 删除缓存值 |
| | | * |
| | | * @param cacheNames 缓存组名称 |
| | | * @param key 缓存key |
| | | */ |
| | | public static void evict(String cacheNames, Object key) { |
| | | CACHE_MANAGER.getCache(cacheNames).evict(key); |
| | | } |
| | | |
| | | /** |
| | | * 清空缓存值 |
| | | * |
| | | * @param cacheNames 缓存组名称 |
| | | */ |
| | | public static void clear(String cacheNames) { |
| | | CACHE_MANAGER.getCache(cacheNames).clear(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.redis.utils; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.redisson.api.*; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.function.Consumer; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * redis 工具类 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.1.0 新增 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | @SuppressWarnings(value = {"unchecked", "rawtypes"}) |
| | | public class RedisUtils { |
| | | |
| | | private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class); |
| | | |
| | | /** |
| | | * 限流 |
| | | * |
| | | * @param key 限流key |
| | | * @param rateType 限流类型 |
| | | * @param rate 速率 |
| | | * @param rateInterval 速率间隔 |
| | | * @return -1 表示失败 |
| | | */ |
| | | public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) { |
| | | RRateLimiter rateLimiter = CLIENT.getRateLimiter(key); |
| | | rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS); |
| | | if (rateLimiter.tryAcquire()) { |
| | | return rateLimiter.availablePermits(); |
| | | } else { |
| | | return -1L; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取客户端实例 |
| | | */ |
| | | public static RedissonClient getClient() { |
| | | return CLIENT; |
| | | } |
| | | |
| | | /** |
| | | * 发布通道消息 |
| | | * |
| | | * @param channelKey 通道key |
| | | * @param msg 发送数据 |
| | | * @param consumer 自定义处理 |
| | | */ |
| | | public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) { |
| | | RTopic topic = CLIENT.getTopic(channelKey); |
| | | topic.publish(msg); |
| | | consumer.accept(msg); |
| | | } |
| | | |
| | | public static <T> void publish(String channelKey, T msg) { |
| | | RTopic topic = CLIENT.getTopic(channelKey); |
| | | topic.publish(msg); |
| | | } |
| | | |
| | | /** |
| | | * 订阅通道接收消息 |
| | | * |
| | | * @param channelKey 通道key |
| | | * @param clazz 消息类型 |
| | | * @param consumer 自定义处理 |
| | | */ |
| | | public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) { |
| | | RTopic topic = CLIENT.getTopic(channelKey); |
| | | topic.addListener(clazz, (channel, msg) -> consumer.accept(msg)); |
| | | } |
| | | |
| | | /** |
| | | * 缓存基本的对象,Integer、String、实体类等 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value) { |
| | | setCacheObject(key, value, false); |
| | | } |
| | | |
| | | /** |
| | | * 缓存基本的对象,保留当前对象 TTL 有效期 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90) |
| | | * @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) { |
| | | RBucket<T> bucket = CLIENT.getBucket(key); |
| | | if (isSaveTtl) { |
| | | try { |
| | | bucket.setAndKeepTTL(value); |
| | | } catch (Exception e) { |
| | | long timeToLive = bucket.remainTimeToLive(); |
| | | setCacheObject(key, value, Duration.ofMillis(timeToLive)); |
| | | } |
| | | } else { |
| | | bucket.set(value); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 缓存基本的对象,Integer、String、实体类等 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @param duration 时间 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value, final Duration duration) { |
| | | RBatch batch = CLIENT.createBatch(); |
| | | RBucketAsync<T> bucket = batch.getBucket(key); |
| | | bucket.setAsync(value); |
| | | bucket.expireAsync(duration); |
| | | batch.execute(); |
| | | } |
| | | |
| | | /** |
| | | * 如果不存在则设置 并返回 true 如果存在则返回 false |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @return set成功或失败 |
| | | */ |
| | | public static <T> boolean setObjectIfAbsent(final String key, final T value, final Duration duration) { |
| | | RBucket<T> bucket = CLIENT.getBucket(key); |
| | | return bucket.setIfAbsent(value, duration); |
| | | } |
| | | |
| | | /** |
| | | * 如果存在则设置 并返回 true 如果存在则返回 false |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @return set成功或失败 |
| | | */ |
| | | public static <T> boolean setObjectIfExists(final String key, final T value, final Duration duration) { |
| | | RBucket<T> bucket = CLIENT.getBucket(key); |
| | | return bucket.setIfExists(value, duration); |
| | | } |
| | | |
| | | /** |
| | | * 注册对象监听器 |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param listener 监听器配置 |
| | | */ |
| | | public static <T> void addObjectListener(final String key, final ObjectListener listener) { |
| | | RBucket<T> result = CLIENT.getBucket(key); |
| | | result.addListener(listener); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param timeout 超时时间 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final long timeout) { |
| | | return expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param duration 超时时间 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final Duration duration) { |
| | | RBucket rBucket = CLIENT.getBucket(key); |
| | | return rBucket.expire(duration); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的基本对象。 |
| | | * |
| | | * @param key 缓存键值 |
| | | * @return 缓存键值对应的数据 |
| | | */ |
| | | public static <T> T getCacheObject(final String key) { |
| | | RBucket<T> rBucket = CLIENT.getBucket(key); |
| | | return rBucket.get(); |
| | | } |
| | | |
| | | /** |
| | | * 获得key剩余存活时间 |
| | | * |
| | | * @param key 缓存键值 |
| | | * @return 剩余存活时间 |
| | | */ |
| | | public static <T> long getTimeToLive(final String key) { |
| | | RBucket<T> rBucket = CLIENT.getBucket(key); |
| | | return rBucket.remainTimeToLive(); |
| | | } |
| | | |
| | | /** |
| | | * 删除单个对象 |
| | | * |
| | | * @param key 缓存的键值 |
| | | */ |
| | | public static boolean deleteObject(final String key) { |
| | | return CLIENT.getBucket(key).delete(); |
| | | } |
| | | |
| | | /** |
| | | * 删除集合对象 |
| | | * |
| | | * @param collection 多个对象 |
| | | */ |
| | | public static void deleteObject(final Collection collection) { |
| | | RBatch batch = CLIENT.createBatch(); |
| | | collection.forEach(t -> { |
| | | batch.getBucket(t.toString()).deleteAsync(); |
| | | }); |
| | | batch.execute(); |
| | | } |
| | | |
| | | /** |
| | | * 检查缓存对象是否存在 |
| | | * |
| | | * @param key 缓存的键值 |
| | | */ |
| | | public static boolean isExistsObject(final String key) { |
| | | return CLIENT.getBucket(key).isExists(); |
| | | } |
| | | |
| | | /** |
| | | * 缓存List数据 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param dataList 待缓存的List数据 |
| | | * @return 缓存的对象 |
| | | */ |
| | | public static <T> boolean setCacheList(final String key, final List<T> dataList) { |
| | | RList<T> rList = CLIENT.getList(key); |
| | | return rList.addAll(dataList); |
| | | } |
| | | |
| | | /** |
| | | * 追加缓存List数据 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param data 待缓存的数据 |
| | | * @return 缓存的对象 |
| | | */ |
| | | public static <T> boolean addCacheList(final String key, final T data) { |
| | | RList<T> rList = CLIENT.getList(key); |
| | | return rList.add(data); |
| | | } |
| | | |
| | | /** |
| | | * 注册List监听器 |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param listener 监听器配置 |
| | | */ |
| | | public static <T> void addListListener(final String key, final ObjectListener listener) { |
| | | RList<T> rList = CLIENT.getList(key); |
| | | rList.addListener(listener); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的list对象 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return 缓存键值对应的数据 |
| | | */ |
| | | public static <T> List<T> getCacheList(final String key) { |
| | | RList<T> rList = CLIENT.getList(key); |
| | | return rList.readAll(); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的list对象(范围) |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param form 起始下标 |
| | | * @param to 截止下标 |
| | | * @return 缓存键值对应的数据 |
| | | */ |
| | | public static <T> List<T> getCacheListRange(final String key, int form, int to) { |
| | | RList<T> rList = CLIENT.getList(key); |
| | | return rList.range(form, to); |
| | | } |
| | | |
| | | /** |
| | | * 缓存Set |
| | | * |
| | | * @param key 缓存键值 |
| | | * @param dataSet 缓存的数据 |
| | | * @return 缓存数据的对象 |
| | | */ |
| | | public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) { |
| | | RSet<T> rSet = CLIENT.getSet(key); |
| | | return rSet.addAll(dataSet); |
| | | } |
| | | |
| | | /** |
| | | * 追加缓存Set数据 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param data 待缓存的数据 |
| | | * @return 缓存的对象 |
| | | */ |
| | | public static <T> boolean addCacheSet(final String key, final T data) { |
| | | RSet<T> rSet = CLIENT.getSet(key); |
| | | return rSet.add(data); |
| | | } |
| | | |
| | | /** |
| | | * 注册Set监听器 |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param listener 监听器配置 |
| | | */ |
| | | public static <T> void addSetListener(final String key, final ObjectListener listener) { |
| | | RSet<T> rSet = CLIENT.getSet(key); |
| | | rSet.addListener(listener); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的set |
| | | * |
| | | * @param key 缓存的key |
| | | * @return set对象 |
| | | */ |
| | | public static <T> Set<T> getCacheSet(final String key) { |
| | | RSet<T> rSet = CLIENT.getSet(key); |
| | | return rSet.readAll(); |
| | | } |
| | | |
| | | /** |
| | | * 缓存Map |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param dataMap 缓存的数据 |
| | | */ |
| | | public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) { |
| | | if (dataMap != null) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | rMap.putAll(dataMap); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 注册Map监听器 |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param listener 监听器配置 |
| | | */ |
| | | public static <T> void addMapListener(final String key, final ObjectListener listener) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | rMap.addListener(listener); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的Map |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return map对象 |
| | | */ |
| | | public static <T> Map<String, T> getCacheMap(final String key) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.getAll(rMap.keySet()); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存Map的key列表 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return key列表 |
| | | */ |
| | | public static <T> Set<String> getCacheMapKeySet(final String key) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.keySet(); |
| | | } |
| | | |
| | | /** |
| | | * 往Hash中存入数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKey Hash键 |
| | | * @param value 值 |
| | | */ |
| | | public static <T> void setCacheMapValue(final String key, final String hKey, final T value) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | rMap.put(hKey, value); |
| | | } |
| | | |
| | | /** |
| | | * 获取Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKey Hash键 |
| | | * @return Hash中的对象 |
| | | */ |
| | | public static <T> T getCacheMapValue(final String key, final String hKey) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.get(hKey); |
| | | } |
| | | |
| | | /** |
| | | * 删除Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKey Hash键 |
| | | * @return Hash中的对象 |
| | | */ |
| | | public static <T> T delCacheMapValue(final String key, final String hKey) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.remove(hKey); |
| | | } |
| | | |
| | | /** |
| | | * 删除Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKeys Hash键 |
| | | */ |
| | | public static <T> void delMultiCacheMapValue(final String key, final Set<String> hKeys) { |
| | | RBatch batch = CLIENT.createBatch(); |
| | | RMapAsync<String, T> rMap = batch.getMap(key); |
| | | for (String hKey : hKeys) { |
| | | rMap.removeAsync(hKey); |
| | | } |
| | | batch.execute(); |
| | | } |
| | | |
| | | /** |
| | | * 获取多个Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKeys Hash键集合 |
| | | * @return Hash对象集合 |
| | | */ |
| | | public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) { |
| | | RMap<K, V> rMap = CLIENT.getMap(key); |
| | | return rMap.getAll(hKeys); |
| | | } |
| | | |
| | | /** |
| | | * 设置原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @param value 值 |
| | | */ |
| | | public static void setAtomicValue(String key, long value) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | atomic.set(value); |
| | | } |
| | | |
| | | /** |
| | | * 获取原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long getAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.get(); |
| | | } |
| | | |
| | | /** |
| | | * 递增原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long incrAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.incrementAndGet(); |
| | | } |
| | | |
| | | /** |
| | | * 递减原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long decrAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.decrementAndGet(); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的基本对象列表 |
| | | * |
| | | * @param pattern 字符串前缀 |
| | | * @return 对象列表 |
| | | */ |
| | | public static Collection<String> keys(final String pattern) { |
| | | Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern); |
| | | return stream.collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 删除缓存的基本对象列表 |
| | | * |
| | | * @param pattern 字符串前缀 |
| | | */ |
| | | public static void deleteKeys(final String pattern) { |
| | | CLIENT.getKeys().deleteByPattern(pattern); |
| | | } |
| | | |
| | | /** |
| | | * 检查redis中是否存在key |
| | | * |
| | | * @param key 键 |
| | | */ |
| | | public static Boolean hasKey(String key) { |
| | | RKeys rKeys = CLIENT.getKeys(); |
| | | return rKeys.countExists(key) > 0; |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.redis.config.RedisConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-satoken |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-core</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Sa-Token 整合 jwt --> |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-jwt</artifactId> |
| | | <version>${satoken.version}</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-all</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-jwt</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- RuoYi Api System --> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-api-system</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- RuoYi Common Redis--> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.satoken.config; |
| | | |
| | | import cn.dev33.satoken.dao.SaTokenDao; |
| | | import cn.dev33.satoken.jwt.StpLogicJwtForSimple; |
| | | import cn.dev33.satoken.stp.StpInterface; |
| | | import cn.dev33.satoken.stp.StpLogic; |
| | | import org.dromara.common.core.factory.YmlPropertySourceFactory; |
| | | import org.dromara.common.satoken.core.dao.PlusSaTokenDao; |
| | | import org.dromara.common.satoken.core.service.SaPermissionImpl; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.PropertySource; |
| | | |
| | | /** |
| | | * Sa-Token 配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | @PropertySource(value = "classpath:common-satoken.yml", factory = YmlPropertySourceFactory.class) |
| | | public class SaTokenConfiguration { |
| | | |
| | | @Bean |
| | | public StpLogic getStpLogicJwt() { |
| | | return new StpLogicJwtForSimple(); |
| | | } |
| | | |
| | | /** |
| | | * 权限接口实现(使用bean注入方便用户替换) |
| | | */ |
| | | @Bean |
| | | public StpInterface stpInterface() { |
| | | return new SaPermissionImpl(); |
| | | } |
| | | |
| | | /** |
| | | * 自定义dao层存储 |
| | | */ |
| | | @Bean |
| | | public SaTokenDao saTokenDao() { |
| | | return new PlusSaTokenDao(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.satoken.core.dao; |
| | | |
| | | import cn.dev33.satoken.dao.SaTokenDao; |
| | | import cn.dev33.satoken.util.SaFoxUtil; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Sa-Token持久层接口(使用框架自带RedisUtils实现 协议统一) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class PlusSaTokenDao implements SaTokenDao { |
| | | |
| | | /** |
| | | * 获取Value,如无返空 |
| | | */ |
| | | @Override |
| | | public String get(String key) { |
| | | return RedisUtils.getCacheObject(key); |
| | | } |
| | | |
| | | /** |
| | | * 写入Value,并设定存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void set(String key, String value, long timeout) { |
| | | if (timeout == 0 || timeout <= NOT_VALUE_EXPIRE) { |
| | | return; |
| | | } |
| | | // 判断是否为永不过期 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, value); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修修改指定key-value键值对 (过期时间不变) |
| | | */ |
| | | @Override |
| | | public void update(String key, String value) { |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, value, true); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除Value |
| | | */ |
| | | @Override |
| | | public void delete(String key) { |
| | | RedisUtils.deleteObject(key); |
| | | } |
| | | |
| | | /** |
| | | * 获取Value的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public long getTimeout(String key) { |
| | | long timeout = RedisUtils.getTimeToLive(key); |
| | | return timeout < 0 ? timeout : timeout / 1000; |
| | | } |
| | | |
| | | /** |
| | | * 修改Value的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void updateTimeout(String key, long timeout) { |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取Object,如无返空 |
| | | */ |
| | | @Override |
| | | public Object getObject(String key) { |
| | | return RedisUtils.getCacheObject(key); |
| | | } |
| | | |
| | | /** |
| | | * 写入Object,并设定存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void setObject(String key, Object object, long timeout) { |
| | | if (timeout == 0 || timeout <= NOT_VALUE_EXPIRE) { |
| | | return; |
| | | } |
| | | // 判断是否为永不过期 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, object); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新Object (过期时间不变) |
| | | */ |
| | | @Override |
| | | public void updateObject(String key, Object object) { |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, object, true); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除Object |
| | | */ |
| | | @Override |
| | | public void deleteObject(String key) { |
| | | RedisUtils.deleteObject(key); |
| | | } |
| | | |
| | | /** |
| | | * 获取Object的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public long getObjectTimeout(String key) { |
| | | long timeout = RedisUtils.getTimeToLive(key); |
| | | return timeout < 0 ? timeout : timeout / 1000; |
| | | } |
| | | |
| | | /** |
| | | * 修改Object的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void updateObjectTimeout(String key, long timeout) { |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 搜索数据 |
| | | */ |
| | | @Override |
| | | public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) { |
| | | Collection<String> keys = RedisUtils.keys(prefix + "*" + keyword + "*"); |
| | | List<String> list = new ArrayList<>(keys); |
| | | return SaFoxUtil.searchList(list, start, size, sortType); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.satoken.core.service; |
| | | |
| | | import cn.dev33.satoken.stp.StpInterface; |
| | | import org.dromara.common.core.enums.UserType; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * sa-token 权限管理实现类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class SaPermissionImpl implements StpInterface { |
| | | |
| | | /** |
| | | * 获取菜单权限列表 |
| | | */ |
| | | @Override |
| | | public List<String> getPermissionList(Object loginId, String loginType) { |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | UserType userType = UserType.getUserType(loginUser.getUserType()); |
| | | if (userType == UserType.SYS_USER) { |
| | | return new ArrayList<>(loginUser.getMenuPermission()); |
| | | } else if (userType == UserType.APP_USER) { |
| | | // 其他端 自行根据业务编写 |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | /** |
| | | * 获取角色权限列表 |
| | | */ |
| | | @Override |
| | | public List<String> getRoleList(Object loginId, String loginType) { |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | UserType userType = UserType.getUserType(loginUser.getUserType()); |
| | | if (userType == UserType.SYS_USER) { |
| | | return new ArrayList<>(loginUser.getRolePermission()); |
| | | } else if (userType == UserType.APP_USER) { |
| | | // 其他端 自行根据业务编写 |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.satoken.utils; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.context.model.SaStorage; |
| | | import cn.dev33.satoken.session.SaSession; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.constant.UserConstants; |
| | | import org.dromara.common.core.enums.UserType; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | |
| | | import java.util.Set; |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | | * 登录鉴权助手 |
| | | * <p> |
| | | * user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app |
| | | * deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios |
| | | * 可以组成 用户类型与设备类型多对多的 权限灵活控制 |
| | | * <p> |
| | | * 多用户体系 针对 多种用户类型 但权限控制不一致 |
| | | * 可以组成 多用户类型表与多设备类型 分别控制权限 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class LoginHelper { |
| | | |
| | | public static final String LOGIN_USER_KEY = "loginUser"; |
| | | public static final String TENANT_KEY = "tenantId"; |
| | | public static final String USER_KEY = "userId"; |
| | | public static final String DEPT_KEY = "deptId"; |
| | | public static final String CLIENT_KEY = "clientid"; |
| | | public static final String TENANT_ADMIN_KEY = "isTenantAdmin"; |
| | | |
| | | /** |
| | | * 登录系统 基于 设备类型 |
| | | * 针对相同用户体系不同设备 |
| | | * |
| | | * @param loginUser 登录用户信息 |
| | | * @param model 配置参数 |
| | | */ |
| | | public static void login(LoginUser loginUser, SaLoginModel model) { |
| | | SaStorage storage = SaHolder.getStorage(); |
| | | storage.set(LOGIN_USER_KEY, loginUser); |
| | | storage.set(TENANT_KEY, loginUser.getTenantId()); |
| | | storage.set(USER_KEY, loginUser.getUserId()); |
| | | storage.set(DEPT_KEY, loginUser.getDeptId()); |
| | | model = ObjectUtil.defaultIfNull(model, new SaLoginModel()); |
| | | StpUtil.login(loginUser.getLoginId(), |
| | | model.setExtra(TENANT_KEY, loginUser.getTenantId()) |
| | | .setExtra(USER_KEY, loginUser.getUserId()) |
| | | .setExtra(DEPT_KEY, loginUser.getDeptId())); |
| | | SaSession tokenSession = StpUtil.getTokenSession(); |
| | | tokenSession.updateTimeout(model.getTimeout()); |
| | | tokenSession.set(LOGIN_USER_KEY, loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户(多级缓存) |
| | | */ |
| | | public static LoginUser getLoginUser() { |
| | | return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> { |
| | | SaSession session = StpUtil.getTokenSession(); |
| | | if (ObjectUtil.isNull(session)) { |
| | | return null; |
| | | } |
| | | return session.get(LOGIN_USER_KEY); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户基于token |
| | | */ |
| | | public static LoginUser getLoginUser(String token) { |
| | | SaSession session = StpUtil.getTokenSessionByToken(token); |
| | | if (ObjectUtil.isNull(session)) { |
| | | return null; |
| | | } |
| | | return (LoginUser) session.get(LOGIN_USER_KEY); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户id |
| | | */ |
| | | public static Long getUserId() { |
| | | return Convert.toLong(getExtra(USER_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取租户ID |
| | | */ |
| | | public static String getTenantId() { |
| | | return Convert.toStr(getExtra(TENANT_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取部门ID |
| | | */ |
| | | public static Long getDeptId() { |
| | | return Convert.toLong(getExtra(DEPT_KEY)); |
| | | } |
| | | |
| | | private static Object getExtra(String key) { |
| | | return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key)); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户账户 |
| | | */ |
| | | public static String getUsername() { |
| | | return getLoginUser().getUsername(); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户类型 |
| | | */ |
| | | public static UserType getUserType() { |
| | | String loginId = StpUtil.getLoginIdAsString(); |
| | | return UserType.getUserType(loginId); |
| | | } |
| | | |
| | | /** |
| | | * 是否为超级管理员 |
| | | * |
| | | * @param userId 用户ID |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isSuperAdmin(Long userId) { |
| | | return UserConstants.SUPER_ADMIN_ID.equals(userId); |
| | | } |
| | | |
| | | public static boolean isSuperAdmin() { |
| | | return isSuperAdmin(getUserId()); |
| | | } |
| | | |
| | | /** |
| | | * 是否为超级管理员 |
| | | * |
| | | * @param rolePermission 角色权限标识组 |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isTenantAdmin(Set<String> rolePermission) { |
| | | return rolePermission.contains(TenantConstants.TENANT_ADMIN_ROLE_KEY); |
| | | } |
| | | |
| | | public static boolean isTenantAdmin() { |
| | | Object value = getStorageIfAbsentSet(TENANT_ADMIN_KEY, () -> { |
| | | return isTenantAdmin(getLoginUser().getRolePermission()); |
| | | }); |
| | | return Convert.toBool(value); |
| | | } |
| | | |
| | | public static boolean isLogin() { |
| | | return getLoginUser() != null; |
| | | } |
| | | |
| | | public static Object getStorageIfAbsentSet(String key, Supplier<Object> handle) { |
| | | try { |
| | | Object obj = SaHolder.getStorage().get(key); |
| | | if (ObjectUtil.isNull(obj)) { |
| | | obj = handle.get(); |
| | | SaHolder.getStorage().set(key, obj); |
| | | } |
| | | return obj; |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.satoken.config.SaTokenConfiguration |
New file |
| | |
| | | # 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖 |
| | | # Sa-Token配置 |
| | | sa-token: |
| | | # 允许动态设置 token 有效期 |
| | | dynamic-active-timeout: true |
| | | # 允许从 请求参数 读取 token |
| | | is-read-body: true |
| | | # 允许从 header 读取 token |
| | | is-read-header: true |
| | | # 关闭 cookie 鉴权 从根源杜绝 csrf 漏洞风险 |
| | | is-read-cookie: false |
| | | # token前缀 |
| | | token-prefix: "Bearer" |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-seata</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-seata 分布式事务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.apache.dubbo.extensions</groupId> |
| | | <artifactId>dubbo-filter-seata</artifactId> |
| | | <version>1.0.1</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>io.seata</groupId> |
| | | <artifactId>seata-core</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | <!-- SpringBoot Seata --> |
| | | <dependency> |
| | | <groupId>com.alibaba.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-alibaba-seata</artifactId> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.apache.logging.log4j</groupId> |
| | | <artifactId>*</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>org.apache.dubbo.extensions</groupId> |
| | | <artifactId>dubbo-filter-seata</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.seata.config; |
| | | |
| | | import org.dromara.common.core.factory.YmlPropertySourceFactory; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.PropertySource; |
| | | |
| | | /** |
| | | * seata 配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | @PropertySource(value = "classpath:common-seata.yml", factory = YmlPropertySourceFactory.class) |
| | | public class SeataConfiguration { |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.seata.config.SeataConfiguration |
New file |
| | |
| | | # 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖 |
| | | # seata配置 |
| | | seata: |
| | | config: |
| | | type: nacos |
| | | nacos: |
| | | server-addr: ${spring.cloud.nacos.server-addr} |
| | | group: ${spring.cloud.nacos.config.group} |
| | | namespace: ${spring.profiles.active} |
| | | data-id: seata-server.properties |
| | | registry: |
| | | type: nacos |
| | | nacos: |
| | | application: ruoyi-seata-server |
| | | server-addr: ${spring.cloud.nacos.server-addr} |
| | | group: ${spring.cloud.nacos.discovery.group} |
| | | namespace: ${spring.profiles.active} |
| | | # 关闭自动代理 |
| | | enable-auto-data-source-proxy: false |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-security</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-security 安全模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Sa-Token 权限认证, 在线文档:http://sa-token.dev33.cn/ --> |
| | | <dependency> |
| | | <groupId>cn.dev33</groupId> |
| | | <artifactId>sa-token-spring-boot3-starter</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.security.config; |
| | | |
| | | import cn.dev33.satoken.SaManager; |
| | | import cn.dev33.satoken.filter.SaServletFilter; |
| | | import cn.dev33.satoken.interceptor.SaInterceptor; |
| | | import cn.dev33.satoken.same.SaSameUtil; |
| | | import cn.dev33.satoken.util.SaResult; |
| | | import org.dromara.common.core.constant.HttpStatus; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | /** |
| | | * 权限安全配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | public class SecurityConfiguration implements WebMvcConfigurer { |
| | | |
| | | /** |
| | | * 注册sa-token的拦截器 |
| | | */ |
| | | @Override |
| | | public void addInterceptors(InterceptorRegistry registry) { |
| | | // 注册路由拦截器,自定义验证规则 |
| | | registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**"); |
| | | } |
| | | |
| | | /** |
| | | * 校验是否从网关转发 |
| | | */ |
| | | @Bean |
| | | public SaServletFilter getSaServletFilter() { |
| | | return new SaServletFilter() |
| | | .addInclude("/**") |
| | | .addExclude("/actuator/**") |
| | | .setAuth(obj -> { |
| | | if (SaManager.getConfig().getCheckSameToken()) { |
| | | SaSameUtil.checkCurrentRequestToken(); |
| | | } |
| | | }) |
| | | .setError(e -> SaResult.error("认证失败,无法访问系统资源").setCode(HttpStatus.UNAUTHORIZED)); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.security.handler; |
| | | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.exception.NotPermissionException; |
| | | import cn.dev33.satoken.exception.NotRoleException; |
| | | import cn.dev33.satoken.exception.SameTokenInvalidException; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpStatus; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.exception.base.BaseException; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.springframework.context.support.DefaultMessageSourceResolvable; |
| | | import org.springframework.validation.BindException; |
| | | import org.springframework.web.HttpRequestMethodNotSupportedException; |
| | | import org.springframework.web.bind.MethodArgumentNotValidException; |
| | | import org.springframework.web.bind.MissingPathVariableException; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| | | |
| | | /** |
| | | * 全局异常处理器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RestControllerAdvice |
| | | public class GlobalExceptionHandler { |
| | | |
| | | /** |
| | | * 权限码异常 |
| | | */ |
| | | @ExceptionHandler(NotPermissionException.class) |
| | | public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权"); |
| | | } |
| | | |
| | | /** |
| | | * 角色权限异常 |
| | | */ |
| | | @ExceptionHandler(NotRoleException.class) |
| | | public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权"); |
| | | } |
| | | |
| | | /** |
| | | * 认证失败 |
| | | */ |
| | | @ExceptionHandler(NotLoginException.class) |
| | | public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"); |
| | | } |
| | | |
| | | /** |
| | | * 无效认证 |
| | | */ |
| | | @ExceptionHandler(SameTokenInvalidException.class) |
| | | public R<Void> handleSameTokenInvalidException(SameTokenInvalidException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',内网认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"); |
| | | } |
| | | |
| | | /** |
| | | * 请求方式不支持 |
| | | */ |
| | | @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| | | public R<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, |
| | | HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 业务异常 |
| | | */ |
| | | @ExceptionHandler(ServiceException.class) |
| | | public R<Void> handleServiceException(ServiceException e, HttpServletRequest request) { |
| | | log.error(e.getMessage()); |
| | | Integer code = e.getCode(); |
| | | return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 业务异常 |
| | | */ |
| | | @ExceptionHandler(BaseException.class) |
| | | public R<Void> handleBaseException(BaseException e, HttpServletRequest request) { |
| | | log.error(e.getMessage()); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 请求路径中缺少必需的路径变量 |
| | | */ |
| | | @ExceptionHandler(MissingPathVariableException.class) |
| | | public R<Void> handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI); |
| | | return R.fail(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName())); |
| | | } |
| | | |
| | | /** |
| | | * 请求参数类型不匹配 |
| | | */ |
| | | @ExceptionHandler(MethodArgumentTypeMismatchException.class) |
| | | public R<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI); |
| | | return R.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); |
| | | } |
| | | |
| | | /** |
| | | * 拦截未知的运行时异常 |
| | | */ |
| | | @ExceptionHandler(RuntimeException.class) |
| | | public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',发生未知异常.", requestURI, e); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 系统异常 |
| | | */ |
| | | @ExceptionHandler(Exception.class) |
| | | public R<Void> handleException(Exception e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',发生系统异常.", requestURI, e); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(BindException.class) |
| | | public R<Void> handleBindException(BindException e) { |
| | | log.error(e.getMessage()); |
| | | String message = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ", "); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(ConstraintViolationException.class) |
| | | public R<Void> constraintViolationException(ConstraintViolationException e) { |
| | | log.error(e.getMessage()); |
| | | String message = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ", "); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(MethodArgumentNotValidException.class) |
| | | public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { |
| | | log.error(e.getMessage()); |
| | | String message = e.getBindingResult().getFieldError().getDefaultMessage(); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.security.handler.GlobalExceptionHandler |
| | | org.dromara.common.security.config.SecurityConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-sensitive</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-sensitive 脱敏模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.sensitive.annotation; |
| | | |
| | | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; |
| | | import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| | | import org.dromara.common.sensitive.core.SensitiveStrategy; |
| | | import org.dromara.common.sensitive.handler.SensitiveHandler; |
| | | |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | /** |
| | | * 数据脱敏注解 |
| | | * |
| | | * @author zhujie |
| | | */ |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target(ElementType.FIELD) |
| | | @JacksonAnnotationsInside |
| | | @JsonSerialize(using = SensitiveHandler.class) |
| | | public @interface Sensitive { |
| | | SensitiveStrategy strategy(); |
| | | |
| | | String roleKey() default ""; |
| | | |
| | | String perms() default ""; |
| | | } |
New file |
| | |
| | | package org.dromara.common.sensitive.core; |
| | | |
| | | /** |
| | | * 脱敏服务 |
| | | * 默认管理员不过滤 |
| | | * 需自行根据业务重写实现 |
| | | * |
| | | * @author Lion Li |
| | | * @version 3.6.0 |
| | | */ |
| | | public interface SensitiveService { |
| | | |
| | | /** |
| | | * 是否脱敏 |
| | | */ |
| | | boolean isSensitive(String roleKey, String perms); |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.sensitive.core; |
| | | |
| | | import cn.hutool.core.util.DesensitizedUtil; |
| | | import lombok.AllArgsConstructor; |
| | | |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * 脱敏策略 |
| | | * |
| | | * @author Yjoioooo |
| | | * @version 3.6.0 |
| | | */ |
| | | @AllArgsConstructor |
| | | public enum SensitiveStrategy { |
| | | |
| | | /** |
| | | * 身份证脱敏 |
| | | */ |
| | | ID_CARD(s -> DesensitizedUtil.idCardNum(s, 3, 4)), |
| | | |
| | | /** |
| | | * 手机号脱敏 |
| | | */ |
| | | PHONE(DesensitizedUtil::mobilePhone), |
| | | |
| | | /** |
| | | * 地址脱敏 |
| | | */ |
| | | ADDRESS(s -> DesensitizedUtil.address(s, 8)), |
| | | |
| | | /** |
| | | * 邮箱脱敏 |
| | | */ |
| | | EMAIL(DesensitizedUtil::email), |
| | | |
| | | /** |
| | | * 银行卡 |
| | | */ |
| | | BANK_CARD(DesensitizedUtil::bankCard); |
| | | |
| | | //可自行添加其他脱敏策略 |
| | | |
| | | private final Function<String, String> desensitizer; |
| | | |
| | | public Function<String, String> desensitizer() { |
| | | return desensitizer; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.sensitive.handler; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.core.JsonGenerator; |
| | | import com.fasterxml.jackson.databind.BeanProperty; |
| | | import com.fasterxml.jackson.databind.JsonMappingException; |
| | | import com.fasterxml.jackson.databind.JsonSerializer; |
| | | import com.fasterxml.jackson.databind.SerializerProvider; |
| | | import com.fasterxml.jackson.databind.ser.ContextualSerializer; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.sensitive.annotation.Sensitive; |
| | | import org.dromara.common.sensitive.core.SensitiveService; |
| | | import org.dromara.common.sensitive.core.SensitiveStrategy; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeansException; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 数据脱敏json序列化工具 |
| | | * |
| | | * @author Yjoioooo |
| | | */ |
| | | @Slf4j |
| | | public class SensitiveHandler extends JsonSerializer<String> implements ContextualSerializer { |
| | | |
| | | private SensitiveStrategy strategy; |
| | | private String roleKey; |
| | | private String perms; |
| | | |
| | | @Override |
| | | public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | try { |
| | | SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class); |
| | | if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(roleKey, perms)) { |
| | | gen.writeString(strategy.desensitizer().apply(value)); |
| | | } else { |
| | | gen.writeString(value); |
| | | } |
| | | } catch (BeansException e) { |
| | | log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage()); |
| | | gen.writeString(value); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { |
| | | Sensitive annotation = property.getAnnotation(Sensitive.class); |
| | | if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) { |
| | | this.strategy = annotation.strategy(); |
| | | this.roleKey = annotation.roleKey(); |
| | | this.perms = annotation.perms(); |
| | | return this; |
| | | } |
| | | return prov.findValueSerializer(property.getType(), property); |
| | | } |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-sentinel</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-sentinel 限流模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <!-- SpringCloud Alibaba Sentinel --> |
| | | <dependency> |
| | | <groupId>com.alibaba.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Sentinel Datasource Nacos --> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-datasource-nacos</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba.csp</groupId> |
| | | <artifactId>sentinel-apache-dubbo3-adapter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.dataformat</groupId> |
| | | <artifactId>jackson-dataformat-xml</artifactId> |
| | | <optional>true</optional> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-autoconfigure</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | /* |
| | | * Copyright 2013-2018 the original author or authors. |
| | | * |
| | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | * you may not use this file except in compliance with the License. |
| | | * You may obtain a copy of the License at |
| | | * |
| | | * https://www.apache.org/licenses/LICENSE-2.0 |
| | | * |
| | | * Unless required by applicable law or agreed to in writing, software |
| | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | * See the License for the specific language governing permissions and |
| | | * limitations under the License. |
| | | */ |
| | | |
| | | package com.alibaba.cloud.sentinel.custom; |
| | | |
| | | import com.alibaba.cloud.commons.lang.StringUtils; |
| | | import com.alibaba.cloud.sentinel.SentinelProperties; |
| | | import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter; |
| | | import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter; |
| | | import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; |
| | | import com.alibaba.csp.sentinel.config.SentinelConfig; |
| | | import com.alibaba.csp.sentinel.init.InitExecutor; |
| | | import com.alibaba.csp.sentinel.log.LogBase; |
| | | import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; |
| | | import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; |
| | | import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; |
| | | import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; |
| | | import com.alibaba.csp.sentinel.slots.system.SystemRule; |
| | | import com.alibaba.csp.sentinel.transport.config.TransportConfig; |
| | | import com.fasterxml.jackson.databind.DeserializationFeature; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| | | import jakarta.annotation.PostConstruct; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.sentinel.config.properties.SentinelCustomProperties; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.cloud.client.ServiceInstance; |
| | | import org.springframework.cloud.client.discovery.DiscoveryClient; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.core.env.Environment; |
| | | |
| | | import java.util.List; |
| | | |
| | | import static com.alibaba.cloud.sentinel.SentinelConstants.BLOCK_PAGE_URL_CONF_KEY; |
| | | import static com.alibaba.csp.sentinel.config.SentinelConfig.setConfig; |
| | | |
| | | /** |
| | | * 改造sentinel自动配置 支持服务名注册 |
| | | * |
| | | * @author Lion Li |
| | | * |
| | | * @author xiaojing |
| | | * @author jiashuai.xie |
| | | * @author <a href="mailto:fangjian0423@gmail.com">Jim</a> |
| | | */ |
| | | @Configuration(proxyBeanMethods = false) |
| | | @ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true) |
| | | @EnableConfigurationProperties({SentinelProperties.class, SentinelCustomProperties.class}) |
| | | public class SentinelAutoConfiguration { |
| | | |
| | | @Value("${project.name:${spring.application.name:}}") |
| | | private String projectName; |
| | | |
| | | @Autowired |
| | | private SentinelProperties properties; |
| | | @Autowired |
| | | private SentinelCustomProperties customProperties; |
| | | @Autowired |
| | | private DiscoveryClient discoveryClient; |
| | | |
| | | @PostConstruct |
| | | private void init() { |
| | | if (StringUtils.isEmpty(System.getProperty(LogBase.LOG_DIR)) |
| | | && StringUtils.isNotBlank(properties.getLog().getDir())) { |
| | | System.setProperty(LogBase.LOG_DIR, properties.getLog().getDir()); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(LogBase.LOG_NAME_USE_PID)) |
| | | && properties.getLog().isSwitchPid()) { |
| | | System.setProperty(LogBase.LOG_NAME_USE_PID, |
| | | String.valueOf(properties.getLog().isSwitchPid())); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(SentinelConfig.APP_NAME_PROP_KEY)) |
| | | && StringUtils.isNotBlank(projectName)) { |
| | | System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, projectName); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT)) |
| | | && StringUtils.isNotBlank(properties.getTransport().getPort())) { |
| | | System.setProperty(TransportConfig.SERVER_PORT, |
| | | properties.getTransport().getPort()); |
| | | } |
| | | |
| | | if (StringUtils.isNotBlank(customProperties.getServerName())) { |
| | | List<ServiceInstance> instances = discoveryClient.getInstances(customProperties.getServerName()); |
| | | String serverList = StreamUtils.join(instances, instance -> |
| | | String.format("http://%s:%s", instance.getHost(), instance.getPort())); |
| | | System.setProperty(TransportConfig.CONSOLE_SERVER, serverList); |
| | | } else { |
| | | if (StringUtils.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER)) |
| | | && StringUtils.isNotBlank(properties.getTransport().getDashboard())) { |
| | | System.setProperty(TransportConfig.CONSOLE_SERVER, |
| | | properties.getTransport().getDashboard()); |
| | | } |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_INTERVAL_MS)) |
| | | && StringUtils |
| | | .isNotBlank(properties.getTransport().getHeartbeatIntervalMs())) { |
| | | System.setProperty(TransportConfig.HEARTBEAT_INTERVAL_MS, |
| | | properties.getTransport().getHeartbeatIntervalMs()); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_CLIENT_IP)) |
| | | && StringUtils.isNotBlank(properties.getTransport().getClientIp())) { |
| | | System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, |
| | | properties.getTransport().getClientIp()); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(SentinelConfig.CHARSET)) |
| | | && StringUtils.isNotBlank(properties.getMetric().getCharset())) { |
| | | System.setProperty(SentinelConfig.CHARSET, |
| | | properties.getMetric().getCharset()); |
| | | } |
| | | if (StringUtils |
| | | .isEmpty(System.getProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE)) |
| | | && StringUtils.isNotBlank(properties.getMetric().getFileSingleSize())) { |
| | | System.setProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE, |
| | | properties.getMetric().getFileSingleSize()); |
| | | } |
| | | if (StringUtils |
| | | .isEmpty(System.getProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT)) |
| | | && StringUtils.isNotBlank(properties.getMetric().getFileTotalCount())) { |
| | | System.setProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT, |
| | | properties.getMetric().getFileTotalCount()); |
| | | } |
| | | if (StringUtils.isEmpty(System.getProperty(SentinelConfig.COLD_FACTOR)) |
| | | && StringUtils.isNotBlank(properties.getFlow().getColdFactor())) { |
| | | System.setProperty(SentinelConfig.COLD_FACTOR, |
| | | properties.getFlow().getColdFactor()); |
| | | } |
| | | if (StringUtils.isNotBlank(properties.getBlockPage())) { |
| | | setConfig(BLOCK_PAGE_URL_CONF_KEY, properties.getBlockPage()); |
| | | } |
| | | |
| | | // earlier initialize |
| | | if (properties.isEager()) { |
| | | InitExecutor.doInit(); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Bean |
| | | @ConditionalOnMissingBean |
| | | public SentinelResourceAspect sentinelResourceAspect() { |
| | | return new SentinelResourceAspect(); |
| | | } |
| | | |
| | | @Bean |
| | | @ConditionalOnMissingBean |
| | | @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") |
| | | @ConditionalOnProperty(name = "resttemplate.sentinel.enabled", havingValue = "true", |
| | | matchIfMissing = true) |
| | | public SentinelBeanPostProcessor sentinelBeanPostProcessor( |
| | | ApplicationContext applicationContext) { |
| | | return new SentinelBeanPostProcessor(applicationContext); |
| | | } |
| | | |
| | | @Bean |
| | | @ConditionalOnMissingBean |
| | | public SentinelDataSourceHandler sentinelDataSourceHandler( |
| | | DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties, |
| | | Environment env) { |
| | | return new SentinelDataSourceHandler(beanFactory, sentinelProperties, env); |
| | | } |
| | | |
| | | @ConditionalOnClass(ObjectMapper.class) |
| | | @Configuration(proxyBeanMethods = false) |
| | | protected static class SentinelConverterConfiguration { |
| | | |
| | | @Configuration(proxyBeanMethods = false) |
| | | protected static class SentinelJsonConfiguration { |
| | | |
| | | private ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | public SentinelJsonConfiguration() { |
| | | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, |
| | | false); |
| | | } |
| | | |
| | | @Bean("sentinel-json-flow-converter") |
| | | public JsonConverter jsonFlowConverter() { |
| | | return new JsonConverter(objectMapper, FlowRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-json-degrade-converter") |
| | | public JsonConverter jsonDegradeConverter() { |
| | | return new JsonConverter(objectMapper, DegradeRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-json-system-converter") |
| | | public JsonConverter jsonSystemConverter() { |
| | | return new JsonConverter(objectMapper, SystemRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-json-authority-converter") |
| | | public JsonConverter jsonAuthorityConverter() { |
| | | return new JsonConverter(objectMapper, AuthorityRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-json-param-flow-converter") |
| | | public JsonConverter jsonParamFlowConverter() { |
| | | return new JsonConverter(objectMapper, ParamFlowRule.class); |
| | | } |
| | | |
| | | } |
| | | |
| | | @ConditionalOnClass(XmlMapper.class) |
| | | @Configuration(proxyBeanMethods = false) |
| | | protected static class SentinelXmlConfiguration { |
| | | |
| | | private XmlMapper xmlMapper = new XmlMapper(); |
| | | |
| | | public SentinelXmlConfiguration() { |
| | | xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, |
| | | false); |
| | | } |
| | | |
| | | @Bean("sentinel-xml-flow-converter") |
| | | public XmlConverter xmlFlowConverter() { |
| | | return new XmlConverter(xmlMapper, FlowRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-xml-degrade-converter") |
| | | public XmlConverter xmlDegradeConverter() { |
| | | return new XmlConverter(xmlMapper, DegradeRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-xml-system-converter") |
| | | public XmlConverter xmlSystemConverter() { |
| | | return new XmlConverter(xmlMapper, SystemRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-xml-authority-converter") |
| | | public XmlConverter xmlAuthorityConverter() { |
| | | return new XmlConverter(xmlMapper, AuthorityRule.class); |
| | | } |
| | | |
| | | @Bean("sentinel-xml-param-flow-converter") |
| | | public XmlConverter xmlParamFlowConverter() { |
| | | return new XmlConverter(xmlMapper, ParamFlowRule.class); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.sentinel.config.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * sentinel自定义配置类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "spring.cloud.sentinel.transport") |
| | | public class SentinelCustomProperties { |
| | | |
| | | private String serverName; |
| | | |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-skylog</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-skylog skywalking日志收集模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <!-- skywalking 整合 logback --> |
| | | <dependency> |
| | | <groupId>org.apache.skywalking</groupId> |
| | | <artifactId>apm-toolkit-logback-1.x</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.apache.skywalking</groupId> |
| | | <artifactId>apm-toolkit-trace</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | |
| | | <included> |
| | | |
| | | <!-- 控制台输出 tid --> |
| | | <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> |
| | | <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> |
| | | <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout"> |
| | | <pattern>[%tid] ${console.log.pattern}</pattern> |
| | | </layout> |
| | | <charset>utf-8</charset> |
| | | </encoder> |
| | | </appender> |
| | | |
| | | <!-- skywalking 采集日志 --> |
| | | <appender name="sky_log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender"> |
| | | <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> |
| | | <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout"> |
| | | <pattern>[%tid] ${console.log.pattern}</pattern> |
| | | </layout> |
| | | <charset>utf-8</charset> |
| | | </encoder> |
| | | </appender> |
| | | |
| | | <root level="info"> |
| | | <appender-ref ref="console"/> |
| | | <appender-ref ref="sky_log"/> |
| | | </root> |
| | | </included> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-sms</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-sms 短信模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara.sms4j</groupId> |
| | | <artifactId>sms4j-spring-boot-starter</artifactId> |
| | | <exclusions> |
| | | <!-- 排除京东短信内存在的fastjson等待作者后续修复 --> |
| | | <exclusion> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.sms.config; |
| | | |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | |
| | | /** |
| | | * 短信配置类(暂时没用 预留扩展) |
| | | * |
| | | * @author Lion Li |
| | | * @version 4.2.0 |
| | | */ |
| | | @AutoConfiguration |
| | | public class SmsAutoConfiguration { |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.sms.config.SmsAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-social</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-social 授权认证 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>me.zhyd.oauth</groupId> |
| | | <artifactId>JustAuth</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.social.config; |
| | | |
| | | import me.zhyd.oauth.cache.AuthStateCache; |
| | | import org.dromara.common.social.config.properties.SocialProperties; |
| | | import org.dromara.common.social.utils.AuthRedisStateCache; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | |
| | | /** |
| | | * Social 配置属性 |
| | | * @author thiszhc |
| | | */ |
| | | @AutoConfiguration |
| | | @EnableConfigurationProperties(SocialProperties.class) |
| | | public class SocialAutoConfiguration { |
| | | |
| | | @Bean |
| | | public AuthStateCache authStateCache() { |
| | | return new AuthRedisStateCache(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.config.properties; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * 社交登录配置 |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | @Data |
| | | public class SocialLoginConfigProperties { |
| | | |
| | | /** |
| | | * 应用 ID |
| | | */ |
| | | private String clientId; |
| | | |
| | | /** |
| | | * 应用密钥 |
| | | */ |
| | | private String clientSecret; |
| | | |
| | | /** |
| | | * 回调地址 |
| | | */ |
| | | private String redirectUri; |
| | | |
| | | /** |
| | | * 是否获取unionId |
| | | */ |
| | | private boolean unionId; |
| | | |
| | | /** |
| | | * Coding 企业名称 |
| | | */ |
| | | private String codingGroupName; |
| | | |
| | | /** |
| | | * 支付宝公钥 |
| | | */ |
| | | private String alipayPublicKey; |
| | | |
| | | /** |
| | | * 企业微信应用ID |
| | | */ |
| | | private String agentId; |
| | | |
| | | /** |
| | | * stackoverflow api key |
| | | */ |
| | | private String stackOverflowKey; |
| | | |
| | | /** |
| | | * 设备ID |
| | | */ |
| | | private String deviceId; |
| | | |
| | | /** |
| | | * 客户端系统类型 |
| | | */ |
| | | private String clientOsType; |
| | | |
| | | /** |
| | | * maxkey 服务器地址 |
| | | */ |
| | | private String serverUrl; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.config.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Social 配置属性 |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | @Data |
| | | @Component |
| | | @ConfigurationProperties(prefix = "justauth") |
| | | public class SocialProperties { |
| | | |
| | | /** |
| | | * 是否启用 |
| | | */ |
| | | private Boolean enabled; |
| | | |
| | | /** |
| | | * 授权类型 |
| | | */ |
| | | private Map<String, SocialLoginConfigProperties> type; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.maxkey; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import me.zhyd.oauth.cache.AuthStateCache; |
| | | import me.zhyd.oauth.config.AuthConfig; |
| | | import me.zhyd.oauth.exception.AuthException; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthToken; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import me.zhyd.oauth.request.AuthDefaultRequest; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | |
| | | /** |
| | | * @author 长春叭哥 2023年03月26日 |
| | | */ |
| | | public class AuthMaxKeyRequest extends AuthDefaultRequest { |
| | | |
| | | public static final String SERVER_URL = SpringUtils.getProperty("justauth.type.maxkey.server-url"); |
| | | |
| | | /** |
| | | * 设定归属域 |
| | | */ |
| | | public AuthMaxKeyRequest(AuthConfig config) { |
| | | super(config, AuthMaxKeySource.MAXKEY); |
| | | } |
| | | |
| | | public AuthMaxKeyRequest(AuthConfig config, AuthStateCache authStateCache) { |
| | | super(config, AuthMaxKeySource.MAXKEY, authStateCache); |
| | | } |
| | | |
| | | @Override |
| | | protected AuthToken getAccessToken(AuthCallback authCallback) { |
| | | String body = doPostAuthorizationCode(authCallback.getCode()); |
| | | Dict object = JsonUtils.parseMap(body); |
| | | // oauth/token 验证异常 |
| | | if (object.containsKey("error")) { |
| | | throw new AuthException(object.getStr("error_description")); |
| | | } |
| | | // user 验证异常 |
| | | if (object.containsKey("message")) { |
| | | throw new AuthException(object.getStr("message")); |
| | | } |
| | | return AuthToken.builder() |
| | | .accessToken(object.getStr("access_token")) |
| | | .refreshToken(object.getStr("refresh_token")) |
| | | .idToken(object.getStr("id_token")) |
| | | .tokenType(object.getStr("token_type")) |
| | | .scope(object.getStr("scope")) |
| | | .build(); |
| | | } |
| | | |
| | | @Override |
| | | protected AuthUser getUserInfo(AuthToken authToken) { |
| | | String body = doGetUserInfo(authToken); |
| | | Dict object = JsonUtils.parseMap(body); |
| | | // oauth/token 验证异常 |
| | | if (object.containsKey("error")) { |
| | | throw new AuthException(object.getStr("error_description")); |
| | | } |
| | | // user 验证异常 |
| | | if (object.containsKey("message")) { |
| | | throw new AuthException(object.getStr("message")); |
| | | } |
| | | return AuthUser.builder() |
| | | .uuid(object.getStr("userId")) |
| | | .username(object.getStr("username")) |
| | | .nickname(object.getStr("displayName")) |
| | | .avatar(object.getStr("avatar_url")) |
| | | .blog(object.getStr("web_url")) |
| | | .company(object.getStr("organization")) |
| | | .location(object.getStr("location")) |
| | | .email(object.getStr("email")) |
| | | .remark(object.getStr("bio")) |
| | | .token(authToken) |
| | | .source(source.toString()) |
| | | .build(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.maxkey; |
| | | |
| | | import me.zhyd.oauth.config.AuthSource; |
| | | import me.zhyd.oauth.request.AuthDefaultRequest; |
| | | |
| | | /** |
| | | * Oauth2 默认接口说明 |
| | | * |
| | | * @author 长春叭哥 2023年03月26日 |
| | | * |
| | | */ |
| | | public enum AuthMaxKeySource implements AuthSource { |
| | | |
| | | /** |
| | | * 自己搭建的 maxkey 私服 |
| | | */ |
| | | MAXKEY { |
| | | |
| | | /** |
| | | * 授权的api |
| | | */ |
| | | @Override |
| | | public String authorize() { |
| | | return AuthMaxKeyRequest.SERVER_URL + "/sign/authz/oauth/v20/authorize"; |
| | | } |
| | | |
| | | /** |
| | | * 获取accessToken的api |
| | | */ |
| | | @Override |
| | | public String accessToken() { |
| | | return AuthMaxKeyRequest.SERVER_URL + "/sign/authz/oauth/v20/token"; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户信息的api |
| | | */ |
| | | @Override |
| | | public String userInfo() { |
| | | return AuthMaxKeyRequest.SERVER_URL + "/sign/api/oauth/v20/me"; |
| | | } |
| | | |
| | | /** |
| | | * 平台对应的 AuthRequest 实现类,必须继承自 {@link AuthDefaultRequest} |
| | | */ |
| | | @Override |
| | | public Class<? extends AuthDefaultRequest> getTargetClass() { |
| | | return AuthMaxKeyRequest.class; |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.utils; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import me.zhyd.oauth.cache.AuthStateCache; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | |
| | | import java.time.Duration; |
| | | |
| | | /** |
| | | * 授权状态缓存 |
| | | */ |
| | | @AllArgsConstructor |
| | | public class AuthRedisStateCache implements AuthStateCache { |
| | | |
| | | /** |
| | | * 存入缓存 |
| | | * |
| | | * @param key 缓存key |
| | | * @param value 缓存内容 |
| | | */ |
| | | @Override |
| | | public void cache(String key, String value) { |
| | | // 授权超时时间 默认三分钟 |
| | | RedisUtils.setCacheObject(GlobalConstants.SOCIAL_AUTH_CODE_KEY + key, value, Duration.ofMinutes(3)); |
| | | } |
| | | |
| | | /** |
| | | * 存入缓存 |
| | | * |
| | | * @param key 缓存key |
| | | * @param value 缓存内容 |
| | | * @param timeout 指定缓存过期时间(毫秒) |
| | | */ |
| | | @Override |
| | | public void cache(String key, String value, long timeout) { |
| | | RedisUtils.setCacheObject(GlobalConstants.SOCIAL_AUTH_CODE_KEY + key, value, Duration.ofMillis(timeout)); |
| | | } |
| | | |
| | | /** |
| | | * 获取缓存内容 |
| | | * |
| | | * @param key 缓存key |
| | | * @return 缓存内容 |
| | | */ |
| | | @Override |
| | | public String get(String key) { |
| | | return RedisUtils.getCacheObject(GlobalConstants.SOCIAL_AUTH_CODE_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 是否存在key,如果对应key的value值已过期,也返回false |
| | | * |
| | | * @param key 缓存key |
| | | * @return true:存在key,并且value没过期;false:key不存在或者已过期 |
| | | */ |
| | | @Override |
| | | public boolean containsKey(String key) { |
| | | return RedisUtils.hasKey(GlobalConstants.SOCIAL_AUTH_CODE_KEY + key); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.social.utils; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import me.zhyd.oauth.config.AuthConfig; |
| | | import me.zhyd.oauth.exception.AuthException; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthResponse; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import me.zhyd.oauth.request.*; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.social.config.properties.SocialLoginConfigProperties; |
| | | import org.dromara.common.social.config.properties.SocialProperties; |
| | | import org.dromara.common.social.maxkey.AuthMaxKeyRequest; |
| | | |
| | | /** |
| | | * 认证授权工具类 |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | public class SocialUtils { |
| | | |
| | | private static final AuthRedisStateCache STATE_CACHE = SpringUtils.getBean(AuthRedisStateCache.class); |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static AuthResponse<AuthUser> loginAuth(String source, String code, String state, SocialProperties socialProperties) throws AuthException { |
| | | AuthRequest authRequest = getAuthRequest(source, socialProperties); |
| | | AuthCallback callback = new AuthCallback(); |
| | | callback.setCode(code); |
| | | callback.setState(state); |
| | | return authRequest.login(callback); |
| | | } |
| | | |
| | | public static AuthRequest getAuthRequest(String source, SocialProperties socialProperties) throws AuthException { |
| | | SocialLoginConfigProperties obj = socialProperties.getType().get(source); |
| | | if (ObjectUtil.isNull(obj)) { |
| | | throw new AuthException("不支持的第三方登录类型"); |
| | | } |
| | | final AuthConfig.AuthConfigBuilder builder = AuthConfig.builder() |
| | | .clientId(obj.getClientId()) |
| | | .clientSecret(obj.getClientSecret()) |
| | | .redirectUri(obj.getRedirectUri()); |
| | | return switch (source.toLowerCase()) { |
| | | case "dingtalk" -> new AuthDingTalkRequest(builder.build(), STATE_CACHE); |
| | | case "baidu" -> new AuthBaiduRequest(builder.build(), STATE_CACHE); |
| | | case "github" -> new AuthGithubRequest(builder.build(), STATE_CACHE); |
| | | case "gitee" -> new AuthGiteeRequest(builder.build(), STATE_CACHE); |
| | | case "weibo" -> new AuthWeiboRequest(builder.build(), STATE_CACHE); |
| | | case "coding" -> new AuthCodingRequest(builder.build(), STATE_CACHE); |
| | | case "oschina" -> new AuthOschinaRequest(builder.build(), STATE_CACHE); |
| | | // 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1,所以这儿的回调地址使用的局域网内的ip |
| | | case "alipay_wallet" -> new AuthAlipayRequest(builder.build(), socialProperties.getType().get("alipay_wallet").getAlipayPublicKey(), STATE_CACHE); |
| | | case "qq" -> new AuthQqRequest(builder.build(), STATE_CACHE); |
| | | case "wechat_open" -> new AuthWeChatOpenRequest(builder.build(), STATE_CACHE); |
| | | case "taobao" -> new AuthTaobaoRequest(builder.build(), STATE_CACHE); |
| | | case "douyin" -> new AuthDouyinRequest(builder.build(), STATE_CACHE); |
| | | case "linkedin" -> new AuthLinkedinRequest(builder.build(), STATE_CACHE); |
| | | case "microsoft" -> new AuthMicrosoftRequest(builder.build(), STATE_CACHE); |
| | | case "renren" -> new AuthRenrenRequest(builder.build(), STATE_CACHE); |
| | | case "stack_overflow" -> new AuthStackOverflowRequest(builder.stackOverflowKey("").build(), STATE_CACHE); |
| | | case "huawei" -> new AuthHuaweiRequest(builder.build(), STATE_CACHE); |
| | | case "wechat_enterprise" -> new AuthWeChatEnterpriseQrcodeRequest(builder.agentId("").build(), STATE_CACHE); |
| | | case "gitlab" -> new AuthGitlabRequest(builder.build(), STATE_CACHE); |
| | | case "wechat_mp" -> new AuthWeChatMpRequest(builder.build(), STATE_CACHE); |
| | | case "aliyun" -> new AuthAliyunRequest(builder.build(), STATE_CACHE); |
| | | case "maxkey" -> new AuthMaxKeyRequest(builder.build(), STATE_CACHE); |
| | | default -> throw new AuthException("未获取到有效的Auth配置"); |
| | | }; |
| | | } |
| | | } |
| | | |
New file |
| | |
| | | org.dromara.common.social.config.SocialAutoConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-tenant</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-tenant 租户模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-mybatis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>transmittable-thread-local</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.tenant.config; |
| | | |
| | | import cn.dev33.satoken.dao.SaTokenDao; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import org.dromara.common.mybatis.config.MybatisPlusConfiguration; |
| | | import org.dromara.common.redis.config.RedisConfiguration; |
| | | import org.dromara.common.redis.config.properties.RedissonProperties; |
| | | import org.dromara.common.tenant.core.TenantSaTokenDao; |
| | | import org.dromara.common.tenant.handle.PlusTenantLineHandler; |
| | | import org.dromara.common.tenant.handle.TenantKeyPrefixHandler; |
| | | import org.dromara.common.tenant.manager.TenantSpringCacheManager; |
| | | import org.dromara.common.tenant.properties.TenantProperties; |
| | | import org.redisson.config.ClusterServersConfig; |
| | | import org.redisson.config.SingleServerConfig; |
| | | import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.cache.CacheManager; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Primary; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 租户配置类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @EnableConfigurationProperties(TenantProperties.class) |
| | | @AutoConfiguration(after = {RedisConfiguration.class}) |
| | | @ConditionalOnProperty(value = "tenant.enable", havingValue = "true") |
| | | public class TenantConfiguration { |
| | | |
| | | @ConditionalOnBean(MybatisPlusConfiguration.class) |
| | | @AutoConfiguration(after = {MybatisPlusConfiguration.class}) |
| | | static class MybatisPlusConfig { |
| | | |
| | | /** |
| | | * 初始化租户配置 |
| | | */ |
| | | @Bean |
| | | public boolean tenantInit(MybatisPlusInterceptor mybatisPlusInterceptor, |
| | | TenantProperties tenantProperties) { |
| | | List<InnerInterceptor> interceptors = new ArrayList<>(); |
| | | // 多租户插件 必须放到第一位 |
| | | interceptors.add(tenantLineInnerInterceptor(tenantProperties)); |
| | | interceptors.addAll(mybatisPlusInterceptor.getInterceptors()); |
| | | mybatisPlusInterceptor.setInterceptors(interceptors); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 多租户插件 |
| | | */ |
| | | public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties tenantProperties) { |
| | | return new TenantLineInnerInterceptor(new PlusTenantLineHandler(tenantProperties)); |
| | | } |
| | | } |
| | | |
| | | @Bean |
| | | public RedissonAutoConfigurationCustomizer tenantRedissonCustomizer(RedissonProperties redissonProperties) { |
| | | return config -> { |
| | | TenantKeyPrefixHandler nameMapper = new TenantKeyPrefixHandler(redissonProperties.getKeyPrefix()); |
| | | SingleServerConfig singleServerConfig = ReflectUtils.invokeGetter(config, "singleServerConfig"); |
| | | if (ObjectUtil.isNotNull(singleServerConfig)) { |
| | | // 使用单机模式 |
| | | // 设置多租户 redis key前缀 |
| | | singleServerConfig.setNameMapper(nameMapper); |
| | | ReflectUtils.invokeSetter(config, "singleServerConfig", singleServerConfig); |
| | | } |
| | | ClusterServersConfig clusterServersConfig = ReflectUtils.invokeGetter(config, "clusterServersConfig"); |
| | | // 集群配置方式 参考下方注释 |
| | | if (ObjectUtil.isNotNull(clusterServersConfig)) { |
| | | // 设置多租户 redis key前缀 |
| | | clusterServersConfig.setNameMapper(nameMapper); |
| | | ReflectUtils.invokeSetter(config, "clusterServersConfig", clusterServersConfig); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 多租户缓存管理器 |
| | | */ |
| | | @Primary |
| | | @Bean |
| | | public CacheManager tenantCacheManager() { |
| | | return new TenantSpringCacheManager(); |
| | | } |
| | | |
| | | /** |
| | | * 多租户鉴权dao实现 |
| | | */ |
| | | @Primary |
| | | @Bean |
| | | public SaTokenDao tenantSaTokenDao() { |
| | | return new TenantSaTokenDao(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.core; |
| | | |
| | | import org.dromara.common.mybatis.core.domain.BaseEntity; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | /** |
| | | * 租户基类 |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = true) |
| | | public class TenantEntity extends BaseEntity { |
| | | |
| | | /** |
| | | * 租户编号 |
| | | */ |
| | | private String tenantId; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.core; |
| | | |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.satoken.core.dao.PlusSaTokenDao; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * SaToken 认证数据持久层 适配多租户 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class TenantSaTokenDao extends PlusSaTokenDao { |
| | | |
| | | @Override |
| | | public String get(String key) { |
| | | return super.get(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | @Override |
| | | public void set(String key, String value, long timeout) { |
| | | super.set(GlobalConstants.GLOBAL_REDIS_KEY + key, value, timeout); |
| | | } |
| | | |
| | | /** |
| | | * 修修改指定key-value键值对 (过期时间不变) |
| | | */ |
| | | @Override |
| | | public void update(String key, String value) { |
| | | long expire = getTimeout(key); |
| | | // -2 = 无此键 |
| | | if (expire == NOT_VALUE_EXPIRE) { |
| | | return; |
| | | } |
| | | this.set(key, value, expire); |
| | | } |
| | | |
| | | /** |
| | | * 删除Value |
| | | */ |
| | | @Override |
| | | public void delete(String key) { |
| | | super.delete(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 获取Value的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public long getTimeout(String key) { |
| | | return super.getTimeout(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 修改Value的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void updateTimeout(String key, long timeout) { |
| | | // 判断是否想要设置为永久 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | long expire = getTimeout(key); |
| | | if (expire == NEVER_EXPIRE) { |
| | | // 如果其已经被设置为永久,则不作任何处理 |
| | | } else { |
| | | // 如果尚未被设置为永久,那么再次set一次 |
| | | this.set(key, this.get(key), timeout); |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(GlobalConstants.GLOBAL_REDIS_KEY + key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取Object,如无返空 |
| | | */ |
| | | @Override |
| | | public Object getObject(String key) { |
| | | return super.getObject(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 写入Object,并设定存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void setObject(String key, Object object, long timeout) { |
| | | super.setObject(GlobalConstants.GLOBAL_REDIS_KEY + key, object, timeout); |
| | | } |
| | | |
| | | /** |
| | | * 更新Object (过期时间不变) |
| | | */ |
| | | @Override |
| | | public void updateObject(String key, Object object) { |
| | | long expire = getObjectTimeout(key); |
| | | // -2 = 无此键 |
| | | if (expire == NOT_VALUE_EXPIRE) { |
| | | return; |
| | | } |
| | | this.setObject(key, object, expire); |
| | | } |
| | | |
| | | /** |
| | | * 删除Object |
| | | */ |
| | | @Override |
| | | public void deleteObject(String key) { |
| | | super.deleteObject(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 获取Object的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public long getObjectTimeout(String key) { |
| | | return super.getObjectTimeout(GlobalConstants.GLOBAL_REDIS_KEY + key); |
| | | } |
| | | |
| | | /** |
| | | * 修改Object的剩余存活时间 (单位: 秒) |
| | | */ |
| | | @Override |
| | | public void updateObjectTimeout(String key, long timeout) { |
| | | // 判断是否想要设置为永久 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | long expire = getObjectTimeout(key); |
| | | if (expire == NEVER_EXPIRE) { |
| | | // 如果其已经被设置为永久,则不作任何处理 |
| | | } else { |
| | | // 如果尚未被设置为永久,那么再次set一次 |
| | | this.setObject(key, this.getObject(key), timeout); |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(GlobalConstants.GLOBAL_REDIS_KEY + key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 搜索数据 |
| | | */ |
| | | @Override |
| | | public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) { |
| | | return super.searchData(GlobalConstants.GLOBAL_REDIS_KEY + prefix, keyword, start, size, sortType); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.exception; |
| | | |
| | | import org.dromara.common.core.exception.base.BaseException; |
| | | |
| | | import java.io.Serial; |
| | | |
| | | /** |
| | | * 租户异常类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class TenantException extends BaseException { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public TenantException(String code, Object... args) { |
| | | super("tenant", code, args, null); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.handle; |
| | | |
| | | import cn.hutool.core.collection.ListUtil; |
| | | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.NullValue; |
| | | import net.sf.jsqlparser.expression.StringValue; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.common.tenant.properties.TenantProperties; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 自定义租户处理器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | public class PlusTenantLineHandler implements TenantLineHandler { |
| | | |
| | | private final TenantProperties tenantProperties; |
| | | |
| | | @Override |
| | | public Expression getTenantId() { |
| | | String tenantId = TenantHelper.getTenantId(); |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | log.error("无法获取有效的租户id -> Null"); |
| | | return new NullValue(); |
| | | } |
| | | // 返回固定租户 |
| | | return new StringValue(tenantId); |
| | | } |
| | | |
| | | @Override |
| | | public boolean ignoreTable(String tableName) { |
| | | String tenantId = TenantHelper.getTenantId(); |
| | | // 判断是否有租户 |
| | | if (StringUtils.isNotBlank(tenantId)) { |
| | | // 不需要过滤租户的表 |
| | | List<String> excludes = tenantProperties.getExcludes(); |
| | | // 非业务表 |
| | | List<String> tables = ListUtil.toList( |
| | | "gen_table", |
| | | "gen_table_column" |
| | | ); |
| | | tables.addAll(excludes); |
| | | return tables.contains(tableName); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.handle; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.redis.handler.KeyPrefixHandler; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | |
| | | /** |
| | | * 多租户redis缓存key前缀处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class TenantKeyPrefixHandler extends KeyPrefixHandler { |
| | | |
| | | public TenantKeyPrefixHandler(String keyPrefix) { |
| | | super(keyPrefix); |
| | | } |
| | | |
| | | /** |
| | | * 增加前缀 |
| | | */ |
| | | @Override |
| | | public String map(String name) { |
| | | if (StringUtils.isBlank(name)) { |
| | | return null; |
| | | } |
| | | if (StringUtils.contains(name, GlobalConstants.GLOBAL_REDIS_KEY)) { |
| | | return super.map(name); |
| | | } |
| | | String tenantId = TenantHelper.getTenantId(); |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | log.error("无法获取有效的租户id -> Null"); |
| | | } |
| | | if (StringUtils.startsWith(name, tenantId + "")) { |
| | | // 如果存在则直接返回 |
| | | return super.map(name); |
| | | } |
| | | return super.map(tenantId + ":" + name); |
| | | } |
| | | |
| | | /** |
| | | * 去除前缀 |
| | | */ |
| | | @Override |
| | | public String unmap(String name) { |
| | | String unmap = super.unmap(name); |
| | | if (StringUtils.isBlank(unmap)) { |
| | | return null; |
| | | } |
| | | if (StringUtils.contains(name, GlobalConstants.GLOBAL_REDIS_KEY)) { |
| | | return super.unmap(name); |
| | | } |
| | | String tenantId = TenantHelper.getTenantId(); |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | log.error("无法获取有效的租户id -> Null"); |
| | | } |
| | | if (StringUtils.startsWith(unmap, tenantId + "")) { |
| | | // 如果存在则删除 |
| | | return unmap.substring((tenantId + ":").length()); |
| | | } |
| | | return unmap; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.helper; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.alibaba.ttl.TransmittableThreadLocal; |
| | | import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; |
| | | import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | | * 租户助手 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class TenantHelper { |
| | | |
| | | private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant"; |
| | | |
| | | private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); |
| | | |
| | | /** |
| | | * 租户功能是否启用 |
| | | */ |
| | | public static boolean isEnable() { |
| | | return Convert.toBool(SpringUtils.getProperty("tenant.enable"), false); |
| | | } |
| | | |
| | | /** |
| | | * 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭) |
| | | */ |
| | | public static void enableIgnore() { |
| | | InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); |
| | | } |
| | | |
| | | /** |
| | | * 关闭忽略租户 |
| | | */ |
| | | public static void disableIgnore() { |
| | | InterceptorIgnoreHelper.clearIgnoreStrategy(); |
| | | } |
| | | |
| | | /** |
| | | * 在忽略租户中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static void ignore(Runnable handle) { |
| | | enableIgnore(); |
| | | try { |
| | | handle.run(); |
| | | } finally { |
| | | disableIgnore(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 在忽略租户中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static <T> T ignore(Supplier<T> handle) { |
| | | enableIgnore(); |
| | | try { |
| | | return handle.get(); |
| | | } finally { |
| | | disableIgnore(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置动态租户(一直有效 需要手动清理) |
| | | * <p> |
| | | * 如果为未登录状态下 那么只在当前线程内生效 |
| | | */ |
| | | public static void setDynamic(String tenantId) { |
| | | if (!isEnable()) { |
| | | return; |
| | | } |
| | | if (!isLogin()) { |
| | | TEMP_DYNAMIC_TENANT.set(tenantId); |
| | | return; |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | RedisUtils.setCacheObject(cacheKey, tenantId); |
| | | SaHolder.getStorage().set(cacheKey, tenantId); |
| | | } |
| | | |
| | | /** |
| | | * 获取动态租户(一直有效 需要手动清理) |
| | | * <p> |
| | | * 如果为未登录状态下 那么只在当前线程内生效 |
| | | */ |
| | | public static String getDynamic() { |
| | | if (!isEnable()) { |
| | | return null; |
| | | } |
| | | if (!isLogin()) { |
| | | return TEMP_DYNAMIC_TENANT.get(); |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | String tenantId = (String) SaHolder.getStorage().get(cacheKey); |
| | | if (StringUtils.isNotBlank(tenantId)) { |
| | | return tenantId; |
| | | } |
| | | tenantId = RedisUtils.getCacheObject(cacheKey); |
| | | SaHolder.getStorage().set(cacheKey, tenantId); |
| | | return tenantId; |
| | | } |
| | | |
| | | /** |
| | | * 清除动态租户 |
| | | */ |
| | | public static void clearDynamic() { |
| | | if (!isEnable()) { |
| | | return; |
| | | } |
| | | if (!isLogin()) { |
| | | TEMP_DYNAMIC_TENANT.remove(); |
| | | return; |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | RedisUtils.deleteObject(cacheKey); |
| | | SaHolder.getStorage().delete(cacheKey); |
| | | } |
| | | |
| | | /** |
| | | * 在动态租户中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static void dynamic(String tenantId, Runnable handle) { |
| | | setDynamic(tenantId); |
| | | try { |
| | | handle.run(); |
| | | } finally { |
| | | clearDynamic(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 在动态租户中执行 |
| | | * |
| | | * @param handle 处理执行方法 |
| | | */ |
| | | public static <T> T dynamic(String tenantId, Supplier<T> handle) { |
| | | setDynamic(tenantId); |
| | | try { |
| | | return handle.get(); |
| | | } finally { |
| | | clearDynamic(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取当前租户id(动态租户优先) |
| | | */ |
| | | public static String getTenantId() { |
| | | if (!isEnable()) { |
| | | return null; |
| | | } |
| | | String tenantId = TenantHelper.getDynamic(); |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | tenantId = LoginHelper.getTenantId(); |
| | | } |
| | | return tenantId; |
| | | } |
| | | |
| | | private static boolean isLogin() { |
| | | try { |
| | | StpUtil.checkLogin(); |
| | | return true; |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.manager; |
| | | |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.redis.manager.PlusSpringCacheManager; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.springframework.cache.Cache; |
| | | |
| | | /** |
| | | * 重写 cacheName 处理方法 支持多租户 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class TenantSpringCacheManager extends PlusSpringCacheManager { |
| | | |
| | | public TenantSpringCacheManager() { |
| | | } |
| | | |
| | | @Override |
| | | public Cache getCache(String name) { |
| | | if (StringUtils.contains(name, GlobalConstants.GLOBAL_REDIS_KEY)) { |
| | | return super.getCache(name); |
| | | } |
| | | String tenantId = TenantHelper.getTenantId(); |
| | | if (StringUtils.startsWith(name, tenantId)) { |
| | | // 如果存在则直接返回 |
| | | return super.getCache(name); |
| | | } |
| | | return super.getCache(tenantId + ":" + name); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.tenant.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 租户 配置属性 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @ConfigurationProperties(prefix = "tenant") |
| | | public class TenantProperties { |
| | | |
| | | /** |
| | | * 是否启用 |
| | | */ |
| | | private Boolean enable; |
| | | |
| | | /** |
| | | * 排除表 |
| | | */ |
| | | private List<String> excludes; |
| | | |
| | | } |
New file |
| | |
| | | org.dromara.common.tenant.config.TenantConfiguration |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-translation</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-translation 通用翻译功能 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-dict</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-dubbo</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-api-resource</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.translation.annotation; |
| | | |
| | | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; |
| | | import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| | | import org.dromara.common.translation.core.handler.TranslationHandler; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 通用翻译注解 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Inherited |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target({ElementType.FIELD, ElementType.METHOD}) |
| | | @Documented |
| | | @JacksonAnnotationsInside |
| | | @JsonSerialize(using = TranslationHandler.class) |
| | | public @interface Translation { |
| | | |
| | | /** |
| | | * 类型 (需与实现类上的 {@link org.dromara.common.translation.annotation.TranslationType} 注解type对应) |
| | | * <p> |
| | | * 默认取当前字段的值 如果设置了 @{@link Translation#mapper()} 则取映射字段的值 |
| | | */ |
| | | String type(); |
| | | |
| | | /** |
| | | * 映射字段 (如果不为空则取此字段的值) |
| | | */ |
| | | String mapper() default ""; |
| | | |
| | | /** |
| | | * 其他条件 例如: 字典type(sys_user_sex) |
| | | */ |
| | | String other() default ""; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.annotation; |
| | | |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 翻译类型注解 (标注到{@link TranslationInterface} 的实现类) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Inherited |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target({ElementType.TYPE}) |
| | | @Documented |
| | | public @interface TranslationType { |
| | | |
| | | /** |
| | | * 类型 |
| | | */ |
| | | String type(); |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.config; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import org.dromara.common.translation.core.handler.TranslationBeanSerializerModifier; |
| | | import org.dromara.common.translation.core.handler.TranslationHandler; |
| | | import jakarta.annotation.PostConstruct; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 翻译模块配置类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @AutoConfiguration |
| | | public class TranslationConfig { |
| | | |
| | | @Autowired |
| | | private List<TranslationInterface<?>> list; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | Map<String, TranslationInterface<?>> map = new HashMap<>(list.size()); |
| | | for (TranslationInterface<?> trans : list) { |
| | | if (trans.getClass().isAnnotationPresent(TranslationType.class)) { |
| | | TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class); |
| | | map.put(annotation.type(), trans); |
| | | } else { |
| | | log.warn(trans.getClass().getName() + " 翻译实现类未标注 TranslationType 注解!"); |
| | | } |
| | | } |
| | | TranslationHandler.TRANSLATION_MAPPER.putAll(map); |
| | | // 设置 Bean 序列化修改器 |
| | | objectMapper.setSerializerFactory( |
| | | objectMapper.getSerializerFactory() |
| | | .withSerializerModifier(new TranslationBeanSerializerModifier())); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.constant; |
| | | |
| | | /** |
| | | * 翻译常量 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface TransConstant { |
| | | |
| | | /** |
| | | * 用户id转账号 |
| | | */ |
| | | String USER_ID_TO_NAME = "user_id_to_name"; |
| | | |
| | | /** |
| | | * 用户id转用户昵称 |
| | | */ |
| | | String USER_ID_TO_NICKNAME = "user_id_to_nickname"; |
| | | |
| | | |
| | | /** |
| | | * 部门id转名称 |
| | | */ |
| | | String DEPT_ID_TO_NAME = "dept_id_to_name"; |
| | | |
| | | /** |
| | | * 字典type转label |
| | | */ |
| | | String DICT_TYPE_TO_LABEL = "dict_type_to_label"; |
| | | |
| | | /** |
| | | * ossId转url |
| | | */ |
| | | String OSS_ID_TO_URL = "oss_id_to_url"; |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core; |
| | | |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | |
| | | /** |
| | | * 翻译接口 (实现类需标注 {@link TranslationType} 注解标明翻译类型) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface TranslationInterface<T> { |
| | | |
| | | /** |
| | | * 翻译 |
| | | * |
| | | * @param key 需要被翻译的键(不为空) |
| | | * @param other 其他参数 |
| | | * @return 返回键对应的值 |
| | | */ |
| | | T translation(Object key, String other); |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.handler; |
| | | |
| | | import com.fasterxml.jackson.databind.BeanDescription; |
| | | import com.fasterxml.jackson.databind.SerializationConfig; |
| | | import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; |
| | | import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Bean 序列化修改器 解决 Null 被单独处理问题 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class TranslationBeanSerializerModifier extends BeanSerializerModifier { |
| | | |
| | | @Override |
| | | public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, |
| | | List<BeanPropertyWriter> beanProperties) { |
| | | for (BeanPropertyWriter writer : beanProperties) { |
| | | // 如果序列化器为 TranslationHandler 的话 将 Null 值也交给他处理 |
| | | if (writer.getSerializer() instanceof TranslationHandler serializer) { |
| | | writer.assignNullSerializer(serializer); |
| | | } |
| | | } |
| | | return beanProperties; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.handler; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.core.JsonGenerator; |
| | | import com.fasterxml.jackson.databind.BeanProperty; |
| | | import com.fasterxml.jackson.databind.JsonMappingException; |
| | | import com.fasterxml.jackson.databind.JsonSerializer; |
| | | import com.fasterxml.jackson.databind.SerializerProvider; |
| | | import com.fasterxml.jackson.databind.ser.ContextualSerializer; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import org.dromara.common.translation.annotation.Translation; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * 翻译处理器 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class TranslationHandler extends JsonSerializer<Object> implements ContextualSerializer { |
| | | |
| | | /** |
| | | * 全局翻译实现类映射器 |
| | | */ |
| | | public static final Map<String, TranslationInterface<?>> TRANSLATION_MAPPER = new ConcurrentHashMap<>(); |
| | | |
| | | private Translation translation; |
| | | |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type()); |
| | | if (ObjectUtil.isNotNull(trans)) { |
| | | // 如果映射字段不为空 则取映射字段的值 |
| | | if (StringUtils.isNotBlank(translation.mapper())) { |
| | | value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper()); |
| | | } |
| | | // 如果为 null 直接写出 |
| | | if (ObjectUtil.isNull(value)) { |
| | | gen.writeNull(); |
| | | return; |
| | | } |
| | | Object result = trans.translation(value, translation.other()); |
| | | gen.writeObject(result); |
| | | } else { |
| | | gen.writeObject(value); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { |
| | | Translation translation = property.getAnnotation(Translation.class); |
| | | if (Objects.nonNull(translation)) { |
| | | this.translation = translation; |
| | | return this; |
| | | } |
| | | return prov.findValueSerializer(property.getType(), property); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.impl; |
| | | |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.constant.TransConstant; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import org.dromara.system.api.RemoteDeptService; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | |
| | | /** |
| | | * 部门翻译实现 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.DEPT_ID_TO_NAME) |
| | | public class DeptNameTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | @DubboReference |
| | | private RemoteDeptService remoteDeptService; |
| | | |
| | | @Override |
| | | public String translation(Object key, String other) { |
| | | return remoteDeptService.selectDeptNameByIds(key.toString()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.impl; |
| | | |
| | | import org.dromara.common.core.service.DictService; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.constant.TransConstant; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import lombok.AllArgsConstructor; |
| | | |
| | | /** |
| | | * 字典翻译实现 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL) |
| | | public class DictTypeTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | private final DictService dictService; |
| | | |
| | | @Override |
| | | public String translation(Object key, String other) { |
| | | if (key instanceof String && StringUtils.isNotBlank(other)) { |
| | | return dictService.getDictLabel(other, key.toString()); |
| | | } |
| | | return null; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.impl; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.constant.TransConstant; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import org.dromara.system.api.RemoteUserService; |
| | | |
| | | /** |
| | | * 用户昵称翻译实现 |
| | | * |
| | | * @author may |
| | | */ |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.USER_ID_TO_NICKNAME) |
| | | public class NicknameTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | @DubboReference |
| | | private RemoteUserService remoteUserService; |
| | | |
| | | @Override |
| | | public String translation(Object key, String other) { |
| | | return remoteUserService.selectNicknameById((Long) key); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.impl; |
| | | |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.constant.TransConstant; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import org.dromara.resource.api.RemoteFileService; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | |
| | | /** |
| | | * OSS翻译实现 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.OSS_ID_TO_URL) |
| | | public class OssUrlTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | @DubboReference(mock = "true") |
| | | private RemoteFileService ossService; |
| | | |
| | | @Override |
| | | public String translation(Object key, String other) { |
| | | return ossService.selectUrlByIds(key.toString()); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.translation.core.impl; |
| | | |
| | | import org.dromara.common.translation.annotation.TranslationType; |
| | | import org.dromara.common.translation.constant.TransConstant; |
| | | import org.dromara.common.translation.core.TranslationInterface; |
| | | import org.dromara.system.api.RemoteUserService; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.dubbo.config.annotation.DubboReference; |
| | | |
| | | /** |
| | | * 用户名翻译实现 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.USER_ID_TO_NAME) |
| | | public class UserNameTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | @DubboReference |
| | | private RemoteUserService remoteUserService; |
| | | |
| | | @Override |
| | | public String translation(Object key, String other) { |
| | | return remoteUserService.selectUserNameById((Long) key); |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.translation.config.TranslationConfig |
| | | org.dromara.common.translation.core.impl.DeptNameTranslationImpl |
| | | org.dromara.common.translation.core.impl.DictTypeTranslationImpl |
| | | org.dromara.common.translation.core.impl.OssUrlTranslationImpl |
| | | org.dromara.common.translation.core.impl.UserNameTranslationImpl |
| | | org.dromara.common.translation.core.impl.NicknameTranslationImpl |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-web</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-web web服务 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- SpringBoot Web容器 --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-web</artifactId> |
| | | <exclusions> |
| | | <exclusion> |
| | | <artifactId>spring-boot-starter-tomcat</artifactId> |
| | | <groupId>org.springframework.boot</groupId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <!-- web 容器使用 undertow 性能更强 --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-undertow</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- SpringBoot Actuator --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-actuator</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
| | | <optional>true</optional> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>net.dreamlu</groupId> |
| | | <artifactId>mica-metrics</artifactId> |
| | | <version>2.7.6</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>net.dreamlu</groupId> |
| | | <artifactId>mica-core</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>net.dreamlu</groupId> |
| | | <artifactId>mica-core</artifactId> |
| | | <version>2.7.6</version> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.web.config; |
| | | |
| | | import org.dromara.common.web.core.I18nLocaleResolver; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.web.servlet.LocaleResolver; |
| | | |
| | | /** |
| | | * 国际化配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration(before = WebMvcAutoConfiguration.class) |
| | | public class I18nConfig { |
| | | |
| | | @Bean |
| | | public LocaleResolver localeResolver() { |
| | | return new I18nLocaleResolver(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.web.config; |
| | | |
| | | import io.undertow.server.DefaultByteBufferPool; |
| | | import io.undertow.websockets.jsr.WebSocketDeploymentInfo; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; |
| | | import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
| | | |
| | | /** |
| | | * Undertow 自定义配置 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @AutoConfiguration |
| | | public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> { |
| | | |
| | | /** |
| | | * 设置 Undertow 的 websocket 缓冲池 |
| | | */ |
| | | @Override |
| | | public void customize(UndertowServletWebServerFactory factory) { |
| | | // 默认不直接分配内存 如果项目中使用了 websocket 建议直接分配 |
| | | factory.addDeploymentInfoCustomizers(deploymentInfo -> { |
| | | WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); |
| | | webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 512)); |
| | | deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo); |
| | | }); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.web.core; |
| | | |
| | | import org.dromara.common.core.domain.R; |
| | | |
| | | /** |
| | | * web层通用数据处理 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class BaseController { |
| | | |
| | | /** |
| | | * 响应返回结果 |
| | | * |
| | | * @param rows 影响行数 |
| | | * @return 操作结果 |
| | | */ |
| | | protected R<Void> toAjax(int rows) { |
| | | return rows > 0 ? R.ok() : R.fail(); |
| | | } |
| | | |
| | | /** |
| | | * 响应返回结果 |
| | | * |
| | | * @param result 结果 |
| | | * @return 操作结果 |
| | | */ |
| | | protected R<Void> toAjax(boolean result) { |
| | | return result ? R.ok() : R.fail(); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.web.core; |
| | | |
| | | import org.springframework.web.servlet.LocaleResolver; |
| | | |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.util.Locale; |
| | | |
| | | /** |
| | | * 获取请求头国际化信息 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class I18nLocaleResolver implements LocaleResolver { |
| | | |
| | | @Override |
| | | public Locale resolveLocale(HttpServletRequest httpServletRequest) { |
| | | String language = httpServletRequest.getHeader("content-language"); |
| | | Locale locale = Locale.getDefault(); |
| | | if (language != null && language.length() > 0) { |
| | | String[] split = language.split("_"); |
| | | locale = new Locale(split[0], split[1]); |
| | | } |
| | | return locale; |
| | | } |
| | | |
| | | @Override |
| | | public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.web.config.I18nConfig |
| | | org.dromara.common.web.config.UndertowConfig |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | |
| | | <included> |
| | | |
| | | <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"/> |
| | | |
| | | <!-- 控制台输出 --> |
| | | <appender name="file_console" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
| | | <file>${log.path}/console.log</file> |
| | | <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
| | | <!-- 日志文件名格式 --> |
| | | <fileNamePattern>${log.path}/console.%d{yyyy-MM-dd}.log</fileNamePattern> |
| | | <!-- 日志最大 1天 --> |
| | | <maxHistory>1</maxHistory> |
| | | </rollingPolicy> |
| | | <encoder> |
| | | <pattern>${log.pattern}</pattern> |
| | | <charset>utf-8</charset> |
| | | </encoder> |
| | | <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> |
| | | <!-- 过滤的级别 --> |
| | | <level>INFO</level> |
| | | </filter> |
| | | </appender> |
| | | |
| | | <!-- 系统日志输出 --> |
| | | <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
| | | <file>${log.path}/info.log</file> |
| | | <!-- 循环政策:基于时间创建日志文件 --> |
| | | <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
| | | <!-- 日志文件名格式 --> |
| | | <fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern> |
| | | <!-- 日志最大的历史 60天 --> |
| | | <maxHistory>60</maxHistory> |
| | | </rollingPolicy> |
| | | <encoder> |
| | | <pattern>${log.pattern}</pattern> |
| | | </encoder> |
| | | <filter class="ch.qos.logback.classic.filter.LevelFilter"> |
| | | <!-- 过滤的级别 --> |
| | | <level>INFO</level> |
| | | <!-- 匹配时的操作:接收(记录) --> |
| | | <onMatch>ACCEPT</onMatch> |
| | | <!-- 不匹配时的操作:拒绝(不记录) --> |
| | | <onMismatch>DENY</onMismatch> |
| | | </filter> |
| | | </appender> |
| | | |
| | | <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
| | | <file>${log.path}/error.log</file> |
| | | <!-- 循环政策:基于时间创建日志文件 --> |
| | | <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
| | | <!-- 日志文件名格式 --> |
| | | <fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern> |
| | | <!-- 日志最大的历史 60天 --> |
| | | <maxHistory>60</maxHistory> |
| | | </rollingPolicy> |
| | | <encoder> |
| | | <pattern>${log.pattern}</pattern> |
| | | </encoder> |
| | | <filter class="ch.qos.logback.classic.filter.LevelFilter"> |
| | | <!-- 过滤的级别 --> |
| | | <level>ERROR</level> |
| | | <!-- 匹配时的操作:接收(记录) --> |
| | | <onMatch>ACCEPT</onMatch> |
| | | <!-- 不匹配时的操作:拒绝(不记录) --> |
| | | <onMismatch>DENY</onMismatch> |
| | | </filter> |
| | | </appender> |
| | | |
| | | <!-- info异步输出 --> |
| | | <appender name="async_info" class="ch.qos.logback.classic.AsyncAppender"> |
| | | <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> |
| | | <discardingThreshold>0</discardingThreshold> |
| | | <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> |
| | | <queueSize>512</queueSize> |
| | | <!-- 添加附加的appender,最多只能添加一个 --> |
| | | <appender-ref ref="file_info"/> |
| | | </appender> |
| | | |
| | | <!-- error异步输出 --> |
| | | <appender name="async_error" class="ch.qos.logback.classic.AsyncAppender"> |
| | | <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> |
| | | <discardingThreshold>0</discardingThreshold> |
| | | <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> |
| | | <queueSize>512</queueSize> |
| | | <!-- 添加附加的appender,最多只能添加一个 --> |
| | | <appender-ref ref="file_error"/> |
| | | </appender> |
| | | |
| | | <!--系统操作日志--> |
| | | <root level="info"> |
| | | <appender-ref ref="async_info"/> |
| | | <appender-ref ref="async_error"/> |
| | | <appender-ref ref="file_console"/> |
| | | </root> |
| | | </included> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | <version>${revision}</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>ruoyi-common-websocket</artifactId> |
| | | |
| | | <description> |
| | | ruoyi-common-websocket 模块 |
| | | </description> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-core</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-redis</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-satoken</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.dromara</groupId> |
| | | <artifactId>ruoyi-common-json</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-websocket</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | </project> |
New file |
| | |
| | | package org.dromara.common.websocket.config; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import org.dromara.common.websocket.config.properties.WebSocketProperties; |
| | | import org.dromara.common.websocket.handler.PlusWebSocketHandler; |
| | | import org.dromara.common.websocket.interceptor.PlusWebSocketInterceptor; |
| | | import org.dromara.common.websocket.listener.WebSocketTopicListener; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.web.socket.WebSocketHandler; |
| | | import org.springframework.web.socket.config.annotation.EnableWebSocket; |
| | | import org.springframework.web.socket.config.annotation.WebSocketConfigurer; |
| | | import org.springframework.web.socket.server.HandshakeInterceptor; |
| | | |
| | | /** |
| | | * WebSocket 配置 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @AutoConfiguration |
| | | @ConditionalOnProperty(value = "websocket.enabled", havingValue = "true") |
| | | @EnableConfigurationProperties(WebSocketProperties.class) |
| | | @EnableWebSocket |
| | | public class WebSocketConfig { |
| | | |
| | | @Bean |
| | | public WebSocketConfigurer webSocketConfigurer(HandshakeInterceptor handshakeInterceptor, |
| | | WebSocketHandler webSocketHandler, |
| | | WebSocketProperties webSocketProperties) { |
| | | if (StrUtil.isBlank(webSocketProperties.getPath())) { |
| | | webSocketProperties.setPath("/websocket"); |
| | | } |
| | | |
| | | if (StrUtil.isBlank(webSocketProperties.getAllowedOrigins())) { |
| | | webSocketProperties.setAllowedOrigins("*"); |
| | | } |
| | | |
| | | return registry -> registry |
| | | .addHandler(webSocketHandler, webSocketProperties.getPath()) |
| | | .addInterceptors(handshakeInterceptor) |
| | | .setAllowedOrigins(webSocketProperties.getAllowedOrigins()); |
| | | } |
| | | |
| | | @Bean |
| | | public HandshakeInterceptor handshakeInterceptor() { |
| | | return new PlusWebSocketInterceptor(); |
| | | } |
| | | |
| | | @Bean |
| | | public WebSocketHandler webSocketHandler() { |
| | | return new PlusWebSocketHandler(); |
| | | } |
| | | |
| | | @Bean |
| | | public WebSocketTopicListener topicListener() { |
| | | return new WebSocketTopicListener(); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.config.properties; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | |
| | | /** |
| | | * WebSocket 配置项 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @ConfigurationProperties("websocket") |
| | | @Data |
| | | public class WebSocketProperties { |
| | | |
| | | private Boolean enabled; |
| | | |
| | | /** |
| | | * 路径 |
| | | */ |
| | | private String path; |
| | | |
| | | /** |
| | | * 设置访问源地址 |
| | | */ |
| | | private String allowedOrigins; |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.constant; |
| | | |
| | | /** |
| | | * websocket的常量配置 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | public interface WebSocketConstants { |
| | | /** |
| | | * websocketSession中的参数的key |
| | | */ |
| | | String LOGIN_USER_KEY = "loginUser"; |
| | | |
| | | /** |
| | | * 订阅的频道 |
| | | */ |
| | | String WEB_SOCKET_TOPIC = "global:websocket"; |
| | | |
| | | /** |
| | | * 前端心跳检查的命令 |
| | | */ |
| | | String PING = "ping"; |
| | | |
| | | /** |
| | | * 服务端心跳恢复的字符串 |
| | | */ |
| | | String PONG = "pong"; |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.dto; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serial; |
| | | import java.io.Serializable; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 消息的dto |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @Data |
| | | public class WebSocketMessageDto implements Serializable { |
| | | |
| | | @Serial |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 需要推送到的session key 列表 |
| | | */ |
| | | private List<Long> sessionKeys; |
| | | |
| | | /** |
| | | * 需要发送的消息 |
| | | */ |
| | | private String message; |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.handler; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.websocket.dto.WebSocketMessageDto; |
| | | import org.dromara.common.websocket.holder.WebSocketSessionHolder; |
| | | import org.dromara.common.websocket.utils.WebSocketUtils; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | import org.springframework.web.socket.*; |
| | | import org.springframework.web.socket.handler.AbstractWebSocketHandler; |
| | | |
| | | import java.util.List; |
| | | |
| | | import static org.dromara.common.websocket.constant.WebSocketConstants.LOGIN_USER_KEY; |
| | | |
| | | /** |
| | | * WebSocketHandler 实现类 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @Slf4j |
| | | public class PlusWebSocketHandler extends AbstractWebSocketHandler { |
| | | |
| | | /** |
| | | * 连接成功后 |
| | | */ |
| | | @Override |
| | | public void afterConnectionEstablished(WebSocketSession session) { |
| | | LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY); |
| | | WebSocketSessionHolder.addSession(loginUser.getUserId(), session); |
| | | log.info("[connect] sessionId: {},userId:{},userType:{}", session.getId(), loginUser.getUserId(), loginUser.getUserType()); |
| | | } |
| | | |
| | | /** |
| | | * 处理发送来的文本消息 |
| | | * |
| | | * @param session |
| | | * @param message |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { |
| | | LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY); |
| | | List<Long> userIds = List.of(loginUser.getUserId()); |
| | | WebSocketMessageDto webSocketMessageDto = new WebSocketMessageDto(); |
| | | webSocketMessageDto.setSessionKeys(userIds); |
| | | webSocketMessageDto.setMessage(message.getPayload()); |
| | | WebSocketUtils.publishMessage(webSocketMessageDto); |
| | | } |
| | | |
| | | @Override |
| | | protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception { |
| | | super.handleBinaryMessage(session, message); |
| | | } |
| | | |
| | | /** |
| | | * 心跳监测的回复 |
| | | * |
| | | * @param session |
| | | * @param message |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception { |
| | | WebSocketUtils.sendPongMessage(session); |
| | | } |
| | | |
| | | /** |
| | | * 连接出错时 |
| | | * |
| | | * @param session |
| | | * @param exception |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { |
| | | log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 连接关闭后 |
| | | * |
| | | * @param session |
| | | * @param status |
| | | */ |
| | | @Override |
| | | public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { |
| | | LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY); |
| | | WebSocketSessionHolder.removeSession(loginUser.getUserId()); |
| | | log.info("[disconnect] sessionId: {},userId:{},userType:{}", session.getId(), loginUser.getUserId(), loginUser.getUserType()); |
| | | } |
| | | |
| | | /** |
| | | * 是否支持分片消息 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean supportsPartialMessages() { |
| | | return false; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.holder; |
| | | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.web.socket.WebSocketSession; |
| | | |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * WebSocketSession 用于保存当前所有在线的会话信息 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class WebSocketSessionHolder { |
| | | |
| | | private static final Map<Long, WebSocketSession> USER_SESSION_MAP = new ConcurrentHashMap<>(); |
| | | |
| | | public static void addSession(Long sessionKey, WebSocketSession session) { |
| | | USER_SESSION_MAP.put(sessionKey, session); |
| | | } |
| | | |
| | | public static void removeSession(Long sessionKey) { |
| | | if (USER_SESSION_MAP.containsKey(sessionKey)) { |
| | | USER_SESSION_MAP.remove(sessionKey); |
| | | } |
| | | } |
| | | |
| | | public static WebSocketSession getSessions(Long sessionKey) { |
| | | return USER_SESSION_MAP.get(sessionKey); |
| | | } |
| | | |
| | | public static Set<Long> getSessionsAll() { |
| | | return USER_SESSION_MAP.keySet(); |
| | | } |
| | | |
| | | public static Boolean existSession(Long sessionKey) { |
| | | return USER_SESSION_MAP.containsKey(sessionKey); |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.interceptor; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.api.model.LoginUser; |
| | | import org.springframework.http.server.ServerHttpRequest; |
| | | import org.springframework.http.server.ServerHttpResponse; |
| | | import org.springframework.web.socket.WebSocketHandler; |
| | | import org.springframework.web.socket.server.HandshakeInterceptor; |
| | | |
| | | import java.util.Map; |
| | | |
| | | import static org.dromara.common.websocket.constant.WebSocketConstants.LOGIN_USER_KEY; |
| | | |
| | | /** |
| | | * WebSocket握手请求的拦截器 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @Slf4j |
| | | public class PlusWebSocketInterceptor implements HandshakeInterceptor { |
| | | |
| | | /** |
| | | * 握手前 |
| | | * |
| | | * @param request request |
| | | * @param response response |
| | | * @param wsHandler wsHandler |
| | | * @param attributes attributes |
| | | * @return 是否握手成功 |
| | | */ |
| | | @Override |
| | | public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) { |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | attributes.put(LOGIN_USER_KEY, loginUser); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 握手后 |
| | | * |
| | | * @param request request |
| | | * @param response response |
| | | * @param wsHandler wsHandler |
| | | * @param exception 异常 |
| | | */ |
| | | @Override |
| | | public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.listener; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import org.dromara.common.websocket.holder.WebSocketSessionHolder; |
| | | import org.dromara.common.websocket.utils.WebSocketUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.boot.ApplicationArguments; |
| | | import org.springframework.boot.ApplicationRunner; |
| | | import org.springframework.core.Ordered; |
| | | |
| | | /** |
| | | * WebSocket 主题订阅监听器 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @Slf4j |
| | | public class WebSocketTopicListener implements ApplicationRunner, Ordered { |
| | | |
| | | @Override |
| | | public void run(ApplicationArguments args) throws Exception { |
| | | WebSocketUtils.subscribeMessage((message) -> { |
| | | log.info("WebSocket主题订阅收到消息session keys={} message={}", message.getSessionKeys(), message.getMessage()); |
| | | // 如果key不为空就按照key发消息 如果为空就群发 |
| | | if (CollUtil.isNotEmpty(message.getSessionKeys())) { |
| | | message.getSessionKeys().forEach(key -> { |
| | | if (WebSocketSessionHolder.existSession(key)) { |
| | | WebSocketUtils.sendMessage(key, message.getMessage()); |
| | | } |
| | | }); |
| | | } else { |
| | | WebSocketSessionHolder.getSessionsAll().forEach(key -> { |
| | | WebSocketUtils.sendMessage(key, message.getMessage()); |
| | | }); |
| | | } |
| | | }); |
| | | log.info("初始化WebSocket主题订阅监听器成功"); |
| | | } |
| | | |
| | | @Override |
| | | public int getOrder() { |
| | | return -1; |
| | | } |
| | | } |
New file |
| | |
| | | package org.dromara.common.websocket.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.websocket.dto.WebSocketMessageDto; |
| | | import org.dromara.common.websocket.holder.WebSocketSessionHolder; |
| | | import org.springframework.web.socket.PongMessage; |
| | | import org.springframework.web.socket.TextMessage; |
| | | import org.springframework.web.socket.WebSocketMessage; |
| | | import org.springframework.web.socket.WebSocketSession; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.function.Consumer; |
| | | |
| | | import static org.dromara.common.websocket.constant.WebSocketConstants.WEB_SOCKET_TOPIC; |
| | | |
| | | /** |
| | | * 工具类 |
| | | * |
| | | * @author zendwang |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class WebSocketUtils { |
| | | |
| | | /** |
| | | * 发送消息 |
| | | * |
| | | * @param sessionKey session主键 一般为用户id |
| | | * @param message 消息文本 |
| | | */ |
| | | public static void sendMessage(Long sessionKey, String message) { |
| | | WebSocketSession session = WebSocketSessionHolder.getSessions(sessionKey); |
| | | sendMessage(session, message); |
| | | } |
| | | |
| | | /** |
| | | * 订阅消息 |
| | | * |
| | | * @param consumer 自定义处理 |
| | | */ |
| | | public static void subscribeMessage(Consumer<WebSocketMessageDto> consumer) { |
| | | RedisUtils.subscribe(WEB_SOCKET_TOPIC, WebSocketMessageDto.class, consumer); |
| | | } |
| | | |
| | | /** |
| | | * 发布订阅的消息 |
| | | * |
| | | * @param webSocketMessage 消息对象 |
| | | */ |
| | | public static void publishMessage(WebSocketMessageDto webSocketMessage) { |
| | | List<Long> unsentSessionKeys = new ArrayList<>(); |
| | | // 当前服务内session,直接发送消息 |
| | | for (Long sessionKey : webSocketMessage.getSessionKeys()) { |
| | | if (WebSocketSessionHolder.existSession(sessionKey)) { |
| | | WebSocketUtils.sendMessage(sessionKey, webSocketMessage.getMessage()); |
| | | continue; |
| | | } |
| | | unsentSessionKeys.add(sessionKey); |
| | | } |
| | | // 不在当前服务内session,发布订阅消息 |
| | | if (CollUtil.isNotEmpty(unsentSessionKeys)) { |
| | | WebSocketMessageDto broadcastMessage = new WebSocketMessageDto(); |
| | | broadcastMessage.setMessage(webSocketMessage.getMessage()); |
| | | broadcastMessage.setSessionKeys(unsentSessionKeys); |
| | | RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> { |
| | | log.info("WebSocket发送主题订阅消息topic:{} session keys:{} message:{}", |
| | | WEB_SOCKET_TOPIC, unsentSessionKeys, webSocketMessage.getMessage()); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发布订阅的消息(群发) |
| | | * |
| | | * @param message 消息内容 |
| | | */ |
| | | public static void publishAll(String message) { |
| | | WebSocketMessageDto broadcastMessage = new WebSocketMessageDto(); |
| | | broadcastMessage.setMessage(message); |
| | | RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> { |
| | | log.info("WebSocket发送主题订阅消息topic:{} message:{}", WEB_SOCKET_TOPIC, message); |
| | | }); |
| | | } |
| | | |
| | | public static void sendPongMessage(WebSocketSession session) { |
| | | sendMessage(session, new PongMessage()); |
| | | } |
| | | |
| | | public static void sendMessage(WebSocketSession session, String message) { |
| | | sendMessage(session, new TextMessage(message)); |
| | | } |
| | | |
| | | private static void sendMessage(WebSocketSession session, WebSocketMessage<?> message) { |
| | | if (session == null || !session.isOpen()) { |
| | | log.warn("[send] session会话已经关闭"); |
| | | } else { |
| | | try { |
| | | session.sendMessage(message); |
| | | } catch (IOException e) { |
| | | log.error("[send] session({}) 发送消息({}) 异常", session, message, e); |
| | | } |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | org.dromara.common.websocket.config.WebSocketConfig |