프론트 로그인 이력 쌓기 추가
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package app.module.front.config.application.security;
|
||||||
|
|
||||||
|
import app.module.core.config.application.enums.AppType;
|
||||||
|
import app.module.core.config.application.util.CommonUtil;
|
||||||
|
import app.module.core.usecase.loggedinuser.save.ports.LoggedInUserSavePortIn;
|
||||||
|
import app.module.core.usecase.loggedinuser.save.ports.application.domain.LoggedInUserIdSaveCmd;
|
||||||
|
import app.module.core.usecase.loggedinuser.save.ports.application.domain.LoggedInUserSaveCmd;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
|
||||||
|
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class FrontAuthSuccessEvent {
|
||||||
|
|
||||||
|
private final LoggedInUserSavePortIn loggedInUserSavePortIn;
|
||||||
|
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
public void handleAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
|
||||||
|
Authentication authentication = event.getAuthentication();
|
||||||
|
FrontHouseholdUser householdUser = (FrontHouseholdUser) authentication.getPrincipal();
|
||||||
|
|
||||||
|
HttpServletRequest request =
|
||||||
|
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||||
|
// 로그인 이력 쌓기
|
||||||
|
loggedInUserSavePortIn.connect(
|
||||||
|
LoggedInUserSaveCmd.builder()
|
||||||
|
.loggedInUserId(
|
||||||
|
LoggedInUserIdSaveCmd.builder()
|
||||||
|
.appType(AppType.FRONT)
|
||||||
|
.id(authentication.getName())
|
||||||
|
.build())
|
||||||
|
.clientIP(CommonUtil.getClientIP(request))
|
||||||
|
.lastLoggedInAt(LocalDateTime.now())
|
||||||
|
.build());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
public void handleAuthenticationFailureEvent(AbstractAuthenticationFailureEvent event) {
|
||||||
|
Exception e = event.getException();
|
||||||
|
Authentication authentication = event.getAuthentication();
|
||||||
|
log.warn("Unsuccessful authentication result: {}", authentication, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user