01注解名称:CheckUnSysAdmin
package com.sf.XWFS.aop;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CheckUnSysAdmin {
}
02注解实现类:CommonAspect
package com.sf.XWFS.aop;
import com.sf.XWFS.constant.RoleConst;
import com.sf.XWFS.service.OrderService;
import com.sf.erui.common.response.ResponseHelper;
import com.sf.erui.context.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Aspect
@Component
@Slf4j
public class CommonAspect {
@Resource
private OrderService orderService;
@Pointcut("execution(* com.sf.XWFS..*(..)) && @annotation(com.sf.XWFS.aop.CheckUnSysAdmin)")
public void checkUnSysAdmin() {
}
@Around("checkUnSysAdmin()")
public Object checkUnSysAdmin(ProceedingJoinPoint pjp) throws Throwable {
try {
String currentUserName = UserContext.getCurrentUserName();
String roleId = orderService.getRoleId(currentUserName);
if (!RoleConst.SYSADMIN_ROLE_ID.equals(roleId)) {
return ResponseHelper.buildFail("权限不足,请联系管理员");
}
} catch (Throwable throwable) {
throw new SecurityException("用户无权访问!", throwable);
}
return pjp.proceed();
}
}
03角色常量类
package com.sf.XWFS.constant;
public class RoleConst {
public static final String SYSADMIN_ROLE_ID = "102";
public static final String LRADMIN_ROLE_ID = "103";
public static final String RQADMIN_ROLE_ID = "104";
public static final String PT_ROLE_ID = "105";
public static final String PTGLY_ROLE_ID = "106";
}
04Controller层方法上引入
@CheckUnSysAdmin
@ApiOperation(value = "通过主键查询一条数据(订单脱敏)", notes = "查-单查")
@RequestMapping("getOrderById")
@ResponseBody
public Response getOrderById(@RequestBody OrderDO record) {
logger.info("收件人脱敏接口");
String orderid = record.getOrderid();
Assert.notNull(orderid, "请选择一条数据!");
OrderDO orderDO = orderService.getOrderByOrderId(orderid);
return ResponseHelper.buildOk(orderDO);
}