1. 程式人生 > >Spring AOP自定義註解實現許可權控制

Spring AOP自定義註解實現許可權控制

1.建立註解類,controller控制層使用該註解進行許可權控制使用



import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定義註解類實現aop 許可權控制
 * @author sanch
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface PrivilegeAnn {
	
	/**許可權名稱 **/
	String value() default ""; 
}

2.建立註解解析類 , 用於返回註解value值在aop切面註解中環繞/前置攔截器中進行許可權name判斷使用



import java.lang.reflect.Method;

/**
 * 註解解析器
 * @author sanch
 *
 */
public class AnnotationParse {
	
	/**
	 * 解析註解
	 * @param targetClassName
	 * @param methodName
	 * @return 許可權註解上的value值
	 * @throws Exception
	 */
	public static String parse(Class targetClassName,String methodName) throws Exception{
		Method method = targetClassName.getMethod(methodName); //獲得目標方法
		String methodAccess = "";
		//判斷目標方法上面是否存在@PrivilegeAnn註解
		if(method.isAnnotationPresent(PrivilegeAnn.class)){
			PrivilegeAnn annotation = method.getAnnotation(PrivilegeAnn.class);
			methodAccess = annotation.value(); //得到註解上的value值
		}
		return methodAccess;
	}
}

3.spring-mvc.xml 配置檔案

<!-- 自動掃描包路徑下檔案註解 -->
    <context:component-scan base-package="top.lolcl"></context:component-scan>
<!-- 啟用自動代理功能  cglib代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

4.建立Controller 層呼叫自定義註解 實現aop 切面攔截xi

@PrivilegeAnn("user")
	@RequestMapping("/index")
	public String index(Model model) throws Exception{
		List<Slideshow> slideshows = slideshowmapper.selectAll();
		model.addAttribute("model", slideshows);
		model.addAttribute("loadpath","echarts.jsp");
		return "/back/index.jsp";
	}

5.建立interator 類,實現aop 攔截



import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 
 * 請求攔截器 切面程式設計
 * @author sanch
 *
 */
@Aspect // 表示該類是一個通知類
@Component //spring註解方式bean注入 交給spring管理
public class RequestInterceptor {
	private  final static Logger logger = Logger.getLogger(RequestInterceptor.class);
	
	//定義一個空方法 借用其註解抽取切點表示式 
	@Pointcut("execution (* top.lolcl.control.*.*(..)) && @annotation(top.lolcl.annotation.PrivilegeAnn) ")
	public void point() {} 
	
	@Before("point()")
	public void permissCommon(JoinPoint joinPoint) throws Exception{
		System.out.println("進入aop-----------------------");
	}
	
	@Before("")
	public void before(JoinPoint joinPoint){}
	@Around("")
	public void around(ProceedingJoinPoint pjp){}
	@After("")
	public void after(JoinPoint joinPoint){}
	@AfterReturning("")
	public void afterRunning(JoinPoint joinPoint){}
	@AfterThrowing("")
    public void afterThrowing(JoinPoint joinPoint) {}
	
	/** * 在切面中獲取http請求 * @return */ 
    private HttpServletRequest getRequest() { 
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); 
    }

}