Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
一、 引言
二、 方案
方案二
方案三
對外權限分配接口
存在的問題
三、 代碼
最初是打算在Spring Aop的前置通知中通過修改目標方法參數來實現,但是通過源碼發現封裝目標方法參數的類是用final修飾的,所以后面換了種思路。
使用Spring的ator自動代理實現,思路是通過條件判斷決定是否要使用自動代理,要使用代理的話,就需要自己實現接口并重寫其中invoke方法。
下面我貼出核心代碼,文章最后會給出整個demo的鏈接
繼承ator類,重寫()方法
public class BeanTypeAutoProxyCreator extends AbstractAutoProxyCreator {
@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class> beanClass,
String beanName, TargetSource customTargetSource) throws BeansException {
return isMatch(beanClass) ? PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS : DO_NOT_PROXY;
}
/**
* 判斷是否是需要被代理的對象
* @param clazz 代理對象的類型
* @return
*/
private boolean isMatch(Class> clazz) {
//有兩個Class類型的類象,一個是調用isAssignableFrom方法的類對象(后稱對象a),
// 以及方法中作為參數的這個類對象(稱之為對象b),這兩個對象如果滿足以下條件則返回true,否則返回false:
//a對象所對應類信息是b對象所對應的類信息的父類或者是父接口,簡單理解即a是b的父類或接口

//a對象所對應類信息與b對象所對應的類信息相同,簡單理解即a和b為同一個類或同一個接口
if (BaseMapper.class.isAssignableFrom(clazz)) {
return true;
}
return false;
}
}
實現接口,重寫invoke()方法
public class MyMethodInterceptor implements MethodInterceptor {
@Autowired
private SysAccess sysAccess;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 權限封裝類
SysAccessCriteria result = null;
int flag = -1;
// 目標方法的參數
Object[] args = invocation.getArguments();
for (int i=0; i
if(flag >= 0 && result != null){
args[flag] = result;
}
// 執行目標方法
Object object = invocation.proceed();
return object;
}
}
配置到Spring配置文件中
myMethodInterceptor
Demo完整代碼(數據庫文件在目錄下):
單純個人設計,應該有很多不足,有不好的地方可以指出,或者有更好的想法可以評論,一起交流。
參考:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。