flow-with-jwt-authentication

Example how to enable JWT based authentication with Vaadin Flow and Spring Security

UNLICENSE License

Stars
14
Committers
2

Vaadin Flow example with JWT authentication

This example project is generated using start.vaadin.com, with Spring Security on. After that the Spring Security configuration is modified so that it JWT is used to store the authentication information, instead of the usual Java Servlet Session.

Even though Vaadin Flow app needs session for end-users anyways, this approach improves developer and end user experience when deploying new versions (no need to re-login when new UI is deployed).

The HelloWorldView contains a hack that makes this rather trivial Vaadin UI non-serializable and this way lose the session on server restart. This is just to make testing the approach easier. To compare to "default behaviour", uncomment the JWT configuration from SecurityConfiguration and restart the application (also clear cookies).

Running the application

The project is a standard Maven project. To run it from the command line, type mvnw (Windows), or ./mvnw (Mac & Linux), then open http://localhost:8080 in your browser.

You can also import the project to your IDE of choice as you would with any Maven project. Read more on how to import Vaadin projects to different IDEs (Eclipse, IntelliJ IDEA, NetBeans, and VS Code).

Deploying to Production

To create a production build, call mvnw clean package -Pproduction (Windows), or ./mvnw clean package -Pproduction (Mac & Linux). This will build a JAR file with all the dependencies and front-end resources, ready to be deployed. The file can be found in the target folder after the build completes.

Once the JAR file is built, you can run it using java -jar target/flowwithjwtauthentication-1.0-SNAPSHOT.jar

Project structure

  • MainLayout.java in src/main/java contains the navigation setup (i.e., the
    side/top bar and the main menu). This setup uses
    App Layout.
  • views package in src/main/java contains the server-side Java views of your application.
  • views folder in frontend/ contains the client-side JavaScript views of your application.
  • themes folder in frontend/ contains the custom CSS styles.

Useful links