JwtDecoder
@Configuration
@Slf4j
public class JwtDecoderConfig {
ย @NotBlank
ย @Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
ย private String ย issuerUri;
ย @NotBlank
ย @Value("${keycloak.truststore}")
ย private Resource ย trustStore;
ย @NotBlank
ย @Value("${keycloak.truststore-password}")
ย private String ย trustStorePw;
ย @Bean
ย public JwtDecoder jwtDecoder() {
ย ย return ย NimbusJwtDecoder
ย ย ย .withIssuerLocation(issuerUri)
ย ย ย .restOperations(restTemplate())
ย ย ย .build();
ย }
ย private RestTemplate ย restTemplate() {
ย ย log.info("RestTemplate for issuer-uri");
ย ย SSLContext sslContext = null;
ย ย try {
ย ย ย sslContext = new SSLContextBuilder()
ย ย ย ย .loadTrustMaterial(trustStore.getURL(), trustStorePw.toCharArray())
ย ย ย ย .build();
ย ย } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
ย ย ย ย | IOException e) {
ย ย ย log.error("ERROR while SSLContext building.", e);
ย ย ย return ย null;
ย ย }
ย ย final TlsSocketStrategy tlsStrategy = new ConscryptClientTlsStrategy(
ย ย ย ย sslContext, NoopHostnameVerifier.INSTANCE);
ย ย final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
ย ย ย ย .setTlsSocketStrategy(tlsStrategy)
ย ย ย ย .setDefaultTlsConfig(TlsConfig.custom()
ย ย ย ย ย ย .setHandshakeTimeout(Timeout.ofSeconds(30))
ย ย ย ย ย ย .setSupportedProtocols(TLS.V_1_3)
ย ย ย ย ย ย .build())
ย ย ย ย .build();
ย ย final CloseableHttpClient httpClient = HttpClients.custom()
ย ย ย ย .setConnectionManager(cm)
ย ย ย ย .build();
ย ย final var requestFactory = new HttpComponentsClientHttpRequestFactory();
ย ย requestFactory.setHttpClient(httpClient);
ย ย return ย new RestTemplate(requestFactory);
ย }
}
















