Posts

Showing posts with the label java

Securing Spring Boot Microservices with Spring Security

Overview Basically, our microservices are secured this way : The user provides his credentials via a public endpoint If authenticated, the server returns an authentication token (JWT) The JWT is attached to each subsequent request via an HTTP header: Authorization:Bearer TOKEN You can find the source code at https://github.com/vedrax-admin/spring-microservices User Microservice This microservice uses a MySQL database. We begin by creating the Account table: Below is a script to insert some dummy data: The Account entity will be created using JPA: The account repository: User Principal We create a class named UserPrincipal which implements UserDetails . In order to be accessible by all microservices, this class is located in our shared module. JWT Token Service This service is responsible for creating expiring JWT. We can also parse a JWT for getting the UserPrincipal . We use the io.jsonwebtoken dependency for that. Account Service...

Java : read and write to a binary file

Code for reading a binary file : Code for writing to a binary file : As said in a previous article, you should use buffered stream for improving I/O operations. It's also considered a best practice to use the try-with-resources for releasing resources.

Java : Read and Write to a file

The code below show you how you can read a file using Java: Code for Writing to a file: By using a buffered stream, you can dramatically improve the performance of I/O operations. We also use the try-with-resources for automatically closes the object and releases its resources.