Hahah
Hahah
config;
import com.hoidanit.springrestfulapijobhunter.util.SecurityUtil;
import com.nimbusds.jose.jwk.source.ImmutableSecret;
import com.nimbusds.jose.util.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.jwt.*;
import
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticati
onConverter;
import
org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuth
oritiesConverter;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@Configuration
public class JWTTokenConfiguration {
@Value("${hoidanit.jwt.base64-secret}")
private String jwtKey;
@Bean
public JwtEncoder jwtEncoder() {
return new NimbusJwtEncoder(new ImmutableSecret<>(getSecretKey()));
}
@Bean
public JwtDecoder jwtDecoder() {
NimbusJwtDecoder jwtDecoder =
NimbusJwtDecoder.withSecretKey(getSecretKey()).macAlgorithm(SecurityUtil.JWT_A
LGORITHM).build();
return token -> {
try {
return jwtDecoder.decode(token);
} catch (JwtException e) {
System.err.println(">>> JWT decoding error: " + e.getMessage());
throw e;
}
};
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new
JwtGrantedAuthoritiesConverter();
grantedAuthoritiesConverter.setAuthorityPrefix("");
grantedAuthoritiesConverter.setAuthoritiesClaimName("permissions");
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesC
onverter);
return jwtAuthenticationConverter;
}
}