Skip to content

Commit

Permalink
찜 순환참조 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
duhwan05 committed Aug 30, 2024
1 parent 4eba7bb commit f147f49
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Void> 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}")
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/com/example/healthylife/entity/TodayEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Date;
import java.util.List;

@ToString
@Entity
@Getter
@Setter
Expand All @@ -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<TodayCommentsEntity> 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;
Expand All @@ -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--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit f147f49

Please sign in to comment.