Refactor auditore aware

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-06-09 17:31:34 +02:00
parent c3f0f6e0d6
commit ed8827726c

View File

@@ -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;
} }
} }