Commit 7976840c51003041649493805d598cbf13c90333

Authored by zhangdaihao
1 parent 9d3b7722

swagger设置全局token,解决接口需要token验证的问题

jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/config/Swagger2Config.java
1 package org.jeecg.config; 1 package org.jeecg.config;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
  4 +import java.util.Collections;
4 import java.util.List; 5 import java.util.List;
5 6
6 import org.jeecg.modules.shiro.vo.DefContants; 7 import org.jeecg.modules.shiro.vo.DefContants;
@@ -12,6 +13,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -12,6 +13,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12 import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; 13 import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
13 14
14 import io.swagger.annotations.ApiOperation; 15 import io.swagger.annotations.ApiOperation;
  16 +import springfox.documentation.service.ApiKey;
15 import springfox.documentation.service.Parameter; 17 import springfox.documentation.service.Parameter;
16 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
17 import springfox.documentation.builders.ApiInfoBuilder; 19 import springfox.documentation.builders.ApiInfoBuilder;
@@ -20,6 +22,7 @@ import springfox.documentation.builders.PathSelectors; @@ -20,6 +22,7 @@ import springfox.documentation.builders.PathSelectors;
20 import springfox.documentation.builders.RequestHandlerSelectors; 22 import springfox.documentation.builders.RequestHandlerSelectors;
21 import springfox.documentation.schema.ModelRef; 23 import springfox.documentation.schema.ModelRef;
22 import springfox.documentation.service.ApiInfo; 24 import springfox.documentation.service.ApiInfo;
  25 +import springfox.documentation.service.SecurityScheme;
23 import springfox.documentation.spi.DocumentationType; 26 import springfox.documentation.spi.DocumentationType;
24 import springfox.documentation.spring.web.plugins.Docket; 27 import springfox.documentation.spring.web.plugins.Docket;
25 import springfox.documentation.swagger2.annotations.EnableSwagger2; 28 import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -62,9 +65,20 @@ public class Swagger2Config implements WebMvcConfigurer { @@ -62,9 +65,20 @@ public class Swagger2Config implements WebMvcConfigurer {
62 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 65 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
63 .paths(PathSelectors.any()) 66 .paths(PathSelectors.any())
64 .build() 67 .build()
65 - .globalOperationParameters(setHeaderToken()); 68 + .securitySchemes(Collections.singletonList(securityScheme()));
  69 + //.globalOperationParameters(setHeaderToken());
66 } 70 }
67 71
  72 + /***
  73 + * oauth2配置
  74 + * 需要增加swagger授权回调地址
  75 + * http://localhost:8888/webjars/springfox-swagger-ui/o2c.html
  76 + * @return
  77 + */
  78 + @Bean
  79 + SecurityScheme securityScheme() {
  80 + return new ApiKey(DefContants.X_ACCESS_TOKEN, DefContants.X_ACCESS_TOKEN, "header");
  81 + }
68 /** 82 /**
69 * JWT token 83 * JWT token
70 * @return 84 * @return
@@ -90,11 +104,11 @@ public class Swagger2Config implements WebMvcConfigurer { @@ -90,11 +104,11 @@ public class Swagger2Config implements WebMvcConfigurer {
90 .version("1.0") 104 .version("1.0")
91 // .termsOfServiceUrl("NO terms of service") 105 // .termsOfServiceUrl("NO terms of service")
92 // 描述 106 // 描述
93 - .description("restful 风格接口") 107 + .description("后台API接口")
94 // 作者 108 // 作者
95 -// .contact(new Contact("scott", "http://jeecg.org", "jeecgos@163.com"))  
96 -// .license("The Apache License, Version 2.0")  
97 -// .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") 109 + .contact("JEECG团队")
  110 + .license("The Apache License, Version 2.0")
  111 + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
98 .build(); 112 .build();
99 } 113 }
100 114