swagger2新增访问权限、swagger2用户名密码访问

pom.xml

         <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
			<exclusions>
				<exclusion>
					<artifactId>spring-context</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-aop</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-beans</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
        </dependency>


        <dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>swagger-bootstrap-ui</artifactId>
			<version>1.9.3</version>
		</dependency>

java

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.*"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("Spring Boot中使用Swagger2构建RESTful APIs")
                .version("1.0")
                .build();
    }
}

application.yml

swagger:
  ui-config:
    # method<按方法定义顺序排序>
    operations-sorter: method
  basic:
    enable: true
    ## Basic认证用户名
    username: admin
    ## Basic认证密码
    password: admin
    

访问地址:

http://ip:port/doc.html

http://ip:port/工程名/doc.html

http://ip:portswagger-ui.html

http://ip:port/工程名/swagger-ui.html


版权声明:本文为limingdepoxiao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。