Compare commits
6 Commits
e4a11a8a53
...
fdb9dc6daf
| Author | SHA1 | Date | |
|---|---|---|---|
| fdb9dc6daf | |||
| 9d6ab53f5f | |||
| a58da98dd1 | |||
| a38fd92801 | |||
| df34d7cb28 | |||
| a4713648a2 |
@ -1,5 +1,6 @@
|
||||
package org.cmh.backend.NewsManagement.controller;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.cmh.backend.NewsManagement.dto.MessageResponse;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
@ -48,10 +49,15 @@ public class NewsController {
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
||||
}
|
||||
//
|
||||
// @DeleteMapping("/{id}")
|
||||
// public ResponseEntity<String> deleteNews(@PathVariable Long id) {
|
||||
// // TODO: 实现删除资讯的逻辑
|
||||
// return new ResponseEntity<>("删除成功", HttpStatus.OK);
|
||||
// }
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> deleteNews(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
newsService.deleteNews(id);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败,文章不存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,21 +5,6 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface NewsRepository extends JpaRepository<News, Long> {
|
||||
News findByTitle(String title);
|
||||
|
||||
News findBySummary(String summary);
|
||||
|
||||
News findByAuthor(String author);
|
||||
|
||||
News findByImagePath(String imagePath);
|
||||
|
||||
News findByContent(String content);
|
||||
|
||||
News findByTitleAndSummaryAndAuthorAndImagePath(String title, String summary, String author, String imagePath);
|
||||
|
||||
Page<News> findAllByOrderByIdDesc(Pageable pageable);
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package org.cmh.backend.NewsManagement.service;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
import org.cmh.backend.NewsManagement.repository.NewsRepository;
|
||||
@ -46,4 +47,11 @@ public class NewsService {
|
||||
newsRepository.save(news);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteNews(Long id) {
|
||||
if (!newsRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException("News with id " + id + " not found.");
|
||||
}
|
||||
newsRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,9 +2,15 @@ package org.cmh.backend.Utils;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ -12,4 +18,17 @@ public class GlobalExceptionHandler {
|
||||
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||
public ResponseEntity<Map<String, String>> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) {
|
||||
HashMap<String, String> response = new HashMap<>();
|
||||
response.put("error", ex.getMessage());
|
||||
response.put("stackTrace", Arrays.toString(ex.getStackTrace()));
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public ResponseEntity<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
|
||||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
@ -15,11 +15,19 @@ public class JwtVerifyAspect {
|
||||
if (arg instanceof JwtRequest jwtRequest) {
|
||||
String token = jwtRequest.getToken();
|
||||
if (!JwtUtil.isTokenValid(token)) {
|
||||
throw new JwtValidationException("JWT token is invalid");
|
||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||
}
|
||||
return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数
|
||||
}
|
||||
// JWTRequest对象优先,否则再检查其他字符串参数
|
||||
if (arg instanceof String token){
|
||||
if (JwtUtil.isTokenValid(token)){
|
||||
// 验证成功就直接退出。
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user