找到你要的答案

Q:Swagger - Extending SwaggerSpecFilter

Q:延伸swaggerspecfilter招摇

I'm trying to write my own Swagger API Filter to hide certain things from appearing in the API. I think I have a pretty good idea of what I should be doing but I must be missing something in the details.

I'm using Swagger Core 1.3.10 and Spring 3.1. Here's some of what I have now:

SwaggerApiAuthorizationFilter

public class SwaggerApiAuthorizationFilter implements SwaggerSpecFilter {

    private final static Logger logger = LoggerFactory.getLogger(SwaggerApiAuthorizationFilter.class);

    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription api, Map<String, List<String>> params, 
            Map<String, String> cookies, Map<String, List<String>> headers) {

        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params, 
            Map<String, String> cookies, Map<String, List<String>> headers) {

        // do not allow the documentation to be generated on parameters that have their access set to "internal"
        if (parameter.paramAccess().isDefined() && parameter.paramAccess().get().equalsIgnoreCase("internal")) {

            logger.debug("The following parameter has been hidden from the Swagger API documentation: " + parameter.name());

            return false;

        } else {

            return true;
        }
    }

}

SwaggerServlet

public class SwaggerServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private final static Logger logger = LoggerFactory.getLogger(SwaggerServlet.class);

    BeanConfig beanConfig;
    SwaggerApiAuthorizationFilter swaggerApiAuthorizationFilter;

    public void setBeanConfig(BeanConfig beanConfig) {
        this.beanConfig = beanConfig;
    }

    public void setSwaggerApiAuthorizationFilter(SwaggerApiAuthorizationFilter swaggerApiAuthorizationFilter) {
        this.swaggerApiAuthorizationFilter = swaggerApiAuthorizationFilter;
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        try {

            super.init(servletConfig);

            beanConfig.setBasePath("/mbl/services");
            beanConfig.setVersion("1.0");
            beanConfig.setResourcePackage("com.whatever.resources");
            beanConfig.setScan(true);

            FilterFactory.setFilter(swaggerApiAuthorizationFilter);

            logger.debug("The Swagger servlet has been initialized");

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

spring config file

<!-- Swagger Configuration and Providers -->
<bean id="beanConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
    <property name="title" value="Java API"/>
    <property name="version" value="1.0" />
    <property name="basePath" value="/mbl/services"/>
    <property name="resourcePackage" value="com.whatever.resources"/>
    <property name="scan" value="true"/>
</bean>

<bean id="swaggerApiAuthorizationFilter" class="com.whatever.util.SwaggerApiAuthorizationFilter" />

web.xml

<!-- Enabling Swagger servlet -->
<servlet>
    <servlet-name>Swagger Servlet</servlet-name>
    <servlet-class>com.whatever.web.SwaggerServlet</servlet-class>
        <init-param>
            <param-name>swagger.filter</param-name>
            <param-value>com.whatever.util.SwaggerApiAuthorizationFilter</param-value>
        </init-param>
    <load-on-startup>-1</load-on-startup> 
</servlet>
<servlet-mapping>
    <servlet-name>Swagger Servlet</servlet-name>
    <url-pattern>/api-docs</url-pattern>
</servlet-mapping>

Resource class

    public Response getUserInfo( 
    @Context HttpHeaders headers, 
    @ApiParam(name="brand", access="internal") @CookieParam(value = "brand") String brand) {

It appears that my SwaggerApiAuthorizationFilter isn't even being called. My guess is that something is wrong in my configuration either in the web.xml or the SwaggerServlet. Does anyone see anything above that looks incorrect or does anyone have any ideas?

我想写我自己扬鞭API过滤器出现在API隐藏某些东西。我想我有一个很好的想法,我应该做什么,但我一定缺少一些细节。

我使用的核心1.3.10招摇和弹簧3.1。这里有一些我现在:

swaggerapiauthorizationfilter

public class swaggerapiauthorizationfilter implements SwaggerSpecFilter {

    private final static Logger logger = LoggerFactory.getLogger(swaggerapiauthorizationfilter.class);

    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription api, Map<String, List<String>> params, 
            Map<String, String> cookies, Map<String, List<String>> headers) {

        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params, 
            Map<String, String> cookies, Map<String, List<String>> headers) {

        // do not allow the documentation to be generated on parameters that have their access set to "internal"
        if (parameter.paramAccess().isDefined() && parameter.paramAccess().get().equalsIgnoreCase("internal")) {

            logger.debug("The following parameter has been hidden from the Swagger API documentation: " + parameter.name());

            return false;

        } else {

            return true;
        }
    }

}

swaggerservlet

public class swaggerservlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private final static Logger logger = LoggerFactory.getLogger(swaggerservlet.class);

    BeanConfig beanConfig;
    swaggerapiauthorizationfilter swaggerApiAuthorizationFilter;

    public void setBeanConfig(BeanConfig beanConfig) {
        this.beanConfig = beanConfig;
    }

    public void setswaggerapiauthorizationfilter(swaggerapiauthorizationfilter swaggerApiAuthorizationFilter) {
        this.swaggerApiAuthorizationFilter = swaggerApiAuthorizationFilter;
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        try {

            super.init(servletConfig);

            beanConfig.setBasePath("/mbl/services");
            beanConfig.setVersion("1.0");
            beanConfig.setResourcePackage("com.whatever.resources");
            beanConfig.setScan(true);

            FilterFactory.setFilter(swaggerApiAuthorizationFilter);

            logger.debug("The Swagger servlet has been initialized");

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

Spring配置文件

<!-- Swagger Configuration and Providers -->
<bean id="beanConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
    <property name="title" value="Java API"/>
    <property name="version" value="1.0" />
    <property name="basePath" value="/mbl/services"/>
    <property name="resourcePackage" value="com.whatever.resources"/>
    <property name="scan" value="true"/>
</bean>

<bean id="swaggerApiAuthorizationFilter" class="com.whatever.util.swaggerapiauthorizationfilter" />

web.xml

<!-- Enabling Swagger servlet -->
<servlet>
    <servlet-name>Swagger Servlet</servlet-name>
    <servlet-class>com.whatever.web.swaggerservlet</servlet-class>
        <init-param>
            <param-name>swagger.filter</param-name>
            <param-value>com.whatever.util.swaggerapiauthorizationfilter</param-value>
        </init-param>
    <load-on-startup>-1</load-on-startup> 
</servlet>
<servlet-mapping>
    <servlet-name>Swagger Servlet</servlet-name>
    <url-pattern>/api-docs</url-pattern>
</servlet-mapping>

资源类

    public Response getUserInfo( 
    @Context HttpHeaders headers, 
    @ApiParam(name="brand", access="internal") @CookieParam(value = "brand") String brand) {

It appears that my swaggerapiauthorizationfilter isn't even being called. My guess is that something is wrong in my configuration either in the web.xml or the swaggerservlet. Does anyone see anything above that looks incorrect or does anyone have any ideas?

answer1: 回答1:

I'm just guessing but I think you need to implement Filter as well as the SwaggerSpecFilter interface instead of extending HttpServlet (looking at the source it does not appear SwaggerSpecFilter extends Filter as one would expect), then configure it as a filter in your web.xml instead of a servlet. It would be something like this:

<filter> <filter-name>Swagger Filter</filter-name> <filter-class>com.whatever.util.SwaggerApiAuthorizationFilter</filter-class> </filter> <filter-mapping> <filter-name>Swagger Filter</filter-name> <url-pattern>/api-docs</url-pattern> </filter-mapping>

Again, not sure if this will work but just a guess off the top of my head.

我只是猜测,但我认为你需要实现滤波以及反而延长HttpServlet的swaggerspecfilter接口(看着它不出现swaggerspecfilter扩展滤波器作为一种期望源),然后将其配置为在web.xml来代替一个Servlet过滤器。它会是这样的:

<filter> <filter-name>Swagger Filter</filter-name> <filter-class>com.whatever.util.swaggerapiauthorizationfilter</filter-class> </filter> <filter-mapping> <filter-name>Swagger Filter</filter-name> <url-pattern>/api-docs</url-pattern> </filter-mapping>

再次,不知道这是否会工作,但只是一个猜测我的头。

answer2: 回答2:

I've just used the filter successfully. I had problems to make filter function properly. The problem was that there was a problem to find my filter's implemention class. When I changed the package to a place where swagger's class could access, everything funtioned correctly. Next, I just had to implement my logic on my filter class. My filter class implemented the interface SwaggerSpecFilter too. Best regards.

I'm using DefaultJaxrsConfig:

<servlet>
  <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
    <init-param>
        <param-name>api.version</param-name>
        <param-value>1.0.0</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>/my-service/service/</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.filter</param-name>
        <param-value>mypackage.SwaggerFilter</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

我刚刚成功地使用了过滤器。我有问题,使过滤器功能正常。问题是,有一个找到我的滤波器的实现类的问题。当我改变封装到一个地方,大摇大摆的类可以访问,一切运行正常。接下来,我只需要在过滤器类上实现我的逻辑。我的过滤器类实现的接口swaggerspecfilter太。诚挚的问候。

我用的DefaultJaxrsConfig:

<servlet>
  <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
    <init-param>
        <param-name>api.version</param-name>
        <param-value>1.0.0</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>/my-service/service/</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.filter</param-name>
        <param-value>mypackage.SwaggerFilter</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>
java  spring  swagger