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
UserPrincipal
instances. - 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.SignatureAlgorithm
The signature algorithm used for JWT creation: RS512 (RSA + SHA-512).private final String
Token expiration time (in milliseconds), configured via properties.private final String
The issuer (generator) identifier included in the token payload.private final RSAPrivateKey
RSA private key used to sign tokens.private final RSAPublicKey
RSA public key used to verify tokens. -
Constructor Summary
ConstructorsConstructorDescriptionJJwtManager
(@NotNull String jwtPrivateKey, @NotNull String jwtPublicKey, @NotNull String jwtUSerGenerator, @NotNull String jwtTimeExpiration) Constructs aJJwtManager
with the necessary RSA keys and configuration. -
Method Summary
Modifier and TypeMethodDescriptionauthoritiesToString
(Collection<? extends org.springframework.security.core.GrantedAuthority> grantedAuthorities) Converts a collection ofGrantedAuthority
objects into a comma-separated string.org.springframework.security.authentication.UsernamePasswordAuthenticationToken
createAuthentication
(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
.boolean
existString
(String string) Checks whether a string is non-null and non-blank.getAdvertiserId
(io.jsonwebtoken.Claims claims) io.jsonwebtoken.Claims
getAllClaims
(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> T
getSpecificClaim
(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 aUserPrincipal
from JWT claims.getUUIDFromClaim
(String claim) Converts a claim string to aUUID
.boolean
isValidToken
(io.jsonwebtoken.Claims claims) Verifies whether the claims represent a valid token.org.springframework.security.authentication.UsernamePasswordAuthenticationToken
manageAuthentication
(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 aJJwtManager
with 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
Jws
containing 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
UsernamePasswordAuthenticationToken
ornull
if invalid
-
isValidToken
public boolean isValidToken(io.jsonwebtoken.Claims claims) Verifies whether the claims represent a valid token.- Parameters:
claims
- the token claims- Returns:
true
if 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
List
of 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
null
if blank
-
existString
Checks whether a string is non-null and non-blank.- Parameters:
string
- the string to check- Returns:
true
if non-empty, otherwisefalse
-
getUserPrincipal
Builds aUserPrincipal
from JWT claims.- Parameters:
claims
- the token claims- Returns:
- a
UserPrincipal
populated 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
Claims
payload
-
authoritiesToString
public String authoritiesToString(Collection<? extends org.springframework.security.core.GrantedAuthority> grantedAuthorities) Converts a collection ofGrantedAuthority
objects into a comma-separated string.- Parameters:
grantedAuthorities
- the authorities collection- Returns:
- a string representation of authorities
-