【发布时间】:2016-11-17 21:38:34
【问题描述】:
我正在创建一个类来审核对我的 Spring Boot 应用的控制器类的调用:
@Aspect
@Component
public class ServiceAudit { //RequestMappingInterceptor {
@Pointcut("@annotation(requestMapping) && execution(* *(..))")
public void controller(RequestMapping requestMapping) {}
@Before("controller(requestMapping)")
public void advice(JoinPoint thisJoinPoint, RequestMapping requestMapping) {
String url = requestMapping.value()[0];
String httpMethod = requestMapping.method()[0].toString();
...
...
我的一个 Controller 类看起来像这样 - 在类和方法级别带有注释(此时我无法更改):
RestController
@CrossOrigin
@EnableConfigurationProperties
@RequestMapping(value = "/applications")
public class ApplicationController {
...
...
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Application> findById(@PathVariable Integer id) throws Exception {
...
...
}
我可以很好地提取 url、methodtype、参数。但是,我很难做的是拉出 Controller 类的注释('/applications'),这样我就可以为我的审计表构建完整的 URL。
我知道还有其他审计选项(如 Spring Boot Actuator),但出于各种原因,我需要在方面使用这种方法,所以我就卡在这里了。 Spring 的 AnnotationUtils 似乎会有所帮助,但被困在代码上以获取类级别的注释。有人做过吗?
更新: 谢谢。那没有用。你是对的 - 这是一个多余的部分,当我删除“执行”部分时它起作用了,但仍然是同样的问题。本质上,问题是在这种情况下如何获得类级别的注释。因此,对于我的 ApplicationsController 类,我如何检索在类级别定义的“/applications”路径。我认为它在那里,但它嵌套在 requestMapping 对象中如此之深,我无法弄清楚如何提取它。目前,我唯一的解决方案是为我的控制器(ApplicationsController -> '/applications')创建一个 HashMap,然后使用 methodInvocation.targetClass 作为该映射的键。添加控制器时不是很优雅,需要维护。
【问题讨论】:
-
尝试,从切入点移除执行(* *(..))
-
谢谢。那没有用。你是对的 - 这是一个多余的部分,当我删除该部分时它起作用了,但仍然是同样的问题。
标签: spring spring-boot spring-aop