build: hikari config

This commit is contained in:
CaptainB 2023-04-17 20:03:44 +08:00
parent fd61438d82
commit 7b83153b40
3 changed files with 115 additions and 2 deletions

View File

@ -1,21 +1,45 @@
package io.metersphere.config; package io.metersphere.config;
import com.fit2cloud.quartz.anno.QuartzDataSource;
import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import io.metersphere.sdk.interceptor.MybatisInterceptor; import io.metersphere.sdk.interceptor.MybatisInterceptor;
import io.metersphere.sdk.interceptor.UserDesensitizationInterceptor;
import io.metersphere.sdk.util.CompressUtils; import io.metersphere.sdk.util.CompressUtils;
import io.metersphere.sdk.util.MybatisInterceptorConfig; import io.metersphere.sdk.util.MybatisInterceptorConfig;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
@Configuration @Configuration
@MapperScan(basePackages = {"io.metersphere.*.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory") @MapperScan(basePackages = {"io.metersphere.*.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory")
@EnableTransactionManagement @EnableTransactionManagement
public class MybatisConfig { public class MybatisConfig {
@Bean
@ConditionalOnMissingBean
public PageInterceptor pageInterceptor() {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("pageSizeZero", "true");
pageInterceptor.setProperties(properties);
return pageInterceptor;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public MybatisInterceptor dbInterceptor() { public MybatisInterceptor dbInterceptor() {
@ -30,4 +54,34 @@ public class MybatisConfig {
return interceptor; return interceptor;
} }
@Bean
public UserDesensitizationInterceptor userDesensitizationInterceptor() {
return new UserDesensitizationInterceptor();
}
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties properties) {
return DataSourceBuilder.create(properties.getClassLoader()).type(HikariDataSource.class)
.driverClassName(properties.determineDriverClassName())
.url(properties.determineUrl())
.username(properties.determineUsername())
.password(properties.determinePassword())
.build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.quartz.hikari")
@QuartzDataSource
public DataSource quartzDataSource(DataSourceProperties properties) {
return DataSourceBuilder.create(properties.getClassLoader()).type(HikariDataSource.class)
.driverClassName(properties.determineDriverClassName())
.url(properties.determineUrl())
.username(properties.determineUsername())
.password(properties.determinePassword())
.build();
}
} }

View File

@ -0,0 +1,59 @@
package io.metersphere.sdk.interceptor;
import io.metersphere.domain.User;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 用户 password 字段脱敏
*/
@Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
public class UserDesensitizationInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object returnValue = invocation.proceed();
Object result = returnValue;
if (returnValue instanceof ArrayList<?>) {
List<Object> list = new ArrayList<>();
boolean isDecrypted = false;
for (Object val : (ArrayList<?>) returnValue) {
if (val instanceof User) {
isDecrypted = true;
((User) val).setPassword(null);
list.add(val);
}
}
if (isDecrypted) {
result = list;
}
} else {
if (result instanceof User) {
((User) result).setPassword(null);
}
}
return result;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}

View File

@ -20,7 +20,7 @@
<properties> <properties>
<revision>3.x</revision> <revision>3.x</revision>
<java.version>17</java.version> <java.version>17</java.version>
<dubbo.version>2.7.18</dubbo.version> <dubbo.version>2.7.22</dubbo.version>
<platform-plugin-sdk.version>1.5.0</platform-plugin-sdk.version> <platform-plugin-sdk.version>1.5.0</platform-plugin-sdk.version>
<shiro.version>1.11.0</shiro.version> <shiro.version>1.11.0</shiro.version>
<java-websocket.version>1.5.3</java-websocket.version> <java-websocket.version>1.5.3</java-websocket.version>
@ -30,7 +30,7 @@
<pagehelper.version>5.3.2</pagehelper.version> <pagehelper.version>5.3.2</pagehelper.version>
<metersphere-jmeter-functions.version>1.5</metersphere-jmeter-functions.version> <metersphere-jmeter-functions.version>1.5</metersphere-jmeter-functions.version>
<quartz-starter.version>1.0.7</quartz-starter.version> <quartz-starter.version>1.0.7</quartz-starter.version>
<redisson-starter.version>3.20.0</redisson-starter.version> <redisson-starter.version>3.20.1</redisson-starter.version>
<guice.version>5.1.0</guice.version> <guice.version>5.1.0</guice.version>
<mybatis-starter.version>3.0.1</mybatis-starter.version> <mybatis-starter.version>3.0.1</mybatis-starter.version>
<reflections.version>0.10.2</reflections.version> <reflections.version>0.10.2</reflections.version>