springboot 切面开发

李志 2020-06-23 PM 1030℃ 0条

pom.xml 添加 AOP 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

入口类添加@EnableAspectJAutoProxy注解开启AOP

编写切面类

@Aspect
@Component
public class AuthAspect {

    @Autowired
    HttpServletRequest request;

    @Autowired
    protected WaterStationService waterStationService;

    @Before("execution(* cn.agoodwater.admin.controller.*.*(..))")
    public void proceed(JoinPoint joinPoint) throws Throwable {
        System.out.println("进入切点");
        String remoteUser = request.getRemoteUser();

        if(StringUtils.isNotBlank(remoteUser) && !"admin".equalsIgnoreCase(remoteUser)){
            WaterStationBO ws = waterStationService.queryByManager(remoteUser);
            Object[] obj = joinPoint.getArgs();
            for (Object obj1 : obj) {
                if(!(obj1 instanceof HttpServletResponse)){
                    Method setWsIdMethod = null;
                    Class<?> aClass = obj1.getClass();
                    try {
                        setWsIdMethod = aClass.getDeclaredMethod("setWsId",Integer.class);
                    } catch (Exception e) {
                        //
                    }
                    if(setWsIdMethod!=null){
                        setWsIdMethod.invoke(obj1,ws.getId());
                    }
                }
            }
        }


    }
}
标签: none

非特殊说明,本博所有文章均为博主原创。

上一篇 今天父亲节
下一篇 windows 命令

评论啦~