rest controller practice : login log (aop)
This commit is contained in:
@@ -2,7 +2,9 @@ package com.example.restcontroller;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@EnableAspectJAutoProxy
|
||||
@SpringBootApplication
|
||||
public class RestControllerApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.restcontroller.common.aop;
|
||||
|
||||
import com.example.restcontroller.logs.service.LogService;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import com.example.restcontroller.user.model.UserLogin;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@Aspect
|
||||
@RequiredArgsConstructor
|
||||
public class LoginLogger {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
@Around("execution(* com.example.restcontroller..*.*Service*.*(..))")
|
||||
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
|
||||
log.info("############################################################");
|
||||
log.info("서비스 호출 전!");
|
||||
log.info("############################################################");
|
||||
|
||||
Object result = joinPoint.proceed();
|
||||
|
||||
if ("login".equals(joinPoint.getSignature().getName())) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\n")
|
||||
.append("함수명 : ")
|
||||
.append(joinPoint.getSignature().getDeclaringType())
|
||||
.append(".")
|
||||
.append(joinPoint.getSignature().getName())
|
||||
.append("\n")
|
||||
.append("매개변수 : ");
|
||||
|
||||
Object[] args = joinPoint.getArgs();
|
||||
Arrays.stream(args).forEach((v) -> {
|
||||
if (v instanceof UserLogin) {
|
||||
sb.append(v.toString())
|
||||
.append("\n")
|
||||
.append("리턴값 : ")
|
||||
.append(result.toString());
|
||||
}
|
||||
});
|
||||
|
||||
logService.add(sb.toString());
|
||||
log.info(sb.toString());
|
||||
}
|
||||
|
||||
log.info("############################################################");
|
||||
log.info("서비스 호출 후!");
|
||||
log.info("############################################################");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.restcontroller.logs.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Entity
|
||||
public class Logs {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Lob
|
||||
private String text;
|
||||
|
||||
private LocalDateTime regDate;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.restcontroller.logs.repository;
|
||||
|
||||
import com.example.restcontroller.logs.entity.Logs;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface LogsRepository extends JpaRepository<Logs, Long> {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.restcontroller.logs.service;
|
||||
|
||||
public interface LogService {
|
||||
|
||||
void add(String text);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.restcontroller.logs.service;
|
||||
|
||||
import com.example.restcontroller.logs.entity.Logs;
|
||||
import com.example.restcontroller.logs.repository.LogsRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class LogServiceImpl implements LogService{
|
||||
|
||||
private final LogsRepository logsRepository;
|
||||
|
||||
@Override
|
||||
public void add(String text) {
|
||||
|
||||
logsRepository.save(Logs.builder()
|
||||
.text(text)
|
||||
.regDate(LocalDateTime.now())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,6 @@ public class ApiLoginController {
|
||||
if (bindingResult.hasFieldErrors()) {
|
||||
return ResponseResult.fail("입력값이 정확하지 않습니다.", ResponseError.of(bindingResult.getFieldErrors()));
|
||||
}
|
||||
|
||||
User user = userService.login(userLogin);
|
||||
|
||||
UserLoginToken userLoginToken = JWTUtils.createToken(user)
|
||||
|
||||
@@ -112,4 +112,18 @@ public class User {
|
||||
return status.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", email='" + email + '\'' +
|
||||
", userName='" + userName + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", phone='" + phone + '\'' +
|
||||
", status=" + status +
|
||||
", lockYn=" + lockYn +
|
||||
", regDate=" + regDate +
|
||||
", updateDate=" + updateDate +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.example.restcontroller.user.service;
|
||||
|
||||
import com.example.restcontroller.board.model.ServiceResult;
|
||||
import com.example.restcontroller.common.exception.BizException;
|
||||
import com.example.restcontroller.logs.service.LogService;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import com.example.restcontroller.user.entity.UserInterest;
|
||||
import com.example.restcontroller.user.entity.UserStatus;
|
||||
|
||||
@@ -23,7 +23,7 @@ spring:
|
||||
|
||||
jackson:
|
||||
serialization:
|
||||
fail-on-empty-beans: false
|
||||
fail-on-empty-beans: true
|
||||
|
||||
mvc:
|
||||
hiddenmethod:
|
||||
|
||||
Reference in New Issue
Block a user