com.fivefaces.structureclient.config.security.user.UserJwtTokenServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-structure-client Show documentation
Show all versions of common-structure-client Show documentation
structure Client for Five Faces
package com.fivefaces.structureclient.config.security.user;
import com.fivefaces.structureclient.config.security.SecurityProperties;
import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class UserJwtTokenServiceImpl implements UserJwtTokenService {
private final JwtDecoder decoder;
public UserJwtTokenServiceImpl(SecurityProperties securityProperties) {
this.decoder = decoder(securityProperties);
}
@Override
public JWT validateToken(String token) {
try {
decoder.decode(token);
return JWTParser.parse(token);
} catch (Exception e) {
log.error("Could not validate JWT token", e);
throw new BadCredentialsException("Invalid JWT", e);
}
}
private JwtDecoder decoder(SecurityProperties securityProperties) {
OAuth2TokenValidator jwtValidator = JwtValidators.createDefaultWithIssuer(securityProperties.getUserIssuerUri());
NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(securityProperties.getUserJwkSetUri()).build();
jwtDecoder.setJwtValidator(jwtValidator);
return jwtDecoder;
}
}