今天360文8热8凉1汤~ 不好意思走错了。
需求:根据不同的登录设备对xxController层中的方法是否禁用
AOP解释:切面编程
灵魂画图:
代码:
1、建立一个注解SuiteDisable
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface SuiteDisable { }
2、建一个切面类
import com.alphasales.drama.common.enums.ESuiteType; import com.alphasales.drama.common.exception.BizException; import com.alphasales.drama.framework.GlobalContext; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Aspect @Component @Slf4j public class SuiteDisableAspect { @Around("@annotation(SuiteDisable)") public Object around(ProceedingJoinPoint joinPoint, SuiteDisable SuiteDisable) { String device = GlobalContext.LOGIN_USER.get().getDevice(); ListsuiteTypeList = Arrays.stream(ESuiteType.values()).map(ESuiteType::getName).collect(Collectors.toList()); if (suiteTypeList.contains(device)){ throw new BizException("第三方登录不允许操作通讯录"); } try { return joinPoint.proceed(); } catch (Throwable e) { log.error(e.getMessage(), e); return e; } } }
3、 再放到需要的方法上就完成啦
@SuiteDisable @PostMapping("addUser") void addUser(@RequestBody UserDTO userDTO){ userBusiness.addUser(userDTO); }