|
@@ -1,10 +1,41 @@
|
1
|
1
|
package ZipTeamOrange.config;
|
2
|
2
|
|
3
|
3
|
|
|
4
|
+import ZipTeamOrange.security.UserPrincipal;
|
|
5
|
+import org.springframework.context.annotation.Bean;
|
4
|
6
|
import org.springframework.context.annotation.Configuration;
|
|
7
|
+import org.springframework.data.domain.AuditorAware;
|
5
|
8
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
9
|
+import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
|
10
|
+import org.springframework.security.core.Authentication;
|
|
11
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
12
|
+
|
|
13
|
+import java.util.Optional;
|
6
|
14
|
|
7
|
15
|
@Configuration
|
8
|
16
|
@EnableJpaAuditing
|
9
|
17
|
public class AuditingConfig {
|
|
18
|
+
|
|
19
|
+ @Bean
|
|
20
|
+ public AuditorAware<Long> auditorProvider() {
|
|
21
|
+ return new SpringSecurityAuditAwareImpl();
|
|
22
|
+ }
|
|
23
|
+}
|
|
24
|
+
|
|
25
|
+class SpringSecurityAuditAwareImpl implements AuditorAware<Long> {
|
|
26
|
+
|
|
27
|
+ @Override
|
|
28
|
+ public Optional<Long> getCurrentAuditor() {
|
|
29
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
30
|
+
|
|
31
|
+ if (authentication == null ||
|
|
32
|
+ !authentication.isAuthenticated() ||
|
|
33
|
+ authentication instanceof AnonymousAuthenticationToken) {
|
|
34
|
+ return Optional.empty();
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
|
|
38
|
+
|
|
39
|
+ return Optional.ofNullable(userPrincipal.getId());
|
|
40
|
+ }
|
10
|
41
|
}
|