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