Use ContainerRequestFilter in Jersey without web.xml

怎甘沉沦 提交于 2019-12-10 15:59:18

问题


I am trying to intercept requests in Jersey running inside Glassfish.

I created an implementation of ContainerRequestFilter

package mycustom.api.rest.security;

@Provider
public class SecurityProvider implements ContainerRequestFilter {
  @Override
  public ContainerRequest filter(ContainerRequest request) {
    return request;
  }
}

My app is started using a subclass of PackagesResourceConfig.

When Glassfish starts, Jerseys find my provider:

INFO: Provider classes found:
  class mycustom.rest.security.SecurityProvider

But it never hits that filter method. What am I missing??

Everything else seems to be working fine. I added a couple of ContextResolver providers to do JSON mapping and they work fine. Requests hit my resources fine, it just never goes through the filter.


回答1:


I don't think container filters are loaded in as providers. I think you have to set the response filters property. Strangely PackagesResourceConfig doesn't have a setProperty() but you could overload getProperty() and getProperties():

public Object getProperty(String propertyName) {
  if(propertyName.equals(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS)) {
    return new String[] {"mycustom.rest.security.SecurityProvider"};
  } else {
    return super.getProperty(propertyName);
  }
}

public Map<String,Object> getProperties() {
  propName = ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS;
  Map<String,Object> result = super.getProperties();
  result.put(propName,getProperty(propName));
  return result;
}

Actually, reading the javadocs more closely, it appears the preferred method is:

myConfig.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
                              new String [] {"mycustom.rest.security.SecurityProvider"});


来源:https://stackoverflow.com/questions/15685962/use-containerrequestfilter-in-jersey-without-web-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!