spring(AOP)权限处理

这里写图片描述

思路:
1、写dao层和service层的类和接口
2、自定义的注解@PrivilegeInfo
3、注解解析器:解析目标方法上面的注解的name属性的值
4、写一个权限类Privilege(name)
5、写一个关于权限的判断的切面,在切面中写一个环绕通知
/**
 * dao 接口
 * @author w7
 *
 */
public interface PersonDao {
    public void savePerson();
    public void updatePerson();
}
/**
 * dao 实现类
 * @author w7
 *
 */
public class PersonDaoImpl implements PersonDao{
    public void savePerson() {
        System.out.println("save person");
    }
    public void updatePerson() {
        System.out.println("update person");
    }
}
/*
 *权限注解的定义 
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrivilegeInfo {
    String name() default "";
}
/**
 * 注解的解析
 * @author w7
 *
 */
public class AnnotationParse {
    /**
     * 
     * @param classt 目标类
     * @param methodName  目标的方法名子
     * @return
     * @throws Exception
     */
    public static String parse(Class classt,String methodName) throws Exception{
        //权限的名称
        String privilegeName = "";

        //获取指定的目标方法
        Method method = classt.getMethod(methodName);

        //判断目标方法上是否有PrivilegeInfo
        if(method.isAnnotationPresent(PrivilegeInfo.class)){
            //获取目标方法上面的PrivilegeInfo注解
            PrivilegeInfo privilegeInfo = method.getAnnotation(PrivilegeInfo.class);

            //获取该注解的name属性的值
            privilegeName = privilegeInfo.name();
        }
        return privilegeName;
    }
}
/**
 * service  接口
 * @author w7
 *
 */
public interface PersonService {
    public void savePerson();
    public void updatePerson();
}
/**
 * service 实现类
 * @author w7
 *
 */
public class PersonServiceImpl implements PersonService{
    private PersonDao personDao;
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    /**
     * 设置 权限的注解 标记
     */
    @PrivilegeInfo(name="savePerson")
    public void savePerson() {
        this.personDao.savePerson();
    }   


    @PrivilegeInfo(name="updatePerson")
    public void updatePerson() {
        this.personDao.updatePerson();
    }
}
/**
 * 封装权限的对象
 * @author w7
 *
 */
public class Privilege {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
/**
 * 切面:权限的访问
 * @author w7
 *
 */
public class PrivilegeAspect {
    /**
     * 该用户能够访问到的权限集合
     */
    private List<Privilege> privileges = new ArrayList<Privilege>();

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

    public void setPrivileges(List<Privilege> privileges) {
        this.privileges = privileges;
    }

    /*
     * 环绕通知
     */
    public void controlTargetMethod(ProceedingJoinPoint joinPoint) throws Throwable{
        /**
         * 解析注解:
         * 
         * 1、获取用户能够访问到的权限
         * 2、获取访问目标方法的权限
         * 3、检查用户的权限中是否包含目标方法的权限
         *      如果包含
         *          则访问目标方法
         *      如果不包含
         *          则提示不能访问
         */
        //目标类的class形式
        Class targetClass = joinPoint.getTarget().getClass();
        //目标方法
        String methodName = joinPoint.getSignature().getName();

        //能够访问目标方法的权限
        String privilegeName = AnnotationParse.parse(targetClass, methodName);

        boolean flag = false;
        //检查用户的权限是否包含访问目标方法应该具备的权限
        for(Privilege privilege:this.privileges){
            if(privilege.getName().equals(privilegeName)){
                flag = true;
                break;
            }
        }

        if(flag){//可以访问目标方法
            joinPoint.proceed();//访问目标方法,执行目标方法
        }else{
            System.out.println("权限不足");
        }
    }
}
<bean id="personDao" class="com.itheima09.springaop.xml.privilege.dao.PersonDaoImpl"></bean>
    <bean id="personService" class="com.itheima09.springaop.xml.privilege.service.PersonServiceImpl">
        <property name="personDao">
            <ref bean="personDao"/>
        </property>
    </bean>

    <!-- 切面:权限 -->
    <bean id="privilegeAspect" class="com.itheima09.springaop.xml.privilege.aspect.PrivilegeAspect">
    </bean>

    <aop:config>
        <aop:pointcut 
            expression="execution(* com.itheima09.springaop.xml.privilege.service.*.*(..))" 
            id="perform"/>

        <!--环绕通知 -->
        <aop:aspect ref="privilegeAspect">
            <aop:around method="controlTargetMethod" pointcut-ref="perform"/>
        </aop:aspect>
    </aop:config>
//测试
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //从srping容器中 获取权限切面对象
        PrivilegeAspect privilegeAspect = (PrivilegeAspect)context.getBean("privilegeAspect");
        /**
         * 给用户初始化权限值
         */
        Privilege privilege1 = new Privilege();
        privilege1.setName("asdf");

        Privilege privilege2 = new Privilege();
        privilege2.setName("updatePerson");

        /**
         * 将用户的所有权限添加到权限切面的集合中
         */
        privilegeAspect.getPrivileges().add(privilege1);
        privilegeAspect.getPrivileges().add(privilege2);

        /**
         * 执行目标方法
         */
        PersonService personService = (PersonService)context.getBean("personService");
        personService.savePerson();
    }

//结果:权限不足
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值