我写了一个方面,我试图用junit测试。
该方面对名为setQuery
的第三方方法提供了@Around建议。
在编译时它抱怨:Can't find referenced pointcut setQuery
这是我的方面:
@Component
@Aspect
public class ElasticsearchQuerySecurityAspect {
@Around("org.elasticsearch.action.search.SearchRequestBuilder.setQuery() && args(queryBuilder)")
public void addFilter(final ProceedingJoinPoint pjp, QueryBuilder queryBuilder) throws Throwable {
Object[] args = pjp.getArgs();
// Set the filter to use our plugin
FilterBuilder securityFilter = FilterBuilders.scriptFilter("visibility-filter")
.lang("native")
.addParam("visibility-field", "visibility")
.addParam("parameter", "default");
// Re-create original query with the filter applied
QueryBuilder newQuery = QueryBuilders.filteredQuery(queryBuilder,securityFilter);
log.info("Adding filter to search request");
// Tell the method to run with the modified parameter
args[0] = newQuery;
pjp.proceed(args);
}
}
这是我的junit测试:
@RunWith(SpringJUnit4ClassRunner.class)// NOTE #1
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@EnableLoadTimeWeaving
@ComponentScan
public class ElasticsearchQuerySecurityTest {
Client client = mock(Client.class);
@Before
public void setUp() throws Exception {
}
@Test
public void test() {
SearchRequestBuilder s = new SearchRequestBuilder(client);
QueryBuilder qb = QueryBuilders.queryString("name:foo");
XContentBuilder builder;
try {
builder = XContentFactory.jsonBuilder();
qb.toXContent(builder, null);
assertEquals("{\"query_string\":{\"query\":\"name:foo\"}}",builder.string());
// Call setQuery() which will invoke the security advice and add a filter to the query
s.setQuery(qb);
builder = XContentFactory.jsonBuilder().startObject();
qb.toXContent(builder, null);
builder.endObject();
assertEquals("{\"query\": "+
"{ \"filtered\": "+
"{ \"query\": "+
"{ \"query_string\": "+
"{ \"name:foo\", } }, "+
"\"filter\": "+
"{ \"script\": "+
"{ \"script\": \"visibility-filter\","+
"\"lang\":\"native\", "+
"\"params\": "+
"{ \"visibility-field\":\"visibility\", "+
"\"parameter\":\"default\" } } } } } }",
builder.string());
} catch (IOException e) {
fail("We threw an I/O exception!");
}
}
}
我在类路径上也有这个aop.xml:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<include within="org.elasticsearch.action.search.*"/>
</weaver>
<aspects>
<aspect name="org.omaas.security.ElasticsearchQuerySecurityAspect"/>
</aspects>
</aspectj>
我在@Around("execution(public * set*())")
尝试了一个方面,发现它只建议当前包中的内容。如何将其应用于第三方包中的内容?
答案 0 :(得分:5)
Spring AOP只能编织成Spring Beans。由于您的第三方目标类不是Spring bean,因此无法对其应用方面。为此,您需要使用更强大的AspectJ,并且不依赖于Spring的AOP Lite&#34;基于动态代理的实现。
使用AspectJ,您有两个选择:
编辑:顺便说一下,你的切入点语法无效。你不能写
@Around("org.elasticsearch.action.search.SearchRequestBuilder.setQuery() && args(queryBuilder)")
相反,你需要像
这样的东西@Around("execution(* org.elasticsearch.action.search.SearchRequestBuilder.setQuery(*)) && args(queryBuilder)")
方法名称是不够的,您必须告诉AOP框架您要捕获它的execution()
(在AspectJ中,它还可以通过call()
捕获所有调用者)。其次,通过指定没有任何参数的方法签名QueryBuilder
,您将不会捕获具有一个setQuery()
参数的方法,因此我建议您使用setQuery(*)
或者,如果您想要更精确,setQuery(org.elasticsearch.index.query.QueryBuilder)
。您还需要在方法名称前面使用public
之类的返回类型和/或修饰符,或者再次使用像*
这样的小丑。