Commit 7c07ae9421850650b9da6d453c498cc3049a83a3
1 parent
33fba05a
修复文档页面不可用BUG,支持设置认证消息头来方便调用其他接口
Showing
5 changed files
with
20 additions
and
14 deletions
pom.xml
| ... | ... | @@ -163,21 +163,16 @@ |
| 163 | 163 | </dependency> |
| 164 | 164 | |
| 165 | 165 | <!--在线文档 --> |
| 166 | + <!--在线文档 --> | |
| 166 | 167 | <dependency> |
| 167 | 168 | <groupId>org.springdoc</groupId> |
| 168 | 169 | <artifactId>springdoc-openapi-ui</artifactId> |
| 169 | - <version>1.7.0</version> | |
| 170 | - <exclusions> | |
| 171 | - <exclusion> | |
| 172 | - <groupId>org.yaml</groupId> | |
| 173 | - <artifactId>snakeyaml</artifactId> | |
| 174 | - </exclusion> | |
| 175 | - </exclusions> | |
| 170 | + <version>1.6.10</version> | |
| 176 | 171 | </dependency> |
| 177 | 172 | <dependency> |
| 178 | - <groupId>org.yaml</groupId> | |
| 179 | - <artifactId>snakeyaml</artifactId> | |
| 180 | - <version>2.2</version> | |
| 173 | + <groupId>org.springdoc</groupId> | |
| 174 | + <artifactId>springdoc-openapi-security</artifactId> | |
| 175 | + <version>1.6.10</version> | |
| 181 | 176 | </dependency> |
| 182 | 177 | |
| 183 | 178 | <dependency> | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/SpringDocConfig.java
| 1 | 1 | package com.genersoft.iot.vmp.conf; |
| 2 | 2 | |
| 3 | +import com.genersoft.iot.vmp.conf.security.JwtUtils; | |
| 4 | +import io.swagger.v3.oas.models.Components; | |
| 3 | 5 | import io.swagger.v3.oas.models.OpenAPI; |
| 4 | 6 | import io.swagger.v3.oas.models.info.Contact; |
| 5 | 7 | import io.swagger.v3.oas.models.info.Info; |
| 6 | 8 | import io.swagger.v3.oas.models.info.License; |
| 9 | +import io.swagger.v3.oas.models.security.SecurityScheme; | |
| 7 | 10 | import org.springframework.core.annotation.Order; |
| 8 | 11 | import org.springdoc.core.GroupedOpenApi; |
| 9 | 12 | import org.springframework.beans.factory.annotation.Value; |
| ... | ... | @@ -26,10 +29,14 @@ public class SpringDocConfig { |
| 26 | 29 | contact.setName("pan"); |
| 27 | 30 | contact.setEmail("648540858@qq.com"); |
| 28 | 31 | return new OpenAPI() |
| 32 | + .components(new Components() | |
| 33 | + .addSecuritySchemes(JwtUtils.HEADER, new SecurityScheme() | |
| 34 | + .type(SecurityScheme.Type.HTTP) | |
| 35 | + .bearerFormat("JWT"))) | |
| 29 | 36 | .info(new Info().title("WVP-PRO 接口文档") |
| 30 | 37 | .contact(contact) |
| 31 | 38 | .description("开箱即用的28181协议视频平台") |
| 32 | - .version("v2.0") | |
| 39 | + .version("v3.1.0") | |
| 33 | 40 | .license(new License().name("Apache 2.0").url("http://springdoc.org"))); |
| 34 | 41 | } |
| 35 | 42 | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java
| ... | ... | @@ -28,7 +28,7 @@ public class JwtUtils implements InitializingBean { |
| 28 | 28 | |
| 29 | 29 | private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); |
| 30 | 30 | |
| 31 | - private static final String HEADER = "access-token"; | |
| 31 | + public static final String HEADER = "access-token"; | |
| 32 | 32 | |
| 33 | 33 | private static final String AUDIENCE = "Audience"; |
| 34 | 34 | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java
| ... | ... | @@ -68,6 +68,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 68 | 68 | matchers.add("/"); |
| 69 | 69 | matchers.add("/#/**"); |
| 70 | 70 | matchers.add("/static/**"); |
| 71 | + matchers.add("/swagger-ui.html"); | |
| 72 | + matchers.add("/swagger-ui/"); | |
| 71 | 73 | matchers.add("/index.html"); |
| 72 | 74 | matchers.add("/doc.html"); |
| 73 | 75 | matchers.add("/webjars/**"); |
| ... | ... | @@ -77,6 +79,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 77 | 79 | matchers.add("/api/device/query/snap/**"); |
| 78 | 80 | matchers.add("/record_proxy/*/**"); |
| 79 | 81 | matchers.add("/api/emit"); |
| 82 | + matchers.add("/favicon.ico"); | |
| 80 | 83 | // 可以直接访问的静态数据 |
| 81 | 84 | web.ignoring().antMatchers(matchers.toArray(new String[0])); |
| 82 | 85 | } |
| ... | ... | @@ -113,7 +116,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 113 | 116 | .authorizeRequests() |
| 114 | 117 | .requestMatchers(CorsUtils::isPreFlightRequest).permitAll() |
| 115 | 118 | .antMatchers(userSetting.getInterfaceAuthenticationExcludes().toArray(new String[0])).permitAll() |
| 116 | - .antMatchers("/api/user/login", "/index/hook/**").permitAll() | |
| 119 | + .antMatchers("/api/user/login", "/index/hook/**", "/swagger-ui/**", "/doc.html").permitAll() | |
| 117 | 120 | .anyRequest().authenticated() |
| 118 | 121 | // 异常处理器 |
| 119 | 122 | .and() | ... | ... |
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java
| ... | ... | @@ -14,6 +14,7 @@ import com.genersoft.iot.vmp.vmanager.bean.WVPResult; |
| 14 | 14 | import com.github.pagehelper.PageInfo; |
| 15 | 15 | import io.swagger.v3.oas.annotations.Operation; |
| 16 | 16 | import io.swagger.v3.oas.annotations.Parameter; |
| 17 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; | |
| 17 | 18 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 18 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
| 19 | 20 | import org.springframework.security.authentication.AuthenticationManager; |
| ... | ... | @@ -95,7 +96,7 @@ public class UserController { |
| 95 | 96 | |
| 96 | 97 | |
| 97 | 98 | @PostMapping("/add") |
| 98 | - @Operation(summary = "添加用户") | |
| 99 | + @Operation(summary = "添加用户", security = @SecurityRequirement(name = JwtUtils.HEADER)) | |
| 99 | 100 | @Parameter(name = "username", description = "用户名", required = true) |
| 100 | 101 | @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true) |
| 101 | 102 | @Parameter(name = "roleId", description = "角色ID", required = true) | ... | ... |