使用SwaggerBootstrapUi提供的Basic认证功能控制登陆权限
- 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'
}
- 编写配置类
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();
}
}
- 编写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();
}
}
- 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
- 测试,访问路径http://localhost:8945/doc.html
输入账号密码后
swagger-bootstrap-ui的最后一个版本是1.9.6,已更名为knife4j
官网地址 knife4j