Compare commits
4 Commits
cafd2708b4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 98e6d6a557 | |||
| 8a026c3458 | |||
| 4634205384 | |||
| 9395d8d2c9 |
@@ -0,0 +1,22 @@
|
||||
package icu.sunway.ai_spring_example.Config;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* OpenAPI配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
@Bean
|
||||
public OpenAPI customOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("Swagger API文档")
|
||||
.version("1.0.0")
|
||||
.description("查询 API 文档"));
|
||||
}
|
||||
}
|
||||
@@ -11,23 +11,21 @@ import org.springframework.security.web.SecurityFilterChain;
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
// 启用跨域配置
|
||||
.cors(cors -> cors.configurationSource(
|
||||
request -> new org.springframework.web.cors.CorsConfiguration().applyPermitDefaultValues()))
|
||||
// 禁用默认的登录表单和HTTP基本认证
|
||||
.formLogin(form -> form.disable())
|
||||
.httpBasic(basic -> basic.disable())
|
||||
// 允许所有请求通过,取消默认登录验证
|
||||
.authorizeHttpRequests((authz) -> authz
|
||||
.anyRequest().permitAll())
|
||||
// 禁用CSRF保护
|
||||
.csrf(csrf -> csrf.disable())
|
||||
// 设置会话创建策略为无状态
|
||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
// 禁用默认的登录表单和HTTP基本认证
|
||||
.formLogin(form -> form.disable())
|
||||
.httpBasic(basic -> basic.disable())
|
||||
// 允许所有请求通过,取消默认登录验证
|
||||
.authorizeHttpRequests((authz) -> authz
|
||||
.anyRequest().permitAll())
|
||||
// 禁用CSRF保护
|
||||
.csrf(csrf -> csrf.disable())
|
||||
// 设置会话创建策略为无状态
|
||||
.sessionManagement(session -> session
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||
|
||||
return http.build();
|
||||
}
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package icu.sunway.ai_spring_example.Controllers;
|
||||
package icu.sunway.ai_spring_example.Controller.ExampleEntityController;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
111
src/main/java/icu/sunway/ai_spring_example/Utils/PageUtils.java
Normal file
111
src/main/java/icu/sunway/ai_spring_example/Utils/PageUtils.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package icu.sunway.ai_spring_example.Utils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页工具类
|
||||
*/
|
||||
public class PageUtils {
|
||||
|
||||
/**
|
||||
* 默认页码
|
||||
*/
|
||||
public static final Integer DEFAULT_PAGE = 1;
|
||||
|
||||
/**
|
||||
* 默认每页数量
|
||||
*/
|
||||
public static final Integer DEFAULT_LIMIT = 10;
|
||||
|
||||
/**
|
||||
* 最大每页数量
|
||||
*/
|
||||
public static final Integer MAX_LIMIT = 100;
|
||||
|
||||
/**
|
||||
* 分页结果类
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class PaginationResult<T> {
|
||||
private Integer currentPage;
|
||||
private Integer pageSize;
|
||||
private Long totalItems;
|
||||
private Integer totalPages;
|
||||
private List<T> list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证和调整分页参数
|
||||
*
|
||||
* @param page 页码
|
||||
* @param limit 每页数量
|
||||
* @return 调整后的分页参数数组 [page, limit]
|
||||
*/
|
||||
public static Integer[] validatePageParams(Integer page, Integer limit) {
|
||||
// 验证页码
|
||||
if (page == null || page < 1) {
|
||||
page = DEFAULT_PAGE;
|
||||
}
|
||||
|
||||
// 验证每页数量
|
||||
if (limit == null || limit < 1 || limit > MAX_LIMIT) {
|
||||
limit = DEFAULT_LIMIT;
|
||||
}
|
||||
|
||||
return new Integer[] { page, limit };
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Page对象
|
||||
*
|
||||
* @param page 页码
|
||||
* @param limit 每页数量
|
||||
* @return Page对象
|
||||
*/
|
||||
public static <T> Page<T> createPage(Integer page, Integer limit) {
|
||||
Integer[] params = validatePageParams(page, limit);
|
||||
return new Page<>(params[0], params[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将MyBatis Plus的Page结果转换为分页结果
|
||||
*
|
||||
* @param pageResult MyBatis Plus的Page结果
|
||||
* @return 分页结果
|
||||
*/
|
||||
public static <T> PaginationResult<T> createPaginationResult(IPage<T> pageResult) {
|
||||
return new PaginationResult<>(
|
||||
(int) pageResult.getCurrent(),
|
||||
(int) pageResult.getSize(),
|
||||
pageResult.getTotal(),
|
||||
(int) pageResult.getPages(),
|
||||
pageResult.getRecords());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建空的分页结果
|
||||
*
|
||||
* @param page 页码
|
||||
* @param limit 每页数量
|
||||
* @return 空的分页结果
|
||||
*/
|
||||
public static <T> PaginationResult<T> createEmptyPaginationResult(Integer page, Integer limit) {
|
||||
Integer[] params = validatePageParams(page, limit);
|
||||
return new PaginationResult<>(
|
||||
params[0],
|
||||
params[1],
|
||||
0L,
|
||||
0,
|
||||
List.of());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user