refactor: 优化代码结构和配置

- 在PageDto中添加toString方法以便调试
- 关闭开发环境的SQL日志输出以减少日志噪音
- 允许匿名用户发表评论
- 调整安全配置,仅允许POST方法访问认证端点
- 修改文章状态分页接口,使用查询参数替代路径参数
This commit is contained in:
qingfeng1121
2025-12-15 17:54:14 +08:00
parent 9b01ee8889
commit f27be2130c
5 changed files with 17 additions and 6 deletions

View File

@@ -62,7 +62,7 @@ public class SecurityConfig {
.authorizeRequests() .authorizeRequests()
// 允许公开访问的路径 // 允许公开访问的路径
// 登录和认证相关端点应该全部公开 // 登录和认证相关端点应该全部公开
.antMatchers("/api/auth/**").permitAll() .antMatchers(HttpMethod.POST,"/api/auth/**").permitAll()
// 文章浏览量增加接口公开 // 文章浏览量增加接口公开
.antMatchers(HttpMethod.POST,"/api/articles/view/**").permitAll() .antMatchers(HttpMethod.POST,"/api/articles/view/**").permitAll()
// 所有GET请求公开 // 所有GET请求公开

View File

@@ -63,8 +63,11 @@ public class ArticleController {
* @param size 每页大小可选默认为10最大为100 * @param size 每页大小可选默认为10最大为100
* @return 返回包含分页文章列表的ResponseMessage对象 * @return 返回包含分页文章列表的ResponseMessage对象
*/ */
@GetMapping("/status/page/{status}") // api/articles/status/page?status=1&page=1&size=2
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(@PathVariable PageDto pageDto) { // get 只能这样不能传递json
@GetMapping("/status/page")
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(PageDto pageDto) {
System.out.println(pageDto);
return articleService.getArticlesByStatusWithPagination(pageDto.getStatus(), pageDto.getPage(), pageDto.getSize()); return articleService.getArticlesByStatusWithPagination(pageDto.getStatus(), pageDto.getPage(), pageDto.getSize());
} }

View File

@@ -88,7 +88,7 @@ public class MessageController {
* 创建新消息 * 创建新消息
*/ */
@PostMapping @PostMapping
@PreAuthorize("isAuthenticated()") // 允许匿名用户发表评论
public ResponseMessage<Message> createMessage(@RequestBody MessageDto message) { public ResponseMessage<Message> createMessage(@RequestBody MessageDto message) {
logger.info("接收创建消息的请求: {}", message != null ? message.getNickname() : "null"); logger.info("接收创建消息的请求: {}", message != null ? message.getNickname() : "null");
return messageService.saveMessage(message); return messageService.saveMessage(message);

View File

@@ -34,4 +34,12 @@ public class PageDto {
public void setSize(Integer size) { public void setSize(Integer size) {
this.size = size; this.size = size;
} }
@Override
public String toString() {
return "PageDto{" +
"status=" + status +
", page=" + page +
", size=" + size +
'}';
}
} }

View File

@@ -21,8 +21,8 @@ spring.datasource.hikari.minimum-idle=2
# JPA配置开发环境使用update以便自动创建/更新表结构) # JPA配置开发环境使用update以便自动创建/更新表结构)
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
# ==================================================================== # ====================================================================