微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 spring-boot 过滤器中获取自定义注释

如何解决如何在 spring-boot 过滤器中获取自定义注释

我有一个带有自定义过滤器的 Spring Boot 应用程序:

import javax.servlet.Filter

public class MyFilter implements Filter{

    @Override
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) {
        System.out.println("MyFilter");
    }
}

应用程序中的某些控制器在其方法上具有自定义注释。 所有自定义注解都实现了 myAnnotation 接口。

    @GetMapping("/")
    @MyCustomAnnotation
    @AnotherCustomAnnotation
    public ResponseEntity<String> get(){
        addSomeMetric();
        return new ResponseEntity<>("Hello world",HttpStatus.OK);
    }

如何从 doFilter 代码获取在 URI 端点上定义的所有注释的列表?

解决方法

Spring AOP 可用于根据请求/用户决定是否需要执行控制器方法。

以下代码是实现相同的一种方式。

@Aspect
@Component
public class AnnotationFilterAspect {
    
    @Pointcut("execution(public * package.controller..*(..))")
    public void allControllerMethods() {
        
    }
    
    @Around("allControllerMethods()")
    public Object checkAccess(ProceedingJoinPoint pjp) {
        Object retObject = null;
        Method method = getMethod(pjp);
        for(Annotation anno : method.getAnnotations()){
            System.out.println(anno.annotationType());
            // Annotation details
        }
        
        // Logged in user details
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        //or
        //Get the HttpServletRequest currently bound to the thread.
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        
        // Logic to proceed to the controller method based on the logged in user or request can be done here. 
        
        try {
            retObject = pjp.proceed();
        } catch (Throwable e) {
            // Handle the exception
        }
        
        return retObject;
    }
    
    //Refer : https://stackoverflow.com/q/5714411/4214241
    private Method getMethod(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        if (method.getDeclaringClass().isInterface()) {
            try {
                method= pjp.getTarget().getClass().getDeclaredMethod(pjp.getSignature().getName(),method.getParameterTypes());
            } catch (final SecurityException exception) {
                //...
            } catch (final NoSuchMethodException exception) {
                //...                
            }
        }   
        return method;
    }

}

另请注意,注释 RetentionPolicy 必须是 RUNTIME ,此代码才能工作。

,

我最终使用了 HandlerInterceptor 而不是过滤器。

由于我只针对 Web 请求,因此它似乎是比 AOP 更好、更简单的解决方案。

    @Override
    public boolean preHandle(@NonNull HttpServletRequest request,@NonNull HttpServletResponse response,@NonNull Object handler) {
       MyAnnotation myAnnotation = getMyAnnotation(handler);

    }

    private MyAnnotation getMyAnnotation(Object handler) {
       HandlerMethod handlerMethod = (HandlerMethod) handler;
       Method method = handlerMethod.getMethod();
       return method.getAnnotation(MyAnnotation.class);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。