Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 7197736

Browse files
committed
Fix @createdby for AuditRecord
1 parent 6fab241 commit 7197736

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.cloud.dataflow.server.audit.service;
17+
18+
import org.springframework.cloud.common.security.support.SecurityStateBean;
19+
import org.springframework.data.domain.AuditorAware;
20+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
21+
import org.springframework.security.core.Authentication;
22+
import org.springframework.security.core.context.SecurityContextHolder;
23+
24+
/**
25+
*
26+
* @author Gunnar Hillert
27+
*
28+
*/
29+
public class SpringSecurityAuditorAware implements AuditorAware<String> {
30+
31+
private final SecurityStateBean securityStateBean;
32+
33+
public SpringSecurityAuditorAware(SecurityStateBean securityStateBean) {
34+
this.securityStateBean = securityStateBean;
35+
}
36+
37+
@Override
38+
public String getCurrentAuditor() {
39+
final boolean authenticationEnabled = securityStateBean.isAuthenticationEnabled();
40+
if (authenticationEnabled && SecurityContextHolder.getContext() != null) {
41+
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
42+
if (!(authentication instanceof AnonymousAuthenticationToken)) {
43+
return authentication.getName();
44+
}
45+
}
46+
return null;
47+
}
48+
}

spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/DataFlowControllerAutoConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.springframework.cloud.dataflow.server.audit.repository.AuditRecordRepository;
6868
import org.springframework.cloud.dataflow.server.audit.service.AuditRecordService;
6969
import org.springframework.cloud.dataflow.server.audit.service.DefaultAuditRecordService;
70+
import org.springframework.cloud.dataflow.server.audit.service.SpringSecurityAuditorAware;
7071
import org.springframework.cloud.dataflow.server.config.apps.CommonApplicationProperties;
7172
import org.springframework.cloud.dataflow.server.config.features.FeaturesProperties;
7273
import org.springframework.cloud.dataflow.server.controller.AboutController;
@@ -184,6 +185,11 @@ public AuditRecordService auditRecordService(AuditRecordRepository auditRecordRe
184185
return new DefaultAuditRecordService(auditRecordRepository);
185186
}
186187

188+
@Bean
189+
public SpringSecurityAuditorAware springSecurityAuditorAware(SecurityStateBean securityStateBean) {
190+
return new SpringSecurityAuditorAware(securityStateBean);
191+
}
192+
187193
@Bean
188194
@ConditionalOnBean(AuditRecordService.class)
189195
public AuditRecordController auditController(

0 commit comments

Comments
 (0)