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