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

com.fivefaces.structureclient.config.security.user.UserJwtTokenServiceImpl Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy