Refactor auditore aware
Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
@@ -14,12 +14,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auditor class that allows {@link BaseEntity}s to insert currenlt logged in
|
* Auditor class that allows BaseEntitys to insert current logged in user for
|
||||||
* user for repository changes.
|
* repository changes.
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SpringSecurityAuditorAware implements AuditorAware<String> {
|
public class SpringSecurityAuditorAware implements AuditorAware<String> {
|
||||||
@@ -29,16 +25,21 @@ public class SpringSecurityAuditorAware implements AuditorAware<String> {
|
|||||||
|
|
||||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
if (authentication == null || !authentication.isAuthenticated()) {
|
if (isAuthentifactionInvalid(authentication)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authentication.getPrincipal() != null) {
|
return getCurrentAuditor(authentication);
|
||||||
if (authentication.getPrincipal() instanceof UserDetails) {
|
}
|
||||||
return ((UserDetails) authentication.getPrincipal()).getUsername();
|
|
||||||
}
|
private String getCurrentAuditor(final Authentication authentication) {
|
||||||
return authentication.getPrincipal().toString();
|
if (authentication.getPrincipal() instanceof UserDetails) {
|
||||||
|
return ((UserDetails) authentication.getPrincipal()).getUsername();
|
||||||
}
|
}
|
||||||
return null;
|
return authentication.getPrincipal().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isAuthentifactionInvalid(final Authentication authentication) {
|
||||||
|
return authentication == null || !authentication.isAuthenticated() || authentication.getPrincipal() == null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user