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

cn.home1.oss.lib.security.swagger.AuthenticationTokenHeaderBuilderPlugin Maven / Gradle / Ivy

There is a newer version: 1.0.8.OSS
Show newest version
package cn.home1.oss.lib.security.swagger;

import com.google.common.collect.Lists;

import cn.home1.oss.boot.autoconfigure.AppProperties;
import cn.home1.oss.boot.autoconfigure.AppSecurityProperties;
import cn.home1.oss.lib.security.api.Security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.List;

/**
 * Created by zhanghaolun on 16/11/1.
 */
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public class AuthenticationTokenHeaderBuilderPlugin implements OperationBuilderPlugin {

  private final ParameterBuilder parameterBuilder = new ParameterBuilder();

  @Autowired
  private AppProperties appProperties;

  @Override
  public void apply(final OperationContext operationContext) {
    final AppSecurityProperties appSecurityProperties = this.appProperties.getSecurity();

    final String loginEndpoint = appSecurityProperties.getLoginProcessingUrl();
    // Get endpoint request mapping
    final String mapping = operationContext.requestMappingPattern();

    // Check if private api endpoint
    if (appSecurityProperties.getEnabled() && !loginEndpoint.equals(mapping)) {
      // Create auth header parameter
      final String description = "Authentication token (see " + loginEndpoint + ")";
      final List parameters = Lists.newArrayList( //
        this.parameterBuilder
          .parameterType("header")
          .name(Security.HEADER_AUTH_TOKEN)
          .modelRef(new ModelRef("string"))
          .description(description)
          .allowMultiple(false)
          .required(false)
          .build() //
      );

      // Add parameter to endpoint documentation
      operationContext.operationBuilder().parameters(parameters);
    }
  }

  @Override
  public boolean supports(final DocumentationType delimiter) {
    return DocumentationType.SWAGGER_2.equals(delimiter);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy