sping AOP 权限管理方案浅析

此权限系统把待访问的业务方法作为要访问的资源,通过spring aop 对接口进行拦截,然后经过自己代码的管理,可以实现细粒度的权限管理。细化到一个方法上。
方法拦截部分代码:
package test;

import java.security.Permission;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class PermissionCheckAroundAdvice implements MethodInterceptor {

SecurityManager securityMgr= new SecurityManager();


public SecurityManager getSecurityMgr() {
return securityMgr;
}
public void setSecurityMgr(SecurityManager securityMgr) {
this.securityMgr = securityMgr;
}


public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("(被调用方法接口类名: "
+ invocation.getMethod().getDeclaringClass().getName() + ")");
System.out.println("(被调用方法名:" + invocation.getMethod().getName()+ ")");
String methodName = invocation.getMethod().getDeclaringClass().getName()
+ "." + invocation.getMethod().getName();
System.out.println("(被调用方法全名:" + methodName + ")");
System.out.println("有否权限:"+securityMgr.checkPermission(methodName));
if(securityMgr.checkPermission(methodName))
return invocation.proceed();
System.out.println("Goodbye! NO Permission!(by " + this.getClass().getName() + ")");
return "--";
}

}

他会拦截业务层调用的每一个方法。试想既然我知道了 你调用的哪个方法而且方法是我自己写得 方法是做什么的我当然知道。接下来是不是就可以对权限细化做具体实现了。
SecurityManager:
package test;

public class SecurityManager {
User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public boolean checkPermission(String privilege){
return checkPermission(user,privilege);
}
public boolean checkPermission(User user, String privilege){
return user.isPermission(privilege);
}
}

User:
package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class User {

List privilages=new ArrayList();
String name;
public User(){

}
public List getPrivilages() {
return privilages;
}
public void setPrivilages(List privilages) {
this.privilages = privilages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public boolean isPermission(String pri){
Iterator it=privilages.iterator();
System.out.println("privilages : "+privilages.size());
String p="";
boolean pass=false;
while(it.hasNext()){
p=(String) it.next();
System.out.println(p);
if(p.equals(pri)){
pass=true;
break;
}
}
return pass;
}

}

Service:
package test;

public interface Service {

public String getBeanInfo();
}

ServiceBean:
package test;

public class ServiceBean implements Service{

ResourceBean bean;

public void setBean(ResourceBean bean) {
this.bean = bean;
}

public String getBeanInfo() {
// TODO Auto-generated method stub
String result="";
result+=bean.getMethod1();
result+=bean.getMethod2();
result+=bean.getMethod3();

return result;
}

}

ResourceBean:
package test;

public interface ResourceBean extends Resource{

public void theMethod();
public String getMethod1();
public String getMethod2();
public String getMethod3();
}

ResourceBeanImpl:
package test;

public class ResourceBeanImpl implements ResourceBean {

public void theMethod(){
System.out.println(this.getClass().getName()
+"."+new Exception().getStackTrace()[0].getMethodName()
+"()"
+" say Hello!");
}
public String getMethod1(){
return "zhangsan";
}
public String getMethod2(){
return "lisi";

}
public String getMethod3(){
return "wangwu";
}
public void afterPropertiesSet(){
System.out.println("事件监听:类 ResourceBeanImpl属性设置完毕");
}
}

Resource:
package test;

public interface Resource {

}

测试类SpringAopTest:
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopTest {

public static void main(String[] args) {
ApplicationContext ctx
=new ClassPathXmlApplicationContext("applicationContext.xml");
String name="";
Service sb= (Service) ctx.getBean("service");
name=sb.getBeanInfo();

System.out.println("test result::"+name);
}
}

下面是配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>
test.ResourceBean
</value>
</property>
<property name="target">
<ref local="beanTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>permissionAroundAdvisor</value>
</list>
</property>
</bean>
<bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>test.Service</value>
</property>
<property name="target">
<ref local="serviceBean"/>
</property>
<property name="interceptorNames">
<list>
<value>permissionAroundAdvisor</value>
</list>
</property>
</bean>

<bean id="beanTarget" class="test.ResourceBeanImpl"/>
<bean id="user" class="test.User">
<property name="name">
<value>tester</value>
</property>
<property name="privilages">
<list>
<value>test.ResourceBean.getMethod3</value>
<value>test.ResourceBean.getMethod1</value>
<value>test.ResourceBean.getMethod2</value>
<value>test.Service.getBeanInfo</value>
</list>
</property>
</bean>
<bean id="securityMgr" class="test.SecurityManager">
<property name="user">
<ref local="user"/>
</property>
</bean>
<bean id="serviceBean" class="test.ServiceBean">
<property name="bean">
<ref local="bean"/>
</property>
</bean>
<bean id="permissionAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" >
<ref local="thePermissionAroundAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>
<bean id="thePermissionAroundAdvice" class="test.PermissionCheckAroundAdvice">
<property name="securityMgr">
<ref local="securityMgr"/>
</property>
</bean>
</beans>

处理权限问题一般需要资源库(就是具有权限可以访问什么资源),权限(分出一个个权限让他们对应资源库中的资源,设定具有这个权限可以对什么资源进行访问),角色(拥有什么权限可以有多个),用户(具有什么角色)。
权限细粒度的划分可以通过对权限和角色等进行设计实现对应现实生活中的权限实现。这里对其的德代码实现使用spring aop 。
上面已经可以得到你访问的时候调用的各个业务方法。配置文件中定义有user节点中有privilages属性这里面可以定义用户对应角色下的权限的所有资源。我们可以拦截到他是否越权。然后可以对其越权行为进行屏蔽。
这里只是一个简单的原型。希望可以给大家带来帮助
下面是代码包
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值