前言:本来想用Swagger3呢,但是找了好久都没有找到Swagger3的文档,百度也搜不到。这里说的是添加security的登录、登出配置。所以就借用了网上的部分代码做的
- 首先在pom.xml中添加swagger2的坐标
io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
- 创建一个swagger的配置文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Swagger2Config implements WebMvcConfigurer {
@Value("${debugger_tools}")
private Boolean debuggerTools;
@Bean
public Docket createRestApi() {
return new Docket(documentationType.SWAGGER_2)
// 这里表示是否启用swagger
.enable(debuggerTools)
// 项目前缀
.apiInfo(new ApiInfoBuilder()
.title("*******")//网页标题
.description("一个简单的测试")//网页描述
.contact(new Contact("tom",null,"tom@163.com"))//用户名,url,email
.version("1.00")//接口版本号
.build());
}
}
- 在security中configure配置,其他配置这里不介绍
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// 设置Swagger2匿名访问
.antMatchers("/swagger-ui.html", "/webjars
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {
@Override
public List apply(documentationContext context) {
return Arrays.asList(login(),unLogin());
}
public Parameter username(){
return new ParameterBuilder()
.description("用户名")
.type(new TypeResolver().resolve(String.class))
.name("username")
.parameterType("form")
.parameterAccess("access")
.defaultValue("admin")
.required(true)
.modelRef(new ModelRef("string"))
.build();
}
public Parameter password(){
return new ParameterBuilder()
.description("密码")
.type(new TypeResolver().resolve(String.class))
.name("password")
.parameterType("form")
.parameterAccess("access")
.defaultValue("admin123")
.required(true)
.modelRef(new ModelRef("string"))
.build();
}
public Parameter code(){
return new ParameterBuilder()
.description("验证码")
.type(new TypeResolver().resolve(String.class))
.name("code")
.parameterType("form")
.parameterAccess("access")
.required(false)
.modelRef(new ModelRef("string"))
.build();
}
public Set responseMessages(){
Set messages = new HashSet<>();
messages.add(new ResponseMessageBuilder().code(200).message("ok").build());
messages.add(new ResponseMessageBuilder().code(401).message("Unauthorized").build());
messages.add(new ResponseMessageBuilder().code(403).message("Forbidden").build());
messages.add(new ResponseMessageBuilder().code(404).message("Not Found").build());
return messages;
}
public ApiDescription login(){
Operation loginOperation = new OperationBuilder(new CachingOperationNameGenerator())
.tags(Collections.singleton("用户接口"))
.summary("登录")
.notes("登录")
.method(HttpMethod.POST)
.consumes(Collections.singleton(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) // 接收参数格式
.produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE)) // 返回参数格式
.parameters(Arrays.asList(username(),password(),code())) //定义参数
.responseMessages(responseMessages())
.build();
ApiDescription loginApiDescription = new ApiDescription("login", "/login","登录接口",
Arrays.asList(loginOperation), false);
return loginApiDescription;
}
public ApiDescription unLogin(){
Operation unLoginOperation = new OperationBuilder(new CachingOperationNameGenerator())
.tags(Collections.singleton("用户接口"))
.summary("登出")
.notes("登出")
.method(HttpMethod.GET)
.produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE)) // 返回参数格式
.responseMessages(responseMessages())
.build();
ApiDescription unLoginApiDescription = new ApiDescription("logout", "/logout","退出登录接口",
Arrays.asList(unLoginOperation), false);
return unLoginApiDescription;
}
@Override
public boolean supports(documentationType documentationType) {
return documentationType.SWAGGER_2.equals(documentationType);
}
}
- 在第二步中有一个属性是debuggerTools,说是导入配置文件中的值,是的,正式中swagger是为了方便调试用的,所以在生产环境中是无需启用,因此可以使用配置文件来控制是否启用swagger。可以在不同的环境变量中定义debugger_tools属性值。根据启用不同环境配置,来决定是否启用swagger(SpringBoot如何配置多环境)
如下:
# 是否启用swagger,postman调试 # application-dev.yml环境 debugger_tools: true # application-test.yml、application-prod.yml环境 debugger_tools: false
- 结果,因为我的登录、登出路径是重定义加了/user 。默认没有
参考文章:
Security如何配置swagger2
swagger2添加Security登录接口



