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());
}
}
}
}
}
}