Introducing new features. add module pig-common-mybatis

This commit is contained in:
冷冷 2020-03-14 21:44:36 +08:00
parent 4ed31d354e
commit b8c76e9003
20 changed files with 56 additions and 285 deletions

View File

@ -5,7 +5,7 @@
<meta charset="UTF-8"/>
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
<title>PigX第三方授权</title>
<title>Pig 第三方授权</title>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="/css/signin.css"/>
</head>

View File

@ -8,7 +8,7 @@
<meta name="description" content="">
<meta name="author" content="">
<title>PigX微服务统一认证</title>
<title>Pig 微服务统一认证</title>
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/signin.css" rel="stylesheet">

View File

@ -49,21 +49,16 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--多数据源-->
<!--数据源-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-datasource</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-mybatis</artifactId>
<version>2.7.0</version>
</dependency>
<!--common-->
<dependency>

View File

@ -21,7 +21,6 @@ package com.pig4cloud.pig.codegen;
import com.pig4cloud.pig.common.datasource.annotation.EnableDynamicDataSource;
import com.pig4cloud.pig.common.security.annotation.EnablePigFeignClients;
import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer;
import com.pig4cloud.pigx.common.swagger.annotation.EnablePigSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
@ -31,13 +30,10 @@ import org.springframework.cloud.client.SpringCloudApplication;
* 代码生成模块
*/
@EnableDynamicDataSource
@EnablePigSwagger2
@EnablePigFeignClients
@SpringCloudApplication
@EnablePigResourceServer
public class PigCodeGenApplication {
public static void main(String[] args) {
SpringApplication.run(PigCodeGenApplication.class, args);
}

View File

@ -83,7 +83,6 @@
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.core.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,49 +0,0 @@
/*
*
* * Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
* * <p>
* * Licensed under the GNU Lesser General Public License 3.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* * <p>
* * https://www.gnu.org/licenses/lgpl.html
* * <p>
* * 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.pig4cloud.pig.common.core.mybatis;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.HashMap;
import java.util.List;
/**
* @author lengleng
* @date 2019/2/1
* 数据权限查询参数
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class DataScope extends HashMap {
/**
* 限制范围的字段名称
*/
private String scopeName = "deptId";
/**
* 具体的数据范围
*/
private List<Integer> deptIds;
/**
* 是否只查询本部门
*/
private Boolean isOnly = false;
}

View File

@ -1,127 +0,0 @@
/*
*
* * Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
* * <p>
* * Licensed under the GNU Lesser General Public License 3.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* * <p>
* * https://www.gnu.org/licenses/lgpl.html
* * <p>
* * 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.pig4cloud.pig.common.core.mybatis;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
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.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author lengleng
* @date 2019/2/1
* <p>
* mybatis 数据权限拦截器
*/
@Slf4j
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor {
@Override
@SneakyThrows
public Object intercept(Invocation invocation) {
StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
this.sqlParser(metaObject);
// 先判断是不是SELECT操作
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
return invocation.proceed();
}
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
String originalSql = boundSql.getSql();
Object parameterObject = boundSql.getParameterObject();
//查找参数中包含DataScope类型的参数
DataScope dataScope = findDataScopeObject(parameterObject);
if (dataScope == null) {
return invocation.proceed();
} else {
String scopeName = dataScope.getScopeName();
List<Integer> deptIds = dataScope.getDeptIds();
if (StrUtil.isNotBlank(scopeName) && CollectionUtil.isNotEmpty(deptIds)) {
String join = CollectionUtil.join(deptIds, ",");
originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
metaObject.setValue("delegate.boundSql.sql", originalSql);
}
return invocation.proceed();
}
}
/**
* 生成拦截对象的代理
*
* @param target 目标对象
* @return 代理对象
*/
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
/**
* mybatis配置的属性
*
* @param properties mybatis配置的属性
*/
@Override
public void setProperties(Properties properties) {
}
/**
* 查找参数是否包括DataScope对象
*
* @param parameterObj 参数列表
* @return DataScope
*/
private DataScope findDataScopeObject(Object parameterObj) {
if (parameterObj instanceof DataScope) {
return (DataScope) parameterObj;
} else if (parameterObj instanceof Map) {
for (Object val : ((Map<?, ?>) parameterObj).values()) {
if (val instanceof DataScope) {
return (DataScope) val;
}
}
}
return null;
}
}

View File

@ -37,8 +37,8 @@ import org.springframework.context.annotation.Configuration;
* <p>
* 动态数据源切换配置
*/
@Configuration
@AllArgsConstructor
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceProperties.class)
public class DynamicDataSourceAutoConfiguration {

View File

@ -63,6 +63,7 @@ public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvide
String password = rs.getString(DataSourceConstants.DS_USER_PWD);
String url = rs.getString(DataSourceConstants.DS_JDBC_URL);
DataSourceProperty property = new DataSourceProperty();
property.setDriverClassName(DataSourceConstants.DS_DRIVER);
property.setUsername(username);
property.setPassword(stringEncryptor.decrypt(password));
property.setUrl(url);

View File

@ -33,9 +33,9 @@ import org.springframework.scheduling.annotation.EnableAsync;
* 日志自动配置
*/
@EnableAsync
@Configuration
@AllArgsConstructor
@ConditionalOnWebApplication
@Configuration(proxyBeanMethods = false)
public class LogAutoConfiguration {
private final RemoteLogService remoteLogService;

View File

@ -16,19 +16,20 @@
*
*/
package com.pig4cloud.pig.admin.config;
package com.pig4cloud.pig.common.mybatis;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.pig4cloud.pig.common.core.mybatis.DataScopeInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author lengleng
* @date 2019/2/1
* @date 2020-03-14
* <p>
* mybatis plus 统一配置
*/
@Configuration
public class MybatisPlusConfiguration {
@Configuration(proxyBeanMethods = false)
public class MybatisAutoConfiguration {
/**
* 分页插件
*
@ -39,14 +40,4 @@ public class MybatisPlusConfiguration {
return new PaginationInterceptor();
}
/**
* 数据权限插件
*
* @return DataScopeInterceptor
*/
@Bean
public DataScopeInterceptor dataScopeInterceptor() {
return new DataScopeInterceptor();
}
}

View File

@ -15,14 +15,14 @@
* * limitations under the License.
*
*/
package com.pig4cloud.pigx.common.swagger.config;
package com.pig4cloud.pig.common.swagger;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import com.pig4cloud.pig.common.swagger.config.SwaggerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
@ -43,9 +43,9 @@ import java.util.List;
* @author lengleng
* swagger配置
*/
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerAutoConfiguration {
@ -55,11 +55,6 @@ public class SwaggerAutoConfiguration {
private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error","/actuator/**");
private static final String BASE_PATH = "/**";
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
@Bean
public Docket api(SwaggerProperties swaggerProperties) {
@ -85,8 +80,8 @@ public class SwaggerAutoConfiguration {
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))
.build()
.securitySchemes(Collections.singletonList(securitySchema()))
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext(swaggerProperties)))
.pathMapping("/");
}
@ -95,10 +90,10 @@ public class SwaggerAutoConfiguration {
*
* @return
*/
private SecurityContext securityContext() {
private SecurityContext securityContext(SwaggerProperties swaggerProperties) {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex()))
.securityReferences(defaultAuth(swaggerProperties))
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build();
}
@ -107,23 +102,23 @@ public class SwaggerAutoConfiguration {
*
* @return
*/
private List<SecurityReference> defaultAuth() {
private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
return Collections.singletonList(SecurityReference.builder()
.reference(swaggerProperties().getAuthorization().getName())
.reference(swaggerProperties.getAuthorization().getName())
.scopes(authorizationScopeList.toArray(authorizationScopes))
.build());
}
private OAuth securitySchema() {
private OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
ArrayList<GrantType> grantTypes = new ArrayList<>();
swaggerProperties().getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes);
swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
}
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {

View File

@ -15,7 +15,7 @@
* * limitations under the License.
*
*/
package com.pig4cloud.pigx.common.swagger.config;
package com.pig4cloud.pig.common.swagger.config;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -1,37 +0,0 @@
/*
*
* * Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
* * <p>
* * Licensed under the GNU Lesser General Public License 3.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* * <p>
* * https://www.gnu.org/licenses/lgpl.html
* * <p>
* * 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.pig4cloud.pigx.common.swagger.annotation;
import com.pig4cloud.pigx.common.swagger.config.SwaggerAutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author lengleng
* @date 2018/7/21
* 开启pig swagger
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({SwaggerAutoConfiguration.class})
public @interface EnablePigSwagger2 {
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pig.common.swagger.SwaggerAutoConfiguration

View File

@ -35,6 +35,7 @@
<module>pig-common-core</module>
<module>pig-common-datasource</module>
<module>pig-common-log</module>
<module>pig-common-mybatis</module>
<module>pig-common-security</module>
<module>pig-common-swagger</module>
</modules>

View File

@ -6,9 +6,9 @@ ENV TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir -p /pigx-upms
RUN mkdir -p /pig-upms
WORKDIR /pigx-upms
WORKDIR /pig-upms
EXPOSE 4000

View File

@ -56,6 +56,12 @@
<artifactId>pig-common-swagger</artifactId>
<version>2.7.0</version>
</dependency>
<!--mybatis 模块-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-mybatis</artifactId>
<version>2.7.0</version>
</dependency>
<!--注册中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
@ -66,17 +72,6 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -21,7 +21,6 @@ package com.pig4cloud.pig.admin;
import com.pig4cloud.pig.common.security.annotation.EnablePigFeignClients;
import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer;
import com.pig4cloud.pigx.common.swagger.annotation.EnablePigSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
@ -30,7 +29,6 @@ import org.springframework.cloud.client.SpringCloudApplication;
* @date 2018年06月21日
* 用户统一管理系统
*/
@EnablePigSwagger2
@EnablePigResourceServer
@EnablePigFeignClients
@SpringCloudApplication

13
pom.xml
View File

@ -49,7 +49,7 @@
<velocity.version>1.7</velocity.version>
<jasypt.version>2.1.0</jasypt.version>
<swagger.fox.version>2.9.2</swagger.fox.version>
<swagger.core.version>1.5.22</swagger.core.version>
<swagger.core.version>1.5.24</swagger.core.version>
<security.oauth.version>2.3.6.RELEASE</security.oauth.version>
<docker.plugin.version>0.32.0</docker.plugin.version>
<docker.host>http://172.17.0.111:2375</docker.host>
@ -141,6 +141,17 @@
<artifactId>spring-security-oauth2</artifactId>
<version>${security.oauth.version}</version>
</dependency>
<!--swagger 最新依赖内置版本-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<!--web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>