Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable SecurityDefinitions in "../v2/api-docs" json generated file

I want to use swagger client generator and feed the json generated by "../v2/api-docs" from the jHipster application. The problem is that without the security definitions the generated code will not work. The JWT token is not added to the API requests, the code is generated without authentication. The http://petstore.swagger.io/v2/swagger.json example has security and securityDefinitions. Where to modify/configure the jhipster application so that the security and security definitions are generated in the json file? {I manually added the security and security definitions to the json file and after that the generated code works and JWT is enabled in the jHipster application, but I don't want to edit the file each time the API changes... } The "securityDefinitions" and "security":[{"petstore_auth":["write:pets","read:pets"]}] sections are completely missing from the generated json file from the jHipster application, even if JWT is enabled and needed to make API requests.

like image 561
Unimatrix088 Avatar asked Sep 13 '25 18:09

Unimatrix088


1 Answers

Update 28-09-2020:

Since the update to SpringFox 3, classes are now called

  • SpringfoxCustomizer
  • JHipsteSpringfoxCustomizer

Better late than never.

JHipster applications depend on the JHipster Framework, which is in charge of the springfox's Docket configuration.

JHipster Framework's SwaggerAutoConfiguration customizes the springfox Docket with every SwaggerCustomizer bean registered in the application. JHipster registers it's own swagger customizer for the default docket configuration.

This said, you need to add your own docket customizer ir order to include the desired security definitions and any other additional configuration to the springfox's docket. In order to do this you need to:

Create the swagger pacakage inside the already existing config package. Inside it, create a CustomSwaggerConfig class:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomSwaggerConfig {

    public CustomSwaggerConfig() {
    }
    
    @Bean
    public ApplicationSwaggerCustomizer applicationSwaggerCustomizer() {
        return new ApplicationSwaggerCustomizer();
    }

}

And create the ApplicationSwaggerCustomizer class:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.jhipster.config.apidoc.customizer.SwaggerCustomizer;
import springfox.documentation.spring.web.plugins.Docket;

public class ApplicationSwaggerCustomizer implements SwaggerCustomizer {

    private final Logger log = LoggerFactory.getLogger(ApplicationSwaggerCustomizer.class);
    
    public ApplicationSwaggerCustomizer() {
    }

    @Override
    public void customize(Docket docket) {
        log.debug("Customizing springfox docket...");
        // TODO Here you can add all the configurations to the docket
    }

}

Now you can add any additional docket configuration.

like image 177
Sebastiandg7 Avatar answered Sep 17 '25 20:09

Sebastiandg7