Swagger2开启登陆权限控制

使用SwaggerBootstrapUi提供的Basic认证功能控制登陆权限

  1. gradle 添加依赖
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
    runtimeOnly 'mysql:mysql-connector-java'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.4'
    compile('org.springframework.boot:spring-boot-starter-cache')
    //swagger2
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.0'
    //swagger-ui
    compile group: 'com.github.xiaoymin', name: 'swagger-bootstrap-ui', version: '1.9.6'
}
  1. 编写配置类
package org.example.base.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author l
 * @date Created in 2020/11/2 15:08
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {


    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 选择那些路径和api会生成document
                .select()
                // 对所有api进行监控
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 对所有路径进行监控
                .paths(path -> !"/error".equals(path))
                .build();
    }



    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("")
                .contact(new Contact("", "", ""))
                .version("1.0")
                .build();
    }







}

  1. 编写controller层
package org.example.base.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.base.bean.User;
import org.example.base.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author l
 * @date Created in 2020/10/23 15:46
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {

    private UserService userService;
    private Cache secondCache;

    @Autowired
    public UserController(UserService userService, @Qualifier("SECOND_CACHE") Cache secondCache) {
        this.userService = userService;
        this.secondCache = secondCache;
    }


    public User getUser(Integer id) {
        Cache.ValueWrapper valueWrapper = secondCache.get(id);
        return valueWrapper == null ? null : (User) valueWrapper.get();
    }

    public void setUser(User user) {
        secondCache.put(user.getId(), user);
    }


    @GetMapping("/queryUser")
    @ResponseBody
    @ApiOperation(value = "查询用户信息", httpMethod = "GET")
    public User queryUser(@ApiParam(required = true, value = "用户ID") @RequestParam(value = "ID") Integer id) {
        Cache.ValueWrapper valueWrapper = secondCache.get(id);
        return valueWrapper == null ? new User(1, "haha", 90) : (User) valueWrapper.get();
    }


}

  1. application.properties 添加账号密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

server.port=8945



## 开启Swagger的Basic认证功能,默认是false,开启功能,默认账号密码是admin/123321
swagger.basic.enable=true
## Basic认证用户名
swagger.basic.username=l
## Basic认证密码
swagger.basic.password=123456

#屏蔽所有Swagger的相关资源
#swagger.production=true
  1. 测试,访问路径http://localhost:8945/doc.html

在这里插入图片描述

输入账号密码后

在这里插入图片描述

swagger-bootstrap-ui的最后一个版本是1.9.6,已更名为knife4j
官网地址 knife4j

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值