天天看點

用springAOP解決權限問題

1.Service層

public interface PersonService {
	void savePerson();
	void updatePerson();
}
           
public class PersonServiceImpl implements PersonService {

	@PrivilegeInfo(name="savePerson")
	public void savePerson() {
		System.out.println("savePerson");
	}

	@PrivilegeInfo(name="updatePerson")
	public void updatePerson() {
		System.out.println("updatePerson");
	}

}
           

2.寫一個注解PrivilegeInfo 和 注解解析器AnnotationParse

@Target(ElementType.METHOD)//這注解隻作用在方法上面
@Retention(RetentionPolicy.RUNTIME)//作用的範圍
public @interface PrivilegeInfo {
	String name() default "";//定義一個屬性,它的預設值時空字元串
}
           
/**
 * 	這是一個注解解析器
 *
 */
public class AnnotationParse {
	
	/**
	 *	在這裡傳入一個目标類,和一個方法的名字,就傳回一個 PrivilegeInfo 中name屬性的值
	 */
	public static String parse(Class targetClass,String methodName) throws SecurityException, NoSuchMethodException{
		String methodAccess = "";
		Method method = targetClass.getMethod(methodName);
		//判斷method上面有沒有PrivilegeInfo注解
		if(method.isAnnotationPresent(PrivilegeInfo.class)){
			//得到PrivilegeInfo注解
			PrivilegeInfo annotation = method.getAnnotation(PrivilegeInfo.class);
			//獲得PrivilegeInfo注解中name屬性的值
			methodAccess = annotation.name();
		}
		return methodAccess;
	}
	
}
           

3.寫一個權限Privilege 隻有name屬性

public class Privilege {
	private String name;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
           

4.寫一個切面PrivilegeAspect,切面裡面用list裝着權限,寫一個環繞通知isAccessMethod

/**
 *	這是一個切面,作用是檢視使用者是否有權限執行目标方法
 */
public class PrivilegeAspect {
	
	List<Privilege> privileges = new ArrayList<Privilege>();

	public List<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(List<Privilege> privileges) {
		this.privileges = privileges;
	}
	
	/**
	 *	這是一個環繞通知
	 */
	public void isAccessMethod(ProceedingJoinPoint joinPoint) throws Throwable{
		//擷取目标類
		Class targetClass = joinPoint.getTarget().getClass();
		//擷取目标方法的名稱
		String methodName = joinPoint.getSignature().getName();
		//擷取目标方法上面PrivilegeInfo注解中name屬性的值
		String privilegeInfoName = AnnotationParse.parse(targetClass, methodName);
		//擷取使用者的權限
		boolean flag = false;
		//判斷該使用者是不是擁有與要通路的目标方法的權限
		for (Privilege privilege : privileges) {
			if(privilegeInfoName.equals(privilege.getName())){
				flag = true;
			}
		}
		
		if(flag){
			joinPoint.proceed();//執行目标方法
		}else{
			System.out.println("對不起,你沒有通路權限");
		}
	}
}
           

5.Spring的配置檔案中配置

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       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  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
           
        <!-- 這是目标類 -->
		<bean id="personService" class="com.mo.service.PersonServiceImpl"></bean>   
		
		<!-- 這是一個切面 -->
		<bean id="privilegeAspect" class="com.mo.privilegeAspect.PrivilegeAspect"></bean>
		
		<aop:config>
		
			<!-- 這是一個切入點 -->
			<aop:pointcut expression="execution(* com.mo.service.PersonServiceImpl.*(..))" 
			id="perform"/>
			
			<!-- 定義切面 -->
			<aop:aspect ref="privilegeAspect">
				<!-- 這是一個環繞通知 -->
				<aop:around method="isAccessMethod" pointcut-ref="perform"/>
			</aop:aspect>
		</aop:config>
</beans> 
           

6.測試單元

@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		PrivilegeAspect privilegeAspect = (PrivilegeAspect)context.getBean("privilegeAspect");
		
		//建立使用者的去權限
		Privilege privilege1 = new Privilege();
		privilege1.setName("savePerson");
		Privilege privilege2 = new Privilege();
		privilege2.setName("updatePerson");
		
		List<Privilege> privileges = privilegeAspect.getPrivileges();
		privileges.add(privilege1);
		privileges.add(privilege2);
		
		PersonService personService = (PersonService)context.getBean("personService");
		personService.savePerson();
		personService.updatePerson();
	}