Extremely Serious

Category: Java (Page 1 of 3)

Mastering Remote Debugging in Java

Remote debugging is a powerful technique that allows you to troubleshoot Java applications running on a different machine than your development environment. This is invaluable for diagnosing issues in applications deployed on servers, containers, or even other developer machines.

Understanding the JPDA Architecture

Java facilitates remote debugging through the Java Platform Debugger Architecture (JPDA). JPDA acts as the bridge between the debugger and the application being debugged (called the debuggee). Here are the key components of JPDA:

  • Java Debug Interface (JDI): This API provides a common language for the debugger to interact with the debuggee's internal state.
  • Java Virtual Machine Tool Interface (JVMTI): This allows the debugger to access information and manipulate the Java Virtual Machine (JVM) itself.
  • Java Debug Wire Protocol (JDWP): This is the communication protocol between the debugger and the debuggee. It defines how they exchange data and control the debugging session.

Configuring the Remote Application

To enable remote debugging, you'll need to configure the application you want to debug. This typically involves setting specific environment variables when launching the application. These variables control aspects like:

  • Transport mode: This specifies the communication channel between the debugger and the application.
  • Port: This defines the port on which the application listens for incoming debug connections. The default port for JDWP is 5005.
  • Suspend on startup: This determines if the application should pause upon launch, waiting for a debugger to connect.

Here's an example command demonstrating how to enable remote debugging using command-line arguments:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 MyApp.jar

Explanation of arguments:

  • -agentlib:jdwp: Instructs the JVM to use the JDWP agent.
  • transport=<transport_value>: Specifies the transport method.
  • server=y: Enables the application to act as a JDWP server, listening for connections.
  • suspend=n: Allows the application to run immediately without waiting for a debugger.
  • address=*:5005: Defines the port number (5005 in this case) for listening.

Remember to replace MyApp.jar with your application's JAR file name.

Possible Values for Transport

The <transport_value> in the -agentlib:jdwp argument can be set to one of the following values, depending on your desired communication method:

  • dt_socket (default): Uses a standard TCP/IP socket connection for communication. This is the most common and widely supported transport mode.
  • shmem: Utilizes shared memory for communication. This option can be faster than sockets on the same machine, but it's limited to local debugging scenarios.
  • nio (Java 1.4 and above): Leverages Non-blocking I/O (NIO) for socket communication. It can offer better performance compared to the regular dt_socket mode in certain situations.
  • ssl (Java 1.7 and above): Enables secure communication using SSL/TLS sockets. This is useful for establishing a secure connection between the debugger and the debuggee.
  • other: JPDA allows for custom transport implementations, but these are less common and may require specific libraries or configurations.

Setting Up Your IDE

Most Integrated Development Environments (IDEs) like Eclipse or IntelliJ IDEA have built-in support for remote debugging Java applications. You'll need to configure a remote debug configuration within your IDE, specifying:

  • Host: The IP address or hostname of the machine where the application is running.
  • Port: The port number you configured in the remote application (default is 5005 if not specified).

Initiating the Debugging Session

Once you've configured both the application and your IDE, you can start the remote debugging session within your IDE. This typically involves launching the debug configuration and waiting for the IDE to connect to the remote application.

Debugging as Usual

After a successful connection, you can leverage the debugger's functionalities like:

  • Setting breakpoints to pause execution at specific points in the code.
  • Stepping through code line by line to examine variable values and program flow.
  • Inspecting variables to view their contents and modifications.

With these tools at your disposal, you can effectively identify and fix issues within your remotely running Java applications.

Demystifying Memory Management: A Look at Java’s Memory Areas

Java's efficient memory management system is a cornerstone of its success. Unlike some programming languages where developers need to manually allocate and release memory, Java utilizes a garbage collector to automatically manage memory usage. This not only simplifies development but also helps prevent memory leaks and crashes.

However, to truly understand Java's memory management, it's crucial to delve into the different memory areas that the Java Virtual Machine (JVM) employs. Here, we'll explore these areas and their functionalities:

Heap Memory: The Dynamic Stage for Objects

Imagine a bustling marketplace where vendors (objects) hawk their wares (data). The heap memory in Java functions similarly. It's a dynamically sized pool where all your program's objects reside during runtime. Every time you create a new object using the new keyword, the JVM allocates space for it in the heap. This space can include the object's fields (variables) and methods (functions).

Key Characteristics of Heap Memory:

  • Dynamic Size: The heap can expand or shrink as needed. As you create more objects, the heap grows to accommodate them. Conversely, when objects are no longer referenced and eligible for garbage collection, the JVM reclaims the memory they occupied.
  • Object Haven: The heap is the exclusive territory for objects. Primitive data types (like int or boolean) are not stored here; they have their own designated memory areas within the JVM.
  • Garbage Collection Central: A core concept in Java, garbage collection automatically identifies and removes unused objects from the heap, preventing memory leaks and optimizing memory usage.

Metaspace: The Repository of Class Blueprints

Think of metaspace as a specialized library within the JVM. It stores essential class metadata, which acts as the blueprint for creating objects. This metadata includes:

  • Bytecode: The compiled instructions for the class methods.
  • Class Names and Field Names: Information about the class itself and its associated fields.
  • Constant Pool Data: Static final variables used by the class.
  • Method Information: Details about the class methods, including their names, parameters, and return types.

Key Characteristics of Metaspace:

  • Dynamic Sizing: Unlike the fixed size of PermGen (metaspace's predecessor in earlier Java versions), metaspace grows automatically as new classes are loaded. This eliminates OutOfMemoryError exceptions that could occur if class metadata couldn't fit in a limited space.
  • Native Memory Resident: Metaspace resides in native memory (provided by the operating system) rather than the managed heap memory of the JVM. This allows for more efficient garbage collection of unused class metadata.
  • Improved Scalability: Due to its dynamic sizing and efficient memory management, metaspace is better suited for applications that utilize a large number of classes.

Stack Memory: The LIFO Stage for Method Calls

The stack memory is a fixed-size area that plays a crucial role in method calls. Whenever a method is invoked, the JVM creates a stack frame on the stack. This frame stores:

  • Local Variables: Variables declared within the method's scope. Primitive data types (like int or boolean) declared as local variables or method arguments within a method are stored in this stack frame.
  • Method Arguments: The values passed to the method when it was called.
  • Return Address: The memory location to return to after the method execution.

Unlike the heap, the stack follows a Last-In-First-Out (LIFO) principle. When a method finishes, its corresponding stack frame is removed, freeing up space for the next method call.

Program Counter (PC Register): Keeping Track of Instruction Flow

This register keeps track of the currently executing instruction within a method. It essentially points to the next instruction to be executed in the current stack frame. The PC register is very small, typically a single register within the CPU.

Native Method Stack: A Stage for Foreign Actors

Java applications can integrate methods written in languages like C/C++. These are known as native methods. The native method stack is a separate stack used specifically for managing information related to native method execution. It functions similarly to the Java stack but manages details specific to native methods.

Conclusion

By understanding these distinct memory areas, you gain a deeper grasp of how Java programs manage and utilize memory resources. Each area plays a vital role:

  • The heap serves as the active workspace for objects.
  • Metaspace acts as the static repository for class definitions.
  • The stack manages method calls and local data.
  • The PC register tracks execution flow within a method.
  • The native method stack handles information specific to native methods.

This knowledge empowers you to write more efficient and memory-conscious Java applications.

Understanding Reference Types in Java: Strong, Soft, Weak, and Phantom

Java's garbage collector (GC) is a crucial mechanism for managing memory and preventing memory leaks. But how does the GC know which objects to keep and which ones can be reclaimed? This is where references come in. There are four main types of references in Java, each influencing the GC's behavior towards the referenced object.

Strong References

The most common type. A strong reference guarantees that the object it points to will not be collected by the GC as long as the reference itself exists.

// Strong Reference
String data = "This data is strongly referenced";

Use case

  • The default for core application logic where objects need to exist until explicitly removed.

Soft References

Soft references suggest to the GC that it's preferable to keep the referenced object around, but not essential. The GC can reclaim the object if memory is tight. This is useful for caches where keeping data in memory is desirable but not critical.

// Soft Reference
SoftReference<Object> softRef = new SoftReference<>(data);

Use case

  • Caching mechanisms. Keeping data in memory for faster access but allowing GC to reclaim it if needed.

Weak References

Even weaker than soft references. The GC can reclaim the object pointed to by a weak reference at any time, regardless of memory pressure. This is useful for transient data associated with objects that may not be around for long.

// Weak Reference
WeakReference<Object> weakRef = new WeakReference<>(data);

Use case

  • Listener objects in UI components. Prevent memory leaks from unused listeners.

Phantom References

The weakest type. They don't prevent the GC from reclaiming the object, but they notify a queue when the object is reclaimed. This allows for custom cleanup actions before the object is removed from memory.

// Phantom Reference (with cleanup logic)
PhantomReference<Object> phantomRef = new PhantomReference<>(data, cleanUpQueue);

Use case

  • Finalizer cleaners. Perform cleanup tasks (like closing files) associated with a garbage-collected object.

Remember

Soft, Weak, and Phantom references require a good understanding of Java's garbage collection. Use them cautiously for specific memory management scenarios.

Understanding Semaphores in Java for Concurrent Programming

In the realm of concurrent programming, managing shared resources among multiple threads is a critical challenge. To address this, synchronization primitives like semaphores play a pivotal role. In Java, the Semaphore class offers a powerful toolset for controlling access to shared resources.

What is a Semaphore?

A semaphore is a synchronization mechanism that regulates access to shared resources by controlling the number of threads that can access them concurrently. It maintains a set of permits, where each thread must acquire a permit before accessing the shared resource. The number of available permits dictates the level of concurrency allowed.

Java's Semaphore Class

In Java, the Semaphore class resides in the java.util.concurrent package and provides methods to acquire and release permits. Let's explore a simple example to grasp the concept:

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2); // Initializes with 2 permits

        Runnable task = () -> {
            try {
                semaphore.acquire(); // Acquire a permit
                // Critical section: access shared resource
                System.out.println(Thread.currentThread().getName() + " is accessing the shared resource.");
                Thread.sleep(2000); // Simulating some work
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                semaphore.release(); // Release the permit
            }
        };

        // Create and start multiple threads
        for (int i = 0; i < 5; i++) {
            new Thread(task).start();
        }
    }
}

In this example, the semaphore with two permits ensures that only two threads can access the shared resource concurrently. The acquire() and release() methods facilitate controlled access to the critical section.

Use Cases for Semaphores

Semaphores are particularly useful in scenarios where limited resources need to be shared among multiple threads. Some common use cases include:

  1. Thread Pool Management: Semaphores can regulate the number of threads active in a pool, preventing resource exhaustion.
  2. Database Connection Pools: Controlling access to a limited number of database connections to avoid overwhelming the system.
  3. Printers and I/O Devices: Managing concurrent access to printers or other I/O devices to prevent conflicts.
  4. Producer-Consumer Problem: Coordinating the interaction between producers and consumers to avoid race conditions.

In conclusion, semaphores in Java provide a robust mechanism for coordinating access to shared resources in a concurrent environment. Understanding their operations and use cases is crucial for building scalable and efficient multi-threaded applications.

Exploring ArrayBlockingQueue in Java

Java provides a variety of concurrent data structures to facilitate communication and synchronization between threads. One such class is ArrayBlockingQueue, which is a blocking queue implementation backed by an array. This queue is particularly useful in scenarios where multiple threads need to exchange data in a producer-consumer fashion.

Initialization

To use ArrayBlockingQueue, start by importing the necessary class:

import java.util.concurrent.ArrayBlockingQueue;

Then, initialize the queue with a specified capacity:

ArrayBlockingQueue<Type> queue = new ArrayBlockingQueue<>(capacity);

Replace Type with the type of elements you want to store, and capacity with the maximum number of elements the queue can hold.

Adding and Removing Elements

Adding Elements

  • put(element): Adds an element to the queue. Blocks if the queue is full.
  • offer(element): Adds an element to the queue if space is available, returns true if successful, false otherwise.
  • offer(element, timeout, timeUnit): Adds an element to the queue, waiting for the specified time if necessary for space to be available.

Removing Elements

  • take(): Removes and returns the head of the queue. Blocks if the queue is empty.
  • poll(): Removes and returns the head of the queue, or returns null if the queue is empty.
  • poll(timeout, timeUnit): Removes and returns the head of the queue, waiting for the specified time if the queue is empty.

Example Usage: Producer-Consumer Scenario

Consider a simple example where a producer thread produces messages, and a consumer thread consumes them using ArrayBlockingQueue:

import java.util.concurrent.ArrayBlockingQueue;

public class ProducerConsumerExample {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(5);

        // Producer thread
        Thread producer = new Thread(() -> {
            try {
                for (int i = 1; i <= 10; i++) {
                    String message = "Message " + i;
                    queue.put(message);
                    System.out.println("Produced: " + message);
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // Consumer thread
        Thread consumer = new Thread(() -> {
            try {
                for (int i = 1; i <= 10; i++) {
                    String message = queue.take();
                    System.out.println("Consumed: " + message);
                    Thread.sleep(1500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        producer.start();
        consumer.start();
    }
}

In this example, the producer and consumer threads interact through the ArrayBlockingQueue, ensuring a smooth exchange of messages while handling blocking situations when the queue is full or empty.

ArrayBlockingQueue serves as a valuable tool in concurrent programming, providing a simple yet effective means of communication and synchronization between threads in Java.

Generating and Validating JWT Tokens in Java using PEM

JSON Web Tokens (JWT) are a popular way to secure communication between parties using digitally signed tokens. In this article, we will explore how to generate and validate JWT tokens in Java using PEM (Privacy Enhanced Mail) files. PEM files are commonly used to store cryptographic keys and certificates.

Generate JWT Token

In this section, we'll walk through how to generate a JWT token in Java using a private key stored in a PEM file.

Setting up the Environment

Before we proceed, make sure you have the following prerequisites:

  1. Java Development Kit (JDK) installed on your system.
  2. A PEM file containing a private key (referred to as <PRIVATE_KEY_PEM>).

Code Implementation

We'll use Java to create a JWT token. Here's the code for generating a JWT token:

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.UUID;

public class GenerateJWTSignedByPEM {

    public static void main(final String ... args) throws Exception {
        // Load the private key and certificate
        final var privateKeyPEM = """
                <PRIVATE_KEY_PEM>              
                """;

        final var privateKey = getPrivateKeyFromPEM(privateKeyPEM);

        // Create JWT claims
        final var subject = "user123";
        final var issuer = "yourapp.com";
        final var expirationTimeMillis = System.currentTimeMillis() + 3600 * 1000; // 1 hour
        final var jwtID = UUID.randomUUID().toString();

        // Build JWT claims
        final var jwtHeader = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
        final var jwtClaims = "{\"sub\":\"" + subject + "\",\"iss\":\"" + issuer + "\",\"exp\":" + expirationTimeMillis + ",\"jti\":\"" + jwtID + "\"}";

        // Base64Url encode the JWT header and claims
        final var base64UrlHeader = base64UrlEncode(jwtHeader.getBytes());
        final var base64UrlClaims = base64UrlEncode(jwtClaims.getBytes());

        // Combine header and claims with a period separator
        final var headerClaims = base64UrlHeader + "." + base64UrlClaims;

        // Sign the JWT
        final var signature = signWithRSA(headerClaims, privateKey);

        // Combine the JWT components
        final var jwtToken = headerClaims + "." + signature;

        System.out.println("JWT Token: " + jwtToken);
    }

    // Helper function to load a PrivateKey from PEM format
    private static PrivateKey getPrivateKeyFromPEM(String privateKeyPEM) throws Exception {
        privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----", "")
                .replace("-----END PRIVATE KEY-----", "")
                .replaceAll("\\s+", "");

        final var privateKeyBytes = Base64.getDecoder().decode(privateKeyPEM);

        final var keyFactory = KeyFactory.getInstance("RSA");
        final var keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        return keyFactory.generatePrivate(keySpec);
    }

    // Base64 URL encoding
    private static String base64UrlEncode(final byte[] data) {
        return Base64.getUrlEncoder().withoutPadding().encodeToString(data);
    }

    // Sign the JWT using RSA
    private static String signWithRSA(final String data, final PrivateKey privateKey) throws Exception {
        final var signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(data.getBytes());
        final var signatureBytes = signature.sign();
        return base64UrlEncode(signatureBytes);
    }
}

Here's a breakdown of the code:

  1. We load the private key from the PEM file.
  2. Create JWT claims, including subject, issuer, expiration time, and a unique JWT ID.
  3. Base64Url encode the JWT header and claims.
  4. Combine the header and claims with a period separator.
  5. Sign the JWT using the private key.
  6. Combine all the JWT components to get the final JWT token.

Make sure to replace <PRIVATE_KEY_PEM> with the actual content of your private key PEM file.

Validate JWT Token

In this section, we'll learn how to validate a JWT token using a public certificate stored in a PEM file.

Setting up the Environment

Ensure you have the following prerequisites:

  1. Java Development Kit (JDK) installed on your system.
  2. A PEM file containing a public certificate (referred to as <PUBLIC_CERT_PEM>).
  3. A JWT token you want to validate (referred to as <JWT_TOKEN>).

Code Implementation

We'll use Java to validate a JWT token. Here's the code:

import java.io.ByteArrayInputStream;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.util.Base64;

public class ValidateJWTSignedByPEM {
    public static void main(final String ... args) throws Exception {

        // The JWT token to validate.
        final var jwtToken = "<JWT_TOKEN>";

        // Load the X.509 certificate
        final var certificatePEM = """
                <PUBLIC_CERT_PEM>              
                """;

        final var certificate = getCertificateFromPEM(certificatePEM);

        // Parse JWT components
        final var jwtParts = jwtToken.split("\\.");
        if (jwtParts.length != 3) {
            System.out.println("Invalid JWT format");
            return;
        }

        // Decode and verify the JWT signature
        final var base64UrlHeader = jwtParts[0];
        final var base64UrlClaims = jwtParts[1];
        final var signature = jwtParts[2];

        // Verify the signature
        if (verifySignature(base64UrlHeader, base64UrlClaims, signature, certificate.getPublicKey())) {
            System.out.println("JWT signature is valid");
        } else {
            System.out.println("JWT signature is invalid");
        }
    }

    private static X509Certificate getCertificateFromPEM(String certificatePEM) throws Exception {
        certificatePEM = certificatePEM.replace("-----BEGIN CERTIFICATE-----", "")
                .replace("-----END CERTIFICATE-----", "")
                .replaceAll("\\s+", "");

        final var certificateBytes = Base64.getDecoder().decode(certificatePEM);

        final var certificateFactory = java.security.cert.CertificateFactory.getInstance("X.509");
        return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificateBytes));
    }

    private static boolean verifySignature(final String base64UrlHeader, final String base64UrlClaims, final String signature, final PublicKey publicKey) throws Exception {
        final var signedData = base64UrlHeader + "." + base64UrlClaims;
        final var signatureBytes = Base64.getUrlDecoder().decode(signature);

        final var verifier = Signature.getInstance("SHA256withRSA");
        verifier.initVerify(publicKey);
        verifier.update(signedData.getBytes());

        return verifier.verify(signatureBytes);
    }
}

Here's how the code works:

  1. Load the JWT token and public certificate from their respective PEM files.
  2. Parse the JWT token into its components: header, claims, and signature.
  3. Verify the signature by re-signing the header and claims and comparing it with the provided signature.

Replace <PUBLIC_CERT_PEM> and <JWT_TOKEN> with the actual content of your public certificate PEM file and the JWT token you want to validate.

Summary

In this article, we've explored how to generate and validate JWT tokens in Java using PEM files. This approach allows you to secure your applications by creating and verifying digitally signed tokens. Make sure to keep your private keys and certificates secure, as they are crucial for the security of your JWT-based authentication system.

Related Topic

Generating a Self-signed CA Certificate for JSON Web Token (JWT) in Java

Generating and Validating JWT Tokens in Java using Keystore

JWT (JSON Web Tokens) is a compact, URL-safe means of representing claims to be transferred between two parties. In this article, we will walk through how to generate and validate JWT tokens in Java, using a private certificate stored in a keystore. We will provide example Java code for both processes.

Generate JWT Token

Generating a JWT token involves several steps:

  1. Loading the Keystore: You need to load the keystore that contains the private key and certificate. You'll also specify the keystore password and the alias of the certificate in the keystore.
  2. Creating JWT Claims: Define the claims you want to include in the JWT. These can include the subject, issuer, expiration time, and a unique JWT ID (jti).
  3. Base64URL Encoding: Encode the JWT header and claims in base64 URL-safe format. This is a requirement for JWT.
  4. Combining Header and Claims: Combine the base64-encoded header and claims with a period separator.
  5. Signing the JWT: Sign the JWT using RSA with the private key from the keystore.
  6. Combining JWT Components: Combine the header, claims, and signature to create the final JWT token.

Here's the Java code for generating a JWT token:

import java.io.FileInputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.util.Base64;
import java.util.UUID;

public class GenerateJWTSignedByKSCert {

    public static void main(String... args) throws Exception {
        // Load the keystore and retrieve the private key and certificate
        final var keystorePath = "<CERTIFICATE_KEYSTORE>";
        final var keystorePassword = "<KEYSTORE_PASSWORD>";
        final var alias = "<CERTIFICATE_ALIAS>";
        final var keystore = KeyStore.getInstance("JKS");
        keystore.load(new FileInputStream(keystorePath), keystorePassword.toCharArray());
        final var privateKey = (RSAPrivateKey) keystore.getKey(alias, keystorePassword.toCharArray());

        // Sample JWT claims
        final var subject = "user123";
        final var issuer = "yourapp.com";
        final var expirationTimeMillis = System.currentTimeMillis() + 3600 * 1000; // 1 hour
        final var jwtID = UUID.randomUUID().toString();

        // Build JWT claims
        final var jwtHeader = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
        final var jwtClaims = "{\"sub\":\"" + subject + "\",\"iss\":\"" + issuer + "\",\"exp\":" + expirationTimeMillis + ",\"jti\":\"" + jwtID + "\"}";

        // Base64Url encode the JWT header and claims
        final var base64UrlHeader = base64UrlEncode(jwtHeader.getBytes());
        final var base64UrlClaims = base64UrlEncode(jwtClaims.getBytes());

        // Combine header and claims with a period separator
        final var headerClaims = base64UrlHeader + "." + base64UrlClaims;

        // Sign the JWT
        final var signature = signWithRSA(headerClaims, privateKey);

        // Combine the JWT components
        final var jwtToken = headerClaims + "." + signature;

        System.out.println("JWT Token: " + jwtToken);
    }

    // Base64 URL encoding
    private static String base64UrlEncode(byte[] data) {
        return Base64.getUrlEncoder().withoutPadding().encodeToString(data);
    }

    // Sign the JWT using RSA
    private static String signWithRSA(String data, RSAPrivateKey privateKey) throws Exception {
        // Perform the RSA signing (e.g., with Signature.getInstance("SHA256withRSA"))
        // and return the base64Url-encoded signature
        final var signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(data.getBytes());
        final var signatureBytes = signature.sign();
        return base64UrlEncode(signatureBytes);
    }
}

Validate JWT Token

Once you have generated a JWT token, you may need to validate it to ensure its integrity. Validation typically involves verifying the token's signature using the corresponding public key from the keystore. Here's the Java code for validating a JWT token:

import java.io.FileInputStream;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Base64;

public class ValidateJWTSignedByKSCert {

    public static void main(final String ... args) throws Exception {
        // The JWT token to validate.
        final var jwtToken = "<JWT_TOKEN>";

        // Load the keystore and retrieve the public key
        final var keystorePath = "<CERTIFICATE_KEYSTORE>";
        final var keystorePassword = "<KEYSTORE_PASSWORD>";
        final var alias = "<CERTIFICATE_ALIAS>";
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(new FileInputStream(keystorePath), keystorePassword.toCharArray());
        final var certificate = (X509Certificate) keystore.getCertificate(alias);

        // Parse JWT components
        final var jwtParts = jwtToken.split("\\.");
        if (jwtParts.length != 3) {
            System.out.println("Invalid JWT format");
            return;
        }

        // Decode and verify the JWT signature
        final var base64UrlHeader = jwtParts[0];
        final var base64UrlClaims = jwtParts[1];
        final var signature = jwtParts[2];

        // Verify the signature
        if (verifySignature(base64UrlHeader, base64UrlClaims, signature, certificate.getPublicKey())) {
            System.out.println("JWT signature is valid");
        } else {
            System.out.println("JWT signature is invalid");
        }
    }

    private static boolean verifySignature(final String base64UrlHeader, final String base64UrlClaims, final String signature, final PublicKey publicKey) throws Exception {
        final var signedData = base64UrlHeader + "." + base64UrlClaims;
        final var signatureBytes = Base64.getUrlDecoder().decode(signature);

        final var verifier = Signature.getInstance("SHA256withRSA");
        verifier.initVerify(publicKey);
        verifier.update(signedData.getBytes());

        return verifier.verify(signatureBytes);
    }
}

Tokens

In both code examples, there are tokens that need to be replaced with specific values:

  • <CERTIFICATE_KEYSTORE>: Replace this with the absolute path of the keystore. It's possible to have separate keystores for the private and public certificates.
  • <KEYSTORE_PASSWORD>: Replace this with the password that corresponds to the keystore.
  • <CERTIFICATE_ALIAS>: Replace this with the alias of the certificate in the keystore.
  • <JWT_TOKEN>: Replace this with the JWT token you want to validate.

By using the provided code and replacing these tokens with the appropriate values, you can generate and validate JWT tokens in Java with ease, using a private certificate stored in a keystore.

Related Topic

Generating a Self-signed CA Certificate for JSON Web Token (JWT) in Java

Showing JVM Flags and Values

Flags

Flag Description
UnlockDiagnosticVMOptions Unlocks the options intended for diagnosing the JVM. By default, this option is disabled and diagnostic options are not available.
PrintFlagsFinal Shows the final flag values that JVM uses.

Usage

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version

The usage of version argument is used to show the java version information instead of the help information.

Mocking Static Method with Mockito

Sample codes for mocking a static with Mockito.

The StringUtil class is a very trivial class that converts the text to all capitalized.

StringUtil class

public final class StringUtil {

    private StringUtil() {}

    public static String upperCase(String text) {
        return text.toUpperCase();
    }
}

Mocking the StringUtil.upperCase method

Stubbing the StringUtil.upperCase method to return test if test is passed as argument.

Dependencies Required

org.junit.jupiter:junit-jupiter-engine:5.8.2
org.mockito:mockito-inline:4.4.0

StringUtilTest class

import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

public class StringUtilTest {

    @Test
    void staticMethod() {
        try (var utilities = mockStatic(StringUtil.class)) {
            utilities.when(() -> StringUtil.upperCase("test")).thenReturn("test");
            assertEquals("test", StringUtil.upperCase("test"));
        }
    }
}

Java as a Windows service with NSSM

Pre-requisite

Installing a Java Service

  1. Register a java application as a windows service using the following syntax:

    nssm install <SERVICE_NAME> <JAVA_EXECUTABLE> <JAVA_ARGUMENTS>

    Example

    nssm install "JavaService" "${JAVA_HOME}\bin\java.exe" "-jar java-service.jar --spring.profiles.active=dev"
  2. Update the service with application directory using the following syntax:

    nssm set <SERVICE_NAME> AppDirectory <JAR_FILE_DIRECTORY>

    Example

    nssm set "JavaService" AppDirectory "C:\apps"
  3. Update the service with description using the following syntax:

    nssm set <SERVICE_NAME> Description <APP_DESCRIPTION>

    Example

    nssm set "JavaService" Description "A custom java service."

Displaying the NSSM details of the Java Service

Use the following syntax to display the details of the services:

nssm dump <SERVICE_NAME>

Example

nssm dump "JavaService"

Uninstalling a Java Service

Use the following syntax to remove a java service using nssm:

nssm remove <SERVICE_NAME> confirm

confirm parameter here specifies that we don't want to see the gui confirmation.

Example

nssm remove "JavaService" confirm
« Older posts