zoukankan      html  css  js  c++  java
  • 加注解时插入权限切面@EnableDataSecurity

    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;
    
    
    /**
     * Annotation which indicates the annotated method must be secured.
     */
    @Target({ ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface EnableDataSecurity {
        
        /**
         * 启用当前机构条件开关 ON/OFF
         * */
        public String enableCurrentOrgCondition() default "ON";
        
        /**
         * 启用机构货主表关联开关 ON/OFF
         * */
        public String enableOrgOwnerRelation() default "ON";
        
        /**
         * 启用域管理员创建货主 所属域开放权限开关 ON/OFF,只针对域管理员
         * */
        public String enableOwnerCreateDomainAuth() default "ON";
    }
    package com.yundaex.common.security.advice;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
    import org.springframework.stereotype.Component;
    
    import com.yundaex.common.security.annotation.EnableDataSecurity;
    import com.yundaex.common.security.context.WMSSecurityContext;
    
    
    @Aspect
    @Component("enableDataSecurityAroundAdvice")
    public class EnableDataSecurityAroundAdvice {
        
        //private static final Logger logger = Logger.getLogger(EnableDataSecurityAroundAdvice.class);
        
        /*public EnableDataSecurityAroundAdvice() {
            logger.debug("EnableDataSecurityAroundAdvice initial success");
        }*/
        
        @Pointcut(value = "@annotation(enableDataSecurity)", argNames = "enableDataSecurity")
        protected void enableDataSecurity(EnableDataSecurity enableDataSecurity) {}
    
        /**
         * indicate the method should be exeucte the data security operation.
         * */
    //    @SuppressWarnings("rawtypes")
        @Before(value = "enableDataSecurity(enableDataSecurity)", argNames="enableDataSecurity")
        public void before(JoinPoint joinPoint, EnableDataSecurity enableDataSecurity) throws Throwable {
            
    //        Class clazz = joinPoint.getTarget().getClass();
            if (MethodInvocationProceedingJoinPoint.class.isAssignableFrom(joinPoint.getClass())) {
                
                MethodInvocationProceedingJoinPoint methodInvocationProceedingJoinPoint = (MethodInvocationProceedingJoinPoint) joinPoint;
                final String methodName = methodInvocationProceedingJoinPoint.getSignature().getName();
                
    //            String methodSignature = clazz.getName() + methodName;
                String enableCurrentOrgCondition = enableDataSecurity.enableCurrentOrgCondition();
                String enableOrgOwnerRelation = enableDataSecurity.enableOrgOwnerRelation();
                String enableOwnerCreateDomainAuth = enableDataSecurity.enableOwnerCreateDomainAuth();
                String methodSignature = methodName.concat("&")
                                            .concat(enableCurrentOrgCondition)
                                            .concat("&").concat(enableOrgOwnerRelation)
                                            .concat("&").concat(enableOwnerCreateDomainAuth);
                //set to thread local to proceeding
                WMSSecurityContext.getDataSecurityMethodSignature().set(methodSignature);
            } else {
                //TODO THROW EXCEPTION TO EXPLAIN "Unsupport in the scenario using annotation 'EnableDataSecurity'"
            }
            
        }
        
        @After(value = "enableDataSecurity(enableDataSecurity)", argNames="enableDataSecurity")
        public void after(JoinPoint joinPoint,EnableDataSecurity enableDataSecurity) throws Throwable {
            WMSSecurityContext.getDataSecurityMethodSignature().remove();
        }
        
    }
    applicationContext.xml
    
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
  • 相关阅读:
    Yslow-23条规则
    ASP.Net MVC多语言
    Java笔记--反射机制
    Java笔记--常用类
    Java笔记--多线程
    Java--IO流
    Java笔记--枚举&注解
    Java笔记--泛型
    Java笔记--异常
    Java笔记--集合
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6520323.html
Copyright © 2011-2022 走看看