Using Java 8 Optional to set Current User ModelAttribute in Spring Security

This post will be a bit of a follow on from that last one which discussed using a User entity from a database in Spring Security Authentication Object, so it’s probably worth reading that first.

The new Optional  class in Java 8 lets us create a neat little mechanism for transferring our User entity, if one exists, into every controller method, using a ControllerAdvice  along the way.

Starting with the UserPrincipal  class from the last post we’ll make the following modification:

Continue reading “Using Java 8 Optional to set Current User ModelAttribute in Spring Security”