Skip to content

SecurityUtils.getCurrentUserJWT() does not work #32399

@gbrdead

Description

@gbrdead

When using JWT authentication, SecurityContextHolder.getContext().getAuthentication().getCredentials() does not return a String but an instance of Jwt.

Overview of the issue

SecurityUtils.getCurrentUserJWT() always returns an empty Optional.

Suggest a Fix
    public static Optional<String> getCurrentUserJWT() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        return Optional.ofNullable(securityContext.getAuthentication())
            .filter(authentication -> authentication.getCredentials() instanceof Jwt)
            .map(authentication -> ((Jwt) authentication.getCredentials()).getTokenValue());
    }
JHipster Version(s)

Latest from main.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions