Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package shop.minostreet.shoppingmall.handler.aop;

import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import shop.minostreet.shoppingmall.config.auth.LoginUser;
import shop.minostreet.shoppingmall.domain.ErrorLog;
import shop.minostreet.shoppingmall.domain.User;
import shop.minostreet.shoppingmall.handler.exception.MyValidationException;
import shop.minostreet.shoppingmall.repository.ErrorLogRepository;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
@Aspect
//Aspect = PointCut + Advice
@Component
public class MyErrorLogAdvice {
private final Logger log = LoggerFactory.getLogger(getClass());
private final HttpSession session;

private final ErrorLogRepository errorLogRepository;
@Pointcut("@annotation(shop.minostreet.shoppingmall.handler.annotation.MyErrorLogRecord)")
public void myErrorLog(){}

@Before("myErrorLog()")
public void errorLogAdvice(JoinPoint jp) throws HttpMessageNotReadableException {
log.debug("디버그 : errorLogAdvice 호출됨");
Object[] args = jp.getArgs();

for (Object arg : args) {
//매개변수를 돌면서 Exception이 존재하는지 체크한다.
//: Exception의 자식까지 모두 확인
if(arg instanceof Exception){
Exception e = (Exception) arg;

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
User userDetails = (User) authentication.getPrincipal();
LoginUser loginUser = new LoginUser(userDetails); // UserDetails 객체를 LoginUser 객체로 변환합니다.

ErrorLog errorLog = ErrorLog.builder()
.userId(loginUser.getUser().getId())
.msg(e.getMessage())
.build();
errorLogRepository.save(errorLog);
}

// Authentication authentication=(Authentication) SecurityContextHolder.getContext().getAuthentication();
// if(authentication != null){
// LoginUser loginUser = (LoginUser)authentication.getPrincipal();
//
//// LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
//
// ErrorLog errorLog =ErrorLog.builder().userId(loginUser.getUser().getId()).msg(e.getMessage()).build();
// //에러 로그의 아이디, 에러 로그 메시지를 전달해 객체 생성
// errorLogRepository.save(errorLog);
// }
}
}
}
}
/**
* 유효성 검사
* get, delete, post, put에서 body가 존재하는 post, put만 존재
*/
52 changes: 0 additions & 52 deletions src/main/java/shop/mtcoding/restend/core/advice/MyLogAdvice.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package shop.mtcoding.restend.core.annotation;

package shop.minostreet.shoppingmall.handler.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyErrorLog {
}
public @interface MyErrorLogRecord {
}
11 changes: 0 additions & 11 deletions src/main/java/shop/mtcoding/restend/core/annotation/MyLog.java

This file was deleted.

41 changes: 41 additions & 0 deletions src/main/java/shop/mtcoding/restend/model/log/ErrorLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package shop.minostreet.shoppingmall.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
//Audit 기능 사용하기 위한 어노테이션 1
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@Getter
@Table(name = "error_log_tb")
@Entity
public class ErrorLog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String msg;
private Long userId;

@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt;

@LastModifiedDate
@Column(nullable = false)
private LocalDateTime updatedAt;

@Builder
public ErrorLog(Long id, String msg, Long userId, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.msg = msg;
this.userId = userId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package shop.minostreet.shoppingmall.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import shop.minostreet.shoppingmall.domain.ErrorLog;

public interface ErrorLogRepository extends JpaRepository<ErrorLog, Long> {
}
41 changes: 41 additions & 0 deletions src/main/java/shop/mtcoding/restend/model/log/LoginLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package shop.minostreet.shoppingmall.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;

//Audit 기능 사용하기 위한 어노테이션 1
@EntityListeners(AuditingEntityListener.class)
//Spring이 User 객체 생성시 빈생성자로 생성하기 때문에
@NoArgsConstructor
//@Setter // DTO 만들면 삭제해야됨
@Getter
@Table(name = "login_log_tb")
@Entity
public class LoginLog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private String userAgent;
private String clientIP;

@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt;


@Builder
public LoginLog(Long id, Long userId, String userAgent, String clientIP, LocalDateTime createdAt) {
this.id = id;
this.userId = userId;
this.userAgent = userAgent;
this.clientIP = clientIP;
this.createdAt = createdAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package shop.minostreet.shoppingmall.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import shop.minostreet.shoppingmall.domain.LoginLog;

public interface LoginLogRepository extends JpaRepository<LoginLog, Long> {
}