【8.1.1】【db】更新数据范围AOP和注解

pull/60/head
fengshuonan 2024-03-01 13:52:56 +08:00
parent 2501b0ac9f
commit b4e494c221
5 changed files with 197 additions and 0 deletions

View File

@ -92,4 +92,9 @@ public interface DbConstants {
*/
String DRUID_WEB_STAT_FILTER_PROFILE_ENABLE = "true";
/**
* AOP
*/
int DATA_SCOPE_AOP_ORDER = 100;
}

View File

@ -32,6 +32,12 @@
<version>${roses.version}</version>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,21 @@
package cn.stylefeng.roses.kernel.db.mp.datascope;
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
/**
*
*
* @author fengshuonan
* @since 2024-03-01 13:39
*/
public interface UserRoleDataScopeApi {
/**
*
*
* @author fengshuonan
* @since 2024-03-01 13:41
*/
DataScopeConfig getUserRoleDataScopeConfig();
}

View File

@ -0,0 +1,50 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* 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.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.db.mp.datascope.annotations;
import java.lang.annotation.*;
/**
*
*
* @author fengshuonan
* @since 2024-03-01 12:39
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DataScope {
/**
* id
*/
String orgIdFieldName() default "";
/**
* id
*/
String userIdFieldName() default "";
}

View File

@ -0,0 +1,115 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* 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.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.db.mp.datascope.aop;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.db.api.constants.DbConstants;
import cn.stylefeng.roses.kernel.db.mp.datascope.UserRoleDataScopeApi;
import cn.stylefeng.roses.kernel.db.mp.datascope.annotations.DataScope;
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
import cn.stylefeng.roses.kernel.db.mp.datascope.holder.DataScopeHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import java.lang.reflect.Method;
/**
* AOP
*
* @author fengshuonan
* @since 2024-03-01 12:47
*/
@Aspect
@Slf4j
public class DataScopeAop implements Ordered {
@Pointcut(value = "@annotation(cn.stylefeng.roses.kernel.db.mp.datascope.annotations.DataScope)")
private void cutService() {
}
@Around("cutService()")
public Object around(ProceedingJoinPoint point) throws Throwable {
// 获取被aop拦截的方法
Signature signature = point.getSignature();
MethodSignature methodSignature = null;
if (!(signature instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
methodSignature = (MethodSignature) signature;
Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
// 获取注解
DataScope datasource = currentMethod.getAnnotation(DataScope.class);
// 获取当前用户拥有的数据范围
UserRoleDataScopeApi userRoleDataScopeApi = SpringUtil.getBean(UserRoleDataScopeApi.class);
DataScopeConfig userRoleDataScopeConfig = userRoleDataScopeApi.getUserRoleDataScopeConfig();
// 如果有单独配置特定的字段,以注解单独配置的字段为主
String userIdFieldName = datasource.userIdFieldName();
if (StrUtil.isNotBlank(userIdFieldName)) {
userRoleDataScopeConfig.setUserIdFieldName(userIdFieldName);
}
String orgIdFieldName = datasource.orgIdFieldName();
if (StrUtil.isNotBlank(orgIdFieldName)) {
userRoleDataScopeConfig.setOrgIdFieldName(orgIdFieldName);
}
try {
// 放入到临时上下文中
DataScopeHolder.set(userRoleDataScopeConfig);
// 执行原有业务逻辑
return point.proceed();
} finally {
// 清空数据范围的上下文环境
DataScopeHolder.remove();
}
}
/**
* aopspring
*
* @author fengshuonan
* @since 2020/10/31 22:55
*/
@Override
public int getOrder() {
return DbConstants.DATA_SCOPE_AOP_ORDER;
}
}