728x90
반응형
1. Config 파일 생성
@Configuration
@EnableJpaAuditing(auditorAwareRef = "commonAuditorAware")
public class JpaAuditingConfiguration {
@Bean
public AuditorAware<String> commonAuditorAware() {
/*
if you are using spring security, you can get the currently logged username with following code segment.
SecurityContextHolder.getContext().getAuthentication().getName()
*/
return new CommonAuditorAware();
}
}
2. AuditorAware 생성
AuditorAware를 상속받는 commonAuditorAware 클래스 생성
public class CommonAuditorAware implements AuditorAware<String> {
@Autowired
UserServiceImpl userService;
@Override
public Optional<String> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (null == authentication || !authentication.isAuthenticated()) {
return Optional.empty();
}
User user = (User) authentication.getPrincipal();
return Optional.of(user.getUserId());
}
}
3. Entity 에 Listener 추가
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class AuditorCreateEntity<String>
{
@CreatedBy
private String createdBy;
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private Date lastModifiedDate;
}
728x90
반응형
'Web 개발 > Java, SpringBoot, JPA' 카테고리의 다른 글
[JPA]Error creating bean with name 'springSecurityFilterChain' defined in class path resource (0) | 2021.07.01 |
---|---|
[JPA] 접속중인 사용자에 대한 정보 추가 (0) | 2021.06.30 |
[jQuery] val(), html() 차이 (0) | 2021.06.29 |
[JPA/Thymeleaf] An error happened during template parsing (0) | 2021.06.27 |
[JS] 파일 첨부 여부 체크 (0) | 2021.06.22 |