All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.purgeteam.swagger.starter.knife4j.DocketPostProcessor Maven / Gradle / Ivy

package com.purgeteam.swagger.starter.knife4j;

import com.google.common.collect.Lists;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * 增强 {@link Docket} 加入全局token 参数
 *
 * @author purgeyao
 * @since 1.0
 */
public class DocketPostProcessor implements BeanPostProcessor, Ordered {

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof Docket) {
      ((Docket) bean).securityContexts(Lists.newArrayList(securityContext()))
          .securitySchemes(Lists.newArrayList(apiKey()));
    }
    return bean;
  }

  @Override
  public int getOrder() {
    return 0;
  }

  private ApiKey apiKey() {
    return new ApiKey("BearerToken", "Authorization", "header");
  }

  private SecurityContext securityContext() {
    return SecurityContext.builder()
        .securityReferences(defaultAuth())
        .forPaths(PathSelectors.regex("/.*"))
        .build();
  }

  List defaultAuth() {
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy