com.cleverpine.viravaspringhelper.core.ViravaPrincipalProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cp-virava-spring-helper Show documentation
Show all versions of cp-virava-spring-helper Show documentation
Library for Spring Framework projects using Virava
package com.cleverpine.viravaspringhelper.core;
import java.util.Optional;
import org.springframework.security.core.context.SecurityContextHolder;
public abstract class ViravaPrincipalProvider {
private final Class cpiClass;
protected ViravaPrincipalProvider(Class cpiClass) {
this.cpiClass = cpiClass;
}
public abstract CPI provideCustomPrincipalInfo(String username);
public final Optional getAuthentication() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof ViravaAuthenticationToken) {
return Optional.of((ViravaAuthenticationToken) authentication);
}
return Optional.empty();
}
public final CPI getCustomPrincipalInfo() {
var auth = getAuthentication().orElse(null);
if (auth == null) {
return null;
}
var principal = auth.getPrincipal();
if (principal == null || principal.getUsername() == null || principal.getUsername().isEmpty()) {
return null;
}
CPI customInfo = principal.getCustomPrincipalInfo(cpiClass);
if (customInfo == null) {
customInfo = provideCustomPrincipalInfo(principal.getUsername());
principal.setCustomPrincipalInfo(customInfo);
}
return customInfo;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy