• Stars
    star
    356
  • Rank 119,446 (Top 3 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON Web Token example that integrates both a Spring backend with an AngularJS frontend.

JSON Web Token / AngularJS / Spring Boot example

Blog post on this subject

This is an example project where a Spring REST API is secured using JSON Web Tokens. Since there are relatively few examples available for Java and there are some pitfalls (such as most sources pointing to a Java lib that's not straightforward to use) I decided to extract my proof of concept into a stand-alone example and publish it for all to see.

JSON Web Tokens

JSON Web Tokens have a few benefits over just sending a 'regular' token over the line. The more common approach to securing a REST API (outside of normal HTTP Basic Auth) is to send a random string as a token on succesful login from the server to the client. The client then sends this token on every request, and the server does an internal lookup on that token (in for example a REDIS cache or a simple Hashtable) to retrieve the corresponding user data.

With JSON Web Tokens the latter part isn't needed: the token itself contains a representation of the 'claims' of client: this can be just a username, but can also be extended to include any data you wish. This token is transmitted from the client on every request. The contents of the token are encrypted and a hash is added to prevent tampering: this way the content is secure: the server is the one signing and encrypting the token and is also the only one who had the key needed to decrypt the token.

In this example this key is fixed ("secretkey") but in a real life situations the secret key would simply be an array of bytes randomly generated on application startup. This has the added benefit that any tokens get automatically invalidated when you restart the service. If this behaviour is undesired you can persist the keys in for example REDIS.

Server side: Spring Boot

I like using Spring (Boot) to create RESTful services. On the server side, the JWT signing is done in the user/login REST call in UserController. It contains a tiny 'database' of 2 users, one of which has the 'admin' rights. The verification is done in a Filter (JwtFilter): it filters every request that matches "/api/*". If a correct token isn't found an exception is thrown. If a correct token is found, the claims object is added to the Http Request object and can be used in any REST endpoint (as shown in ApiController).

The heavy lifting for JWT signing is done by the more than excellent Java JWT library.

Client Side: AngularJS

The simple Angular app shows a login page. On successful login it checks with 'the API' which roles are available (of which the 'foo' role doesn't exist for any user).

Running

It is a standard Maven project and can be imported into your favorite IDE. You run the example by starting the WebApplication class (it has a main) and navigating to http://localhost:8080/. If everything is correct you should see a "Welcome to the JSON Web Token / AngularJR / Spring example!" message and a login form.

More Repositories

1

spring-boot-websocket-client

Demonstrates a Spring Boot Websocket + Stomp service with both a JavaScript and Java client
Java
99
star
2

spring-async

Asynchronous REST call with DeferredResult
Java
49
star
3

spark-of-life

Example of running a Genetic Algorithm (Travelling Salesman) on Apache Spark
Java
42
star
4

spring-boot-aop

Spring Boot Aspect Oriented Programming (AOP) Example
Java
27
star
5

kafka-serializer-example

Example of how to create your own custom serializers for Kafka queues including JSON, Smile and Kryo
Java
25
star
6

controller-advice-exception-handler

Showcases an Error Handler (Spring @ControllerAdvice) inheriting from a base handler
Java
21
star
7

adventofcode

My AdventOfCode implementations
Kotlin
16
star
8

spring-datastore-examples

Examples on how to integrate and test different databases/datastores with Spring and TestContainers
Java
11
star
9

spring-microservice-versioning

Java
10
star
10

microservice-versioning

Example project that demonstrates a few methods of handling micro service versioning.
Java
7
star
11

spring-dynamodb-integration

Integration tests with Spring, Amazon DynamoDB and TestContainers
Java
7
star
12

jfall-sentiment

JFall Presentation: Sentiment Analysis of Social Media Posts with Apache Spark
Java
6
star
13

spring-fileservice-example

Example Project to demonstrate file upload/download with Spring
Java
6
star
14

store-service

Java
5
star
15

adr-example

Example of Architectural Decision Records
4
star
16

example-antlr

Antlr example
Java
4
star
17

java-streams-bench

Benchmark comparing Java steams versus for-loops
Java
4
star
18

cassandra-it

Cassandra / Spring Integration and Repository tests examples
Java
4
star
19

archunit-example

Java
3
star
20

grub

A Spring Boot reference project
Java
2
star
21

api-security-examples

WIP
Java
2
star
22

cmd-oauth

Example of how to handle OAuth in command line applications
Java
2
star
23

spark-streaming-cassandra

Example project that stores twitter data in cassandra
Java
2
star
24

drools-example

Drools / Spring Boot example application
Java
2
star
25

spring-rest-jpa-seed

Example / Seed project that shows a minimal Spring MVC REST API backed by a Data JPA repository.
Java
2
star
26

spring-executors-example

Spring Executors Example
Java
2
star
27

example-axis2

Axis2 example for the poor sod who still has to use it ;)
2
star
28

kotlin-workshop

Kotlin Workshop Project
Kotlin
2
star
29

spring-open-tracing

Spring / Open tracing example project
Java
2
star
30

example-loadbalancer

Java
2
star
31

lambda

AWS Lambda in Java example
Java
1
star
32

api-integration-examples

Examples of integrating different API's in a Spring Boot application
Kotlin
1
star
33

adventofrust

Advent of Code solutions in Rust
Rust
1
star
34

axi-spring-workshop

Java
1
star
35

nqueens

Java
1
star
36

cassandra

Ccassandra test project
Java
1
star
37

miditest

Mucking about with Midi
Kotlin
1
star
38

random

Random test stuff
Java
1
star
39

spring-saga-temporal

Test project demonstrating Temporal.io integrated in Spring
Java
1
star
40

zoo

Zookeeper based nginx configuration
Java
1
star
41

cannit

Reddit canned response bot
Java
1
star
42

bing

Bing Traffic/Route API Wrapper
Java
1
star
43

fadecandy

Java Fadecandy client library
Java
1
star
44

spring-boot-prometheus

Example that shows how to add a Prometheus endpoint to Spring Boot
Java
1
star
45

spark-sentiment

Apache Spark Sentiment Analysis example
Java
1
star
46

nbtlib

nbt library
Java
1
star
47

hazelcast-example

Hazelcast example
Java
1
star
48

classiccsproblemskotlin

Classic Computer Science Problems in Kotlin
Java
1
star
49

spring-rest-security

Bare bones spring rest security example
Java
1
star
50

gpxrewrite

Rewrites .gpx files with different times / speeds / locations
Kotlin
1
star
51

spring-graphql

Kotlin
1
star
52

twitter-markov

Fun with Markov Chains based on Tweets
Java
1
star
53

graph

Test
Java
1
star
54

today

Java
1
star
55

custom-argument-spring

Example demonstrating the usage of a HandlerMethodArgumentResolver to resolve custom controller arguments.
Java
1
star
56

spring-mongo

Spring / Mongo integration example
Java
1
star
57

lib

Assorted libraries (math, utility, etc.)
Java
1
star
58

s5

Simple S3 Sync
JavaScript
1
star
59

example-caching

AngularJS caching example
JavaScript
1
star
60

antlr-pres

Antlr 4 Presentation Code
Java
1
star
61

example-command-java8

Example of using Java 8 features to implement the command pattern.
Java
1
star
62

simplelogservice

Simple Logging Service used for IoT projects
Java
1
star
63

tjilper-old

Twitter clone example to test / showcase different technologies
Java
1
star
64

example-antlr4

Antlr4 examples
Java
1
star
65

payslip-util

Utility to download payslips from https://werknemerloket.nl
Kotlin
1
star
66

spring-react-skeleton

Spring / Kotlin / React skeleton application with API security and user storage, login and registration
Kotlin
1
star
67

parallel-builds

Building a Spring back-end with a JavaScript front-end in parallel into a single Docker container
TypeScript
1
star
68

example-chat

Chat over WebSockets with AngularJS on the front-end and Spring Boot on the back-end.
HTML
1
star
69

kryotest

Kryo serialization tests
Java
1
star
70

set-performance-test

Performance tests for different set implementations
Java
1
star