All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cleverpine.viravaspringhelper.core.ViravaPrincipalProvider Maven / Gradle / Ivy

There is a newer version: 3.1.7
Show newest version
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