From f147f496c369b8c34db22a2bf12f77d745d159a4 Mon Sep 17 00:00:00 2001 From: parkduhwan Date: Sat, 31 Aug 2024 01:30:17 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B0=9C=20=EC=88=9C=ED=99=98=EC=B0=B8?= =?UTF-8?q?=EC=A1=B0=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/jwt/JwtAuthenticationFilter.java | 9 +++++++- .../config/security/SecurityConfig.java | 3 ++- .../controller/HeartController.java | 21 ----------------- .../healthylife/entity/TodayEntity.java | 23 ++++--------------- .../healthylife/service/HeartService.java | 4 ++-- 5 files changed, 16 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/example/healthylife/config/jwt/JwtAuthenticationFilter.java b/src/main/java/com/example/healthylife/config/jwt/JwtAuthenticationFilter.java index 8215cc3..27352a6 100644 --- a/src/main/java/com/example/healthylife/config/jwt/JwtAuthenticationFilter.java +++ b/src/main/java/com/example/healthylife/config/jwt/JwtAuthenticationFilter.java @@ -28,8 +28,15 @@ public class JwtAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + String path = httpRequest.getRequestURI(); + // Swagger UI와 같은 공개된 엔드포인트는 필터링하지 않음 + if (path.startsWith("/swagger-ui/") || path.startsWith("/v3/api-docs/") || path.startsWith("/swagger-resources/") || path.startsWith("/webjars/")) { + chain.doFilter(request, response); + return; + } try { - restoreAuthentication((HttpServletRequest) request, (HttpServletResponse) response); + restoreAuthentication(httpRequest, (HttpServletResponse) response); chain.doFilter(request, response); } catch (AuthenticationException e) { ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); diff --git a/src/main/java/com/example/healthylife/config/security/SecurityConfig.java b/src/main/java/com/example/healthylife/config/security/SecurityConfig.java index 39d245b..d72d232 100644 --- a/src/main/java/com/example/healthylife/config/security/SecurityConfig.java +++ b/src/main/java/com/example/healthylife/config/security/SecurityConfig.java @@ -31,12 +31,13 @@ protected void configure(HttpSecurity http) throws Exception { .cors().configurationSource(corsConfigurationSource()) // CORS 설정 추가 .and() .authorizeRequests() + .antMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**", "/webjars/**").permitAll() .antMatchers("/user/one","/user/delete","/user/update").authenticated() .antMatchers("/community/register", "/community/update", "/community/delete/**","/community/recommend/**","/community/myCommunityContents").authenticated() .antMatchers("/communityComments/insert", "/communityComments/update", "/communityComments/delete/**").authenticated() .antMatchers("/today/create","/today/myTodayContents","/today/update/**","/today/delete/**","/today/todayDetail/**").authenticated() .antMatchers("/todayComments/register", "/todayComments/update", "/todayComments/delete/**").authenticated() - .antMatchers("/hearts/hasLiked/**", "/hearts/toggle/**").authenticated() + .antMatchers("/hearts/hasLiked/**").authenticated() .anyRequest().permitAll() .and() .addFilterBefore(new JwtAuthenticationFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class); diff --git a/src/main/java/com/example/healthylife/controller/HeartController.java b/src/main/java/com/example/healthylife/controller/HeartController.java index 96434a7..c3ba68b 100644 --- a/src/main/java/com/example/healthylife/controller/HeartController.java +++ b/src/main/java/com/example/healthylife/controller/HeartController.java @@ -3,7 +3,6 @@ import com.example.healthylife.entity.UserEntity; import com.example.healthylife.repository.UserRepository; import com.example.healthylife.service.HeartService; -import com.example.healthylife.service.TodayService; import com.example.healthylife.service.UserService; import com.example.healthylife.config.jwt.JwtUtil; import io.swagger.annotations.ApiOperation; @@ -22,29 +21,9 @@ public class HeartController { private final HeartService heartService; private final UserService userService; - private final TodayService todayService; private final JwtUtil jwtUtil; private final UserRepository userRepository; -// @ApiOperation(value = "오늘의 글 좋아요 토글") -// @PostMapping("/toggle/{todaySq}") -// public ResponseEntity toggleLike(@PathVariable("todaySq") Long todaySq, -// @RequestHeader("Authorization") String authorizationHeader) { -// String jwtToken = jwtUtil.extractTokenFromHeader(authorizationHeader); -// if (jwtToken == null || !jwtUtil.validateToken(jwtToken)) { -// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); -// } -// -// String username = jwtUtil.getUserId(jwtToken); -// UserEntity user = userService.findUserById(username) -// .orElseThrow(() -> new RuntimeException("유저가 없습니다.")); -// -// TodayEntity today = todayService.findbytodaysq(todaySq) -// .orElseThrow(() -> new RuntimeException("오늘의 글이 없습니다.")); -// -// heartService.toggleLike(user, today); -// return ResponseEntity.ok().build(); -// } @ApiOperation(value = "오늘의 글 좋아요 토글") @PostMapping("/toggle/{todaySq}") diff --git a/src/main/java/com/example/healthylife/entity/TodayEntity.java b/src/main/java/com/example/healthylife/entity/TodayEntity.java index 52c00b2..b470b65 100644 --- a/src/main/java/com/example/healthylife/entity/TodayEntity.java +++ b/src/main/java/com/example/healthylife/entity/TodayEntity.java @@ -8,7 +8,6 @@ import java.util.Date; import java.util.List; -@ToString @Entity @Getter @Setter @@ -18,44 +17,32 @@ public class TodayEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "today_sq", unique = true,nullable = false) - //오운완 시퀀스 + @Column(name = "today_sq", unique = true, nullable = false) private long todaySq; - //오운완 게시물 내용 - @Column(name = "today_contents",length = 500) + @Column(name = "today_contents", length = 500) private String todayContents; - //오운완 좋아요 - //today_heart @Column(name = "today_hearts", length = 150) private long todayHearts; - //오운완 게시물 작성 날짜 - //today_created - @Column(name = "today_created",length = 150) + @Column(name = "today_created", length = 150) @Temporal(TemporalType.TIMESTAMP) private Date todayCreated; - //작성자 @ManyToOne @JoinColumn(name = "user_sq") private UserEntity user; - //댓글 @JsonManagedReference @OneToMany(mappedBy = "todayEntity", cascade = CascadeType.ALL, orphanRemoval = true) private List comments; - //이미지 @Column(name = "image_url") private String imageurl; - // builder @Builder(toBuilder = true) - public TodayEntity(long todaySq, String todayContents, - long todayHearts, Date todayCreated, - UserEntity user, String imageurl){ + public TodayEntity(long todaySq, String todayContents, long todayHearts, Date todayCreated, UserEntity user, String imageurl) { this.todaySq = todaySq; this.todayContents = todayContents; this.todayHearts = todayHearts; @@ -64,12 +51,10 @@ public TodayEntity(long todaySq, String todayContents, this.imageurl = imageurl; } - // 좋아요 수 증가 public void incrementLikeCount() { this.todayHearts++; } - // 좋아요 수 감소 public void decrementLikeCount() { if (this.todayHearts > 0) { this.todayHearts--; diff --git a/src/main/java/com/example/healthylife/service/HeartService.java b/src/main/java/com/example/healthylife/service/HeartService.java index 049c5cd..fdf95f9 100644 --- a/src/main/java/com/example/healthylife/service/HeartService.java +++ b/src/main/java/com/example/healthylife/service/HeartService.java @@ -35,11 +35,11 @@ public long toggleLike(Long userSq, Long todaySq) { today.decrementLikeCount(); } else { HeartEntity heart = new HeartEntity(today,user); - heartRepository.saveAndFlush(heart); // 즉시 저장 시도 + heartRepository.save(heart); today.incrementLikeCount(); } - todayRepository.saveAndFlush(today); // 즉시 저장 시도 + todayRepository.save(today); return today.getTodayHearts(); }