diff --git a/spring-boot-springdoc/src/main/java/com/jnssd/config/CorsConfig.java b/spring-boot-springdoc/src/main/java/com/jnssd/config/CorsConfig.java new file mode 100644 index 0000000..8b0732d --- /dev/null +++ b/spring-boot-springdoc/src/main/java/com/jnssd/config/CorsConfig.java @@ -0,0 +1,22 @@ +package com.jnssd.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * @author zxj + * @since 2023-10-20 14:38:00 + * @apiNote 配置接口跨域请求问题 + */ +@Configuration +public class CorsConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedHeaders("*"); + } + +} diff --git a/spring-boot-springdoc/src/main/java/com/jnssd/controller/MenuController.java b/spring-boot-springdoc/src/main/java/com/jnssd/controller/MenuController.java index ee9e83e..21a00f7 100644 --- a/spring-boot-springdoc/src/main/java/com/jnssd/controller/MenuController.java +++ b/spring-boot-springdoc/src/main/java/com/jnssd/controller/MenuController.java @@ -1,11 +1,10 @@ package com.jnssd.controller; import com.jnssd.model.Menu; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Objects; @@ -17,6 +16,7 @@ import java.util.Objects; * @author zxj * @since 2023-10-12 16:33:23 */ +@Tag(name = "菜单", description = "菜单") @RestController @RequestMapping("/menu") public class MenuController { @@ -27,13 +27,15 @@ public class MenuController { List list = new java.util.ArrayList<>(); @GetMapping("/") + @Operation(summary = "获取所有菜单", description = "获取所有菜单") public ResponseEntity> getAll() { return ResponseEntity.ok(list); } // 添加方法 @PostMapping("add") - public ResponseEntity add(Menu entity) { + @Operation(summary = "添加菜单", description = "添加菜单") + public ResponseEntity add(@RequestBody Menu entity) { try { entity.setId(list.size() + 1); list.add(entity); @@ -45,7 +47,8 @@ public class MenuController { } // 添加方法 - @PostMapping("update") + @PutMapping("update") + @Operation(summary = "修改菜单", description = "修改菜单") public ResponseEntity update(Menu entity) { try { // 修改list下面的数据 @@ -58,7 +61,8 @@ public class MenuController { } // 删除方法 - @PostMapping("delete") + @DeleteMapping("delete") + @Operation(summary = "删除菜单", description = "删除菜单") public ResponseEntity delete(Menu entity) { try { boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId())); @@ -69,7 +73,8 @@ public class MenuController { } // 查询方法 - @PostMapping("query") + @GetMapping("query") + @Operation(summary = "查询菜单", description = "查询菜单") public ResponseEntity query(Integer id) { try { Menu menu = (Menu) list.stream().filter(e -> Objects.equals(e.getId(), id)); diff --git a/spring-boot-springdoc/src/main/java/com/jnssd/controller/RoleController.java b/spring-boot-springdoc/src/main/java/com/jnssd/controller/RoleController.java index 1b7e188..f6270a1 100644 --- a/spring-boot-springdoc/src/main/java/com/jnssd/controller/RoleController.java +++ b/spring-boot-springdoc/src/main/java/com/jnssd/controller/RoleController.java @@ -1,11 +1,10 @@ package com.jnssd.controller; import com.jnssd.model.Role; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Objects; @@ -17,6 +16,7 @@ import java.util.Objects; * @author zxj * @since 2023-10-12 16:32:38 */ +@Tag(name = "角色", description = "角色") @RestController @RequestMapping("/role") public class RoleController { @@ -27,12 +27,14 @@ public class RoleController { List list = new java.util.ArrayList<>(); @GetMapping("/") + @Operation(summary = "获取所有角色", description = "获取所有角色") public ResponseEntity> getAll() { return ResponseEntity.ok(list); } // 添加方法 @PostMapping("add") + @Operation(summary = "添加角色", description = "添加角色") public ResponseEntity add(Role entity) { try { entity.setId(list.size() + 1); @@ -45,7 +47,8 @@ public class RoleController { } // 添加方法 - @PostMapping("update") + @PutMapping("update") + @Operation(summary = "修改角色", description = "修改角色") public ResponseEntity update(Role entity) { try { // 修改list下面的数据 @@ -58,7 +61,8 @@ public class RoleController { } // 删除方法 - @PostMapping("delete") + @DeleteMapping("delete") + @Operation(summary = "删除角色", description = "删除角色") public ResponseEntity delete(Role entity) { try { boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId())); @@ -69,7 +73,8 @@ public class RoleController { } // 查询方法 - @PostMapping("query") + @GetMapping("query") + @Operation(summary = "查询角色", description = "查询角色") public ResponseEntity query(Integer id) { try { Role role = (Role) list.stream().filter(e -> Objects.equals(e.getId(), id)); diff --git a/spring-boot-springdoc/src/main/java/com/jnssd/controller/UserController.java b/spring-boot-springdoc/src/main/java/com/jnssd/controller/UserController.java index a5e9dda..3f7dec8 100644 --- a/spring-boot-springdoc/src/main/java/com/jnssd/controller/UserController.java +++ b/spring-boot-springdoc/src/main/java/com/jnssd/controller/UserController.java @@ -1,11 +1,10 @@ package com.jnssd.controller; import com.jnssd.model.User; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Objects; @@ -17,6 +16,7 @@ import java.util.Objects; * @author zxj * @since 2023-10-12 16:30:04 */ +@Tag(name = "用户", description = "用户") @RestController @RequestMapping("/user") public class UserController { @@ -27,12 +27,14 @@ public class UserController { List list = new java.util.ArrayList<>(); @GetMapping("/") + @Operation(summary = "获取所有用户", description = "获取所有用户") public ResponseEntity> getAll() { return ResponseEntity.ok(list); } // 添加方法 @PostMapping("add") + @Operation(summary = "添加用户", description = "添加用户") public ResponseEntity add(User entity) { try { entity.setId(list.size() + 1); @@ -45,7 +47,8 @@ public class UserController { } // 添加方法 - @PostMapping("update") + @PutMapping("update") + @Operation(summary = "修改用户", description = "修改用户") public ResponseEntity update(User entity) { try { // 修改list下面的数据 @@ -58,7 +61,8 @@ public class UserController { } // 删除方法 - @PostMapping("delete") + @DeleteMapping("delete") + @Operation(summary = "删除用户", description = "删除用户") public ResponseEntity delete(User entity) { try { boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId())); @@ -69,7 +73,8 @@ public class UserController { } // 查询方法 - @PostMapping("query") + @GetMapping("query") + @Operation(summary = "查询用户", description = "查询用户") public ResponseEntity query(Integer id) { try { User user = (User) list.stream().filter(e -> Objects.equals(e.getId(), id)); diff --git a/spring-boot-springdoc/src/test/java/com/demo/TestDemo.java b/spring-boot-springdoc/src/test/java/com/demo/TestDemo.java new file mode 100644 index 0000000..8951b02 --- /dev/null +++ b/spring-boot-springdoc/src/test/java/com/demo/TestDemo.java @@ -0,0 +1,30 @@ +package com.demo; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +/** + *

spring-boot-openapi

+ *

1

+ * + * @author zxj + * @since 2023-11-27 10:12:54 + */ +public class TestDemo { + + @Test + public void uoso_2311271014() { + String filterAllow = "111;222;;3333"; + + String referer = "21"; + + + if (Arrays.stream(filterAllow.split(";")).noneMatch(item-> item.contains(referer))) { + System.out.println(111); + } else { + System.out.println("含有"); + } + } + +} diff --git a/spring-boot-swagger/src/main/java/com/jnssd/config/SwaggerOpenApiConfig.java b/spring-boot-swagger/src/main/java/com/jnssd/config/SwaggerOpenApiConfig.java index 77e3ce2..514486a 100644 --- a/spring-boot-swagger/src/main/java/com/jnssd/config/SwaggerOpenApiConfig.java +++ b/spring-boot-swagger/src/main/java/com/jnssd/config/SwaggerOpenApiConfig.java @@ -151,7 +151,7 @@ public class SwaggerOpenApiConfig { // 这里的SecurityReference第一个参数值必须和SecurityScheme的name值一致 Arrays.stream(securitySchemeNames).forEach(name -> list.add(new SecurityReference(name, new AuthorizationScope[0]))); return SecurityContext.builder().operationSelector(operationContext -> { - System.out.println("operationContext" + operationContext); + // System.out.println("operationContext" + operationContext); // 除了get方法其他方法都要校验 // return !operationContext.httpMethod().name().equals("GET");