概述
1,实现”数据权限过滤注解“:
如下:
2,需要在下图实现代码”使其可以项目启动可以自动装配“:
如下:
3,最重要:实现数据过滤处理
代码如下:
package com.bd.common.datascope.aspect;
import com.bd.common.core.utils.StringUtils;
import com.bd.common.core.web.domain.BaseEntity;
import com.bd.common.datascope.annotation.WmsDataScope;
import com.bd.common.security.utils.SecurityUtils;
import com.bd.system.api.domain.SysUser;
import com.bd.system.api.model.LoginUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 数据过滤处理
*
*/
@Aspect
@Component
public class WmsDataScopeAspect
{
/**
* 数据权限过滤关键字
*/
public static final String DATA_SCOPE = "dataScope";
@Before("@annotation(controllerWmsDataScope)")
public void doBefore(JoinPoint point, WmsDataScope controllerWmsDataScope) throws Throwable
{
clearDataScope(point);
handleDataScope(point, controllerWmsDataScope);
}
protected void handleDataScope(final JoinPoint joinPoint, WmsDataScope controllerWmsDataScope)
{
// 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNotNull(loginUser))
{
SysUser currentUser = loginUser.getSysUser();
//如果是超级管理员,则不过滤
if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()){
dataScopeFilter(joinPoint, currentUser, controllerWmsDataScope.wmsDepotAlias());
}
}
}
/**
* 数据范围过滤
*
* @param joinPoint 切点
* @param user 用户
*/
public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String wmsDepotAlias)
{
StringBuilder sqlString = new StringBuilder();
sqlString.append(StringUtils.format(
" JOIN (这里是需要拼接的sql) SUD ON {}=SUD.(需要join的字段) ", user.getSitecode(),
wmsDepotAlias));
if (StringUtils.isNotBlank(sqlString.toString()))
{
Object params = joinPoint.getArgs()[0];
if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
{
BaseEntity baseEntity = (BaseEntity) params;
baseEntity.getParams().put(DATA_SCOPE, sqlString);
}
}
}
/**
* 拼接权限sql前先清空params.dataScope参数防止注入
*/
private void clearDataScope(final JoinPoint joinPoint)
{
Object params = joinPoint.getArgs()[0];
if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
{
BaseEntity baseEntity = (BaseEntity) params;
baseEntity.getParams().put(DATA_SCOPE, "");
}
}
}
4,用法:
4.1需要在impl中添加注解
4.2,在sql中添加:
${params.dataScope}
如下:
需要注意的是 上图的表和你所写的sql有一个字段可以join起来!!
然后,它就会在每一行需要隔离的sql自动拼接上你所写的sql!
end!
最后
以上就是快乐砖头为你收集整理的java+SpringSecurity+用AOP使用注解实现统一数据隔离的全部内容,希望文章能够帮你解决java+SpringSecurity+用AOP使用注解实现统一数据隔离所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复