spring切面應用

一,切面java

1,定義註解web

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,定義切面spring

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,添加註解api

/**
     * 客戶詳情
     * @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("客戶詳情查詢失敗!");
        }
    }
相關文章
相關標籤/搜索