Package com.alpaca.security.manager
Class JJwtManager
java.lang.Object
com.alpaca.security.manager.JJwtManager
JJwtManager is a Spring component responsible for managing JSON Web Tokens (JWT) using
asymmetric RSA key pairs for signing and verification.
This class provides methods to:
- Generate signed JWT tokens from
UserPrincipalinstances. - Validate and parse tokens using the configured RSA public key.
- Extract claims and build authentication objects compatible with Spring Security.
Security is enforced using the RS512 algorithm (SignatureAlgorithm) which
leverages RSA with SHA-512 hashing. Keys are injected via application properties and decoded
using the KeyFactory standard mechanism.
Configuration properties required:
app.jwtPrivateKey: Base64-encoded RSA private key in PKCS#8 format.app.jwtPublicKey: Base64-encoded RSA public key in X.509 format.app.jwtUserGenerator: Identifier of the issuer of tokens.app.jwtTimeExpiration: Token expiration time in milliseconds.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final io.jsonwebtoken.security.SignatureAlgorithmThe signature algorithm used for JWT creation: RS512 (RSA + SHA-512).private final StringToken expiration time (in milliseconds), configured via properties.private final StringThe issuer (generator) identifier included in the token payload.private final RSAPrivateKeyRSA private key used to sign tokens.private final RSAPublicKeyRSA public key used to verify tokens. -
Constructor Summary
ConstructorsConstructorDescriptionJJwtManager(@NotNull String jwtPrivateKey, @NotNull String jwtPublicKey, @NotNull String jwtUSerGenerator, @NotNull String jwtTimeExpiration) Constructs aJJwtManagerwith the necessary RSA keys and configuration. -
Method Summary
Modifier and TypeMethodDescriptionauthoritiesToString(Collection<? extends org.springframework.security.core.GrantedAuthority> grantedAuthorities) Converts a collection ofGrantedAuthorityobjects into a comma-separated string.org.springframework.security.authentication.UsernamePasswordAuthenticationTokencreateAuthentication(io.jsonwebtoken.Claims claims) Creates an authentication object if the claims represent a valid token.createToken(UserPrincipal user) Creates a new signed JWT token containing information about aUserPrincipal.booleanexistString(String string) Checks whether a string is non-null and non-blank.getAdvertiserId(io.jsonwebtoken.Claims claims) io.jsonwebtoken.ClaimsgetAllClaims(io.jsonwebtoken.Jws<io.jsonwebtoken.Claims> claims) Extracts the payload (claims) from a signed JWS token.getAuthorities(io.jsonwebtoken.Claims claims) List<? extends org.springframework.security.core.GrantedAuthority> getAuthoritiesList(io.jsonwebtoken.Claims claims) getProfileId(io.jsonwebtoken.Claims claims) <T> TgetSpecificClaim(io.jsonwebtoken.Claims claims, String claimName, Class<T> t) Retrieves a specific claim from JWT claims.getUserId(io.jsonwebtoken.Claims claims) getUsername(io.jsonwebtoken.Claims claims) getUserPrincipal(io.jsonwebtoken.Claims claims) Builds aUserPrincipalfrom JWT claims.getUUIDFromClaim(String claim) Converts a claim string to aUUID.booleanisValidToken(io.jsonwebtoken.Claims claims) Verifies whether the claims represent a valid token.org.springframework.security.authentication.UsernamePasswordAuthenticationTokenmanageAuthentication(String token) Builds a Spring Security authentication object from a JWT token.io.jsonwebtoken.Jws<io.jsonwebtoken.Claims> validateToken(String token) Validates and parses a JWT token using the public key.
-
Field Details
-
alg
private final io.jsonwebtoken.security.SignatureAlgorithm algThe signature algorithm used for JWT creation: RS512 (RSA + SHA-512). -
privateKey
RSA private key used to sign tokens. -
publicKey
RSA public key used to verify tokens. -
jwtUserGenerator
The issuer (generator) identifier included in the token payload. -
jwtTimeExpiration
Token expiration time (in milliseconds), configured via properties.
-
-
Constructor Details
-
JJwtManager
public JJwtManager(@Value("${app.jwtPrivateKey}") @NotNull @NotNull String jwtPrivateKey, @Value("${app.jwtPublicKey}") @NotNull @NotNull String jwtPublicKey, @Value("${app.jwtUserGenerator}") @NotNull @NotNull String jwtUSerGenerator, @Value("${app.jwtTimeExpiration}") @NotNull @NotNull String jwtTimeExpiration) throws Exception Constructs aJJwtManagerwith the necessary RSA keys and configuration.- Parameters:
jwtPrivateKey- Base64-encoded RSA private key (PKCS#8 format)jwtPublicKey- Base64-encoded RSA public key (X.509 format)jwtUSerGenerator- Issuer name to include in JWT tokensjwtTimeExpiration- Expiration time in milliseconds for tokens- Throws:
Exception- if the provided keys cannot be parsed or decoded
-
-
Method Details
-
createToken
Creates a new signed JWT token containing information about aUserPrincipal.- Parameters:
user- the authenticated user- Returns:
- a signed JWT as a String
-
validateToken
Validates and parses a JWT token using the public key.- Parameters:
token- the token to validate- Returns:
- a
Jwscontaining the claims if valid - Throws:
UnauthorizedException- if the token is invalid or expired
-
manageAuthentication
public org.springframework.security.authentication.UsernamePasswordAuthenticationToken manageAuthentication(String token) Builds a Spring Security authentication object from a JWT token.- Parameters:
token- the JWT token- Returns:
- a populated
UsernamePasswordAuthenticationToken
-
createAuthentication
public org.springframework.security.authentication.UsernamePasswordAuthenticationToken createAuthentication(io.jsonwebtoken.Claims claims) Creates an authentication object if the claims represent a valid token.- Parameters:
claims- the JWT claims- Returns:
- a
UsernamePasswordAuthenticationTokenornullif invalid
-
isValidToken
public boolean isValidToken(io.jsonwebtoken.Claims claims) Verifies whether the claims represent a valid token.- Parameters:
claims- the token claims- Returns:
trueif valid, otherwisefalse
-
getUsername
- Returns:
- the username (JWT subject).
-
getAuthorities
- Returns:
- the raw authorities string (comma-separated).
-
getAuthoritiesList
public List<? extends org.springframework.security.core.GrantedAuthority> getAuthoritiesList(io.jsonwebtoken.Claims claims) - Returns:
- the authorities as a
Listof Spring SecurityGrantedAuthority.
-
getUserId
- Returns:
- the userId claim as a String.
-
getProfileId
- Returns:
- the profileId claim as a String.
-
getAdvertiserId
- Returns:
- the advertiserId claim as a String.
-
getUUIDFromClaim
Converts a claim string to aUUID.- Parameters:
claim- the string claim value- Returns:
- a UUID or
nullif blank
-
existString
Checks whether a string is non-null and non-blank.- Parameters:
string- the string to check- Returns:
trueif non-empty, otherwisefalse
-
getUserPrincipal
Builds aUserPrincipalfrom JWT claims.- Parameters:
claims- the token claims- Returns:
- a
UserPrincipalpopulated with claim values
-
getSpecificClaim
Retrieves a specific claim from JWT claims.- Parameters:
claims- the claims objectclaimName- the name of the claimt- the type to cast the claim value- Returns:
- the claim value cast to type
T
-
getAllClaims
public io.jsonwebtoken.Claims getAllClaims(io.jsonwebtoken.Jws<io.jsonwebtoken.Claims> claims) Extracts the payload (claims) from a signed JWS token.- Parameters:
claims- the signed claims wrapper- Returns:
- the
Claimspayload
-
authoritiesToString
public String authoritiesToString(Collection<? extends org.springframework.security.core.GrantedAuthority> grantedAuthorities) Converts a collection ofGrantedAuthorityobjects into a comma-separated string.- Parameters:
grantedAuthorities- the authorities collection- Returns:
- a string representation of authorities
-