Jersey 带有春季AOP的运动衫
我希望我的AOP建议能够处理当前正在执行的Jersey资源的HttpContext。示例提到用户可以获得请求和身份验证等,但是如何获得通知上下文中的任何值呢 当前我的资源定义如下所示:Jersey 带有春季AOP的运动衫,jersey,spring-aop,Jersey,Spring Aop,我希望我的AOP建议能够处理当前正在执行的Jersey资源的HttpContext。示例提到用户可以获得请求和身份验证等,但是如何获得通知上下文中的任何值呢 当前我的资源定义如下所示: @Singleton @Path("/persist") public class ContentResource { @PUT @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XM
@Singleton
@Path("/persist")
public class ContentResource {
@PUT
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Auth
public Content save(Content content){
//Do some thing with the data
}
}
该方面的定义如下:
@Aspect
public class AuthorizeProcessor {
@Around(value="@annotation(package.Auth) and @annotation(auth)", argNames = "auth")
public Object authorize(ProceedingJoinPoint pjp, Auth auth) throws Throwable{
//How do I get the HttpContext here?
return pjp.proceed();
}
}
当然,这很可能太晚了,但是我通过在进行授权的服务前面实现Servlet过滤器来完成您正在做的事情。这完全避免了AOP的需要,并且它直接提供实际的ServletRequest,而无需在系统周围工作来获取它 具有讽刺意味的是,如果你真的想要AOP,你帮我回答的问题可能会在这里帮助你 您可以向请求提供Spring RequestContextFilter,然后访问
HttpServletRequest
(与HttpContext
相反):
哈哈。那是一个年轻的我:)。我最终使用了
ResourceFilterFactory
来解决这个问题。谢谢
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/path/to/services/*</url-pattern>
</filter-mapping>
/**
* Get the current {@link HttpServletRequest} [hopefully] being made
* containing the {@link HttpServletRequest#getAttribute(String) attribute}.
* @return Never {@code null}.
* @throws NullPointerException if the Servlet Filter for the {@link
* RequestContextHolder} is not setup
* appropriately.
* @see org.springframework.web.filter.RequestContextFilter
*/
protected HttpServletRequest getRequest()
{
// get the request from the Spring Context Holder (this is done for
// every request by a filter)
ServletRequestAttributes attributes =
(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
return attributes.getRequest();
}