spring切面应用

Posted wanhua.wu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring切面应用相关的知识,希望对你有一定的参考价值。

一,切面

1,定义注解

package com.onloon.scrm.pc.web.controller.aspect;

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

/**
 * 客户操作权限检查
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckCompanyAcionValidation {

}

2,定义切面

package com.onloon.scrm.pc.web.controller.aspect;

import com.onloon.scrm.common.beans.Result;
import com.onloon.scrm.common.enums.ResultCodeEnum;
import com.onloon.scrm.common.exception.BusinessException;
import com.onloon.scrm.pc.web.facade.api.contacts.company.FollowLogFacade;
import com.onloon.scrm.pc.web.facade.usercenter.UserCenterFacade;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 客户操作权限检查
 */
@Aspect
@Component
public class CheckCompanyActionPremission {


    @Autowired
    protected UserCenterFacade userCenterFacade;

    @Autowired
    private FollowLogFacade followLogFacade;

    private static Logger logger = LoggerFactory.getLogger(CheckCompanyActionPremission.class);

    @Pointcut("@annotation(com.onloon.scrm.pc.web.controller.aspect.CheckCompanyAcionValidation)")
    public void init(){

    }

    @Before("init()")
    public void checkContactsPerson() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String companyId = request.getParameter("companyId");
        String userId = request.getParameter("userId");
        String mainUserId = request.getParameter("mainUserId");

        //权限校验 子账号不能编辑其他子账号的私海客户
        Result right = followLogFacade.checkCompanyRight(companyId, userId, mainUserId);
        if(right.getCode() != ResultCodeEnum.SUCCESS.getCode()){
            throw new BusinessException(ResultCodeEnum.PERMISSION_DENIED, right.getMessage());
        }
    }

}

3,添加注解

/**
     * 客户详情
     * @param companyId
     * @return
     */
    @CheckCompanyAcionValidation
    @RequestMapping(value="/detail/info")
    public Result<CompanyDetailVO> detailInfo(String companyId, String mainUserId, String userId) {
        try {
            if (StringUtils.isBlank(companyId) || mainUserId == null || userId == null) {
                return Result.failure("参数不能为空");
            }
            return Result.success(contactsCompanyFacade.detailInfo(companyId, mainUserId, userId));
        } catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            logger.error("客户详情查询失败 msg={}", e);
            return Result.failure("客户详情查询失败!");
        }
    }

 

以上是关于spring切面应用的主要内容,如果未能解决你的问题,请参考以下文章

在Spring中使用AOP切面编程

如何理解spring中的切面和过滤

spring切面应用

Spring 切面可以应用五种类型的通知?

Spring框架——AOP(面向切面编程)详解

Spring系列Spring AOP面向切面编程