Java 接口上定义的 javax.ws.rs.Path 注释上的 AOP 切入点

问题描述

我正在尝试在我的项目中进行的所有 JAX-RS 调用上应用 Before Aspect。我将捕获所有的休息服务。当我在包级别应用切入点时,它工作正常,但想法是从安全角度捕获项目中引用的所有@Path。

切入点的 AOP 传递

@Before("execution(* com.dnegi.service..(..))")

我已经设置了 Spring MVC + Apache CXF + Tomcat。

服务到方面:

json.loads(myjson)

申请方面

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    import org.springframework.stereotype.Component;
    
    import com.dnegi.exception.AnimalNotFoundException;
    import com.dnegi.model.CookingInstruction;
    
    
    /**
     * JAX-RS service interface.
     * 
     * Defines our service methods annotated with the JAX-RS mappings
     * 
     * Endpoints all sit under the root Path of /cookingtime 
     * 
     */
    @Component
    @Path("/cookingtime")
    public interface CookingTimeService {
    
        /**
         * Get cooking instructions for the specified type of meat and weight passed
         * in as URL parameters,e.g. cookingtime/turkey/1.24
         *  
         * @param fleshType type of meat to cook 
         * @param fleshWeight weight of meat to cook
         * 
         * @return instructions containing the oven temperature and cooking time
         * 
         * @throws AnimalNotFoundException if the type of meat is one we don't kNow how to cook
         */
        @GET
        @Produces({MediaType.APPLICATION_JSON})
        @Path("/{fleshType}/{fleshWeight}")
        @CustomAnnotation
        public CookingInstruction getCookingTime(@PathParam("fleshType") String fleshType,@PathParam("fleshWeight") Float fleshWeight) 
                        throws AnimalNotFoundException;
    }

它没有应用于接口,但如果我在类中定义@Path,则注释工作正常。

工作一:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class ReadOnlyUserAspect {


      @Before("@annotation(javax.ws.rs.Path)")
        public void logBefore(JoinPoint joinPoint) {

            System.out.println("logBefore() is running!");
            System.out.println("hijacked : " + joinPoint.getSignature().getName());
            System.out.println("******");
        }
}

似乎注释没有被继承,因此注释上的切入点不起作用。我创建了自定义注释,然后当我在其上使用可继承注释时它起作用了。但我仍然面临如何使 @Path [JAX-RS] 注释适用于我的项目的问题。

import javax.management.Notification;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

@Component
@Path("/notifications")
public class NotificationsResource {
   
    @GET
    @Path("/ping")
    public Response ping() {
        return Response.ok().entity("Service online").build();
    }


    @POST
    @Path("/post/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response postNotification(Notification notification) {
        return Response.status(201).entity(notification).build();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)