我想在ContainerRequestFilter中更改路径参数
@Provider
@PreMatching
public class XssFilter implements ContainerRequestFilter {
@Override
public void filter( ContainerRequestContext request ) throws IOException {
cleanPathParams(request);
}
private void cleanPathParams( ContainerRequestContext request )
{
UriBuilder builder = request.getUriInfo().getRequestUriBuilder();
MultivaluedMap<String, String> queries = request.getUriInfo().getPathParameters();
cleanParams(builder, queries); //empty if there is a @PreMatching annotation
request.setRequestUri( builder.build() ); // Method could be called only in pre-matching request filter.
}
}
使用@PreMatching:
由于:https://stackoverflow.com/a/35214503/4274360的答案,我删除了@PreMatching注释,但现在:
有什么方法可以替换@PreMatching过滤器中的URI参数?
答案 0 :(得分:0)
我通过更改业务逻辑解决了这个特殊问题(使用xss拒绝了请求,而不是从xss有效负载中转义了参数)。
替换路径和查询参数的解决方案是将Aspcects配置为仅在调用API方法之后但在执行API主体之前调用(想象为API中的第一行)。
private void api(@PathParam("name") String name) {
//configure aspects to run here (1st line) - at this point we got parameters and can write them
//body of endpoint
return;
}